Skip to content

Commit

Permalink
Fix abs2greg() for edge case of December 31 during years 1-99 CE
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Jul 24, 2020
1 parent 78244e1 commit 61d452e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hebcal/core",
"version": "2.2.1",
"version": "2.2.2",
"author": "Michael J. Radwin (https://github.com/mjradwin)",
"contributors": [
"Eyal Schachter (https://github.com/Scimonster)",
Expand Down
12 changes: 10 additions & 2 deletions src/greg.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,17 @@ export const greg = {
let year = 400 * n400 + 100 * n100 + 4 * n4 + n1;

if (4 == n100 || 4 == n1) {
return new Date(year, 11, 31);
const dt = new Date(year, 11, 31);
if (year < 100) {
dt.setFullYear(year);
}
return dt;
}

return new Date(new Date(++year, 0, day).setFullYear(year)); // new Date() is very smart
const dt = new Date(++year, 0, day);
if (year < 100) {
dt.setFullYear(year);
}
return dt;
},
};
17 changes: 17 additions & 0 deletions src/greg.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ test('abs2greg', (t) => {
t.is(dt.getDate(), 8);
});

test('abs2greg-88ce', (t) => {
const dt = greg.abs2greg(32141); // 0088-12-30
t.is(dt.getFullYear(), 88);
t.is(dt.getMonth(), 11);
t.is(dt.getDate(), 30);

const dt2 = greg.abs2greg(32142);
t.is(dt2.getFullYear(), 88);
t.is(dt2.getMonth(), 11);
t.is(dt2.getDate(), 31);

const dt3 = greg.abs2greg(32143);
t.is(dt3.getFullYear(), 89);
t.is(dt3.getMonth(), 0);
t.is(dt3.getDate(), 1);
});

test('daysInMonth', (t) => {
t.is(greg.daysInMonth(2, 2020), 29);
t.is(greg.daysInMonth(2, 2019), 28);
Expand Down
12 changes: 12 additions & 0 deletions src/hdate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,18 @@ test('abs2hebrew', (t) => {
t.is(h.dd, 15);
});

test('abs2hebrew-88ce', (t) => {
const h2 = HDate.abs2hebrew(32141);
t.is(h2.yy, 3849);
t.is(h2.mm, months.SHVAT);
t.is(h2.dd, 1);

const h3 = HDate.abs2hebrew(32142);
t.is(h3.yy, 3849);
t.is(h3.mm, months.SHVAT);
t.is(h3.dd, 2);
});

test('throws-abs2hebrew', (t) => {
const error = t.throws(() => {
HDate.abs2hebrew(12345678);
Expand Down
2 changes: 1 addition & 1 deletion src/hebcal.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function getAbs(d) {
* @param {HebrewCalendar.Options} options
* @return {number[]}
*/
function getStartAndEnd(options) {
export function getStartAndEnd(options) {
if ((options.start && !options.end) || (options.end && !options.start)) {
throw new TypeError('Both options.start and options.end are required');
} else if (options.start && options.end) {
Expand Down
17 changes: 16 additions & 1 deletion src/hebcal.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import {HebrewCalendar} from './hebcal';
import {HebrewCalendar, getStartAndEnd} from './hebcal';
import {HDate} from './hdate';
import {flags} from './event';
import {Location} from './location';
Expand Down Expand Up @@ -55,12 +55,27 @@ test('greg-year', (t) => {
t.is(gregDtString(events[81]), '12/24/1993');
});

test('getStartAndEnd-2digit', (t) => {
const [start, end] = getStartAndEnd({year: 88});
t.is(start, 31777);
t.is(end, 32142);
});

test('greg-2digit-year', (t) => {
const options = {
year: 50,
};
const events = HebrewCalendar.calendar(options);
t.is(events[0].getDate().greg().getFullYear(), 50);
t.is(events[events.length - 1].getDate().greg().getFullYear(), 50);

const opts2 = {
addHebrewDates: true,
year: 88,
};
const events2 = HebrewCalendar.calendar(opts2);
t.is(events2[0].getDate().greg().getFullYear(), 88);
t.is(events2[events2.length - 1].getDate().greg().getFullYear(), 88);
});

test('heb-year', (t) => {
Expand Down

0 comments on commit 61d452e

Please sign in to comment.