Skip to content

Commit

Permalink
Allow birthday/anniversary to occur on exact same year (but not before)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Feb 24, 2023
1 parent 3fe3774 commit d905ece
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions anniversary.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,13 @@ has his birthday postponed until the first of the following month in
years where that day does not occur. [Calendrical Calculations p. 111]
*/
func GetBirthdayOrAnniversary(hyear int, date HDate) (HDate, error) {
if hyear <= date.Year() {
return HDate{}, errors.New("year " + strconv.Itoa(hyear) + " occurs on or before original date")
origYear := date.Year()
if hyear == origYear {
return date, nil
} else if hyear < origYear {
return HDate{}, errors.New("year " + strconv.Itoa(hyear) + " occurs before original date")
}
isOrigLeap := IsLeapYear(date.Year())
isOrigLeap := IsLeapYear(origYear)
month := date.Month()
day := date.Day()
if (month == Adar1 && !isOrigLeap) || (month == Adar2 && isOrigLeap) {
Expand Down
12 changes: 12 additions & 0 deletions anniversary_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hdate_test

import (
"errors"
"fmt"
"strings"
"testing"
Expand Down Expand Up @@ -88,3 +89,14 @@ func TestBirthday(t *testing.T) {
}
}
}

func TestBirthdayOnOrBefore(t *testing.T) {
assert := assert.New(t)
orig := hdate.New(5753, hdate.Adar1, 9)
birthday, err := hdate.GetBirthdayOrAnniversary(5753, orig)
assert.Equal(err, nil)
assert.Equal(birthday, orig)
birthday, err = hdate.GetBirthdayOrAnniversary(5752, orig)
assert.Equal(birthday, hdate.HDate{})
assert.Equal(err, errors.New("year 5752 occurs before original date"))
}

0 comments on commit d905ece

Please sign in to comment.