hebcal / hebcal-es6

perpetual Jewish Calendar with holidays, Shabbat and holiday candle lighting and havdalah times, Torah readings, and more
https://hebcal.github.io/api/core/
GNU General Public License v2.0
100 stars 14 forks source link

new HDate with month 12 and day greater than 29 error #390

Closed arecaps closed 7 months ago

arecaps commented 7 months ago

When an invalid date is passed to the constructor of HDate(), normally it is handled by calling fix(date), which will either increment or decrement the month to return a valid date. For example, new HDate(30, 2, 5784) will roll over and return HDate {month: 3, day: 1, year: 5784}.
This does not work for Adar in a non leap year, so a call to new HDate(30, 12, 5783) will not return HDate {month: 1, day: 1, year: 5783} as I expected, rather HDate {month: 12, day: 1, year: 5783}.
This is because of this line of code increments the month to 13, and then this next function decrements it back to 12.
A fix for this would be to replace date.mm += 1; in here with

 if (date.month === months.ADAR_I && !date.isLeapYear()) {
      date.month = months.NISAN;
    }
    else date.month += 1;

I don't want to open a PR, because I was unable to test locally.

mjradwin commented 7 months ago

Thanks for the bug report! Fixed in v5.1.1