From 61d452e2935f53c025d8803233bcaa7ba5e7c0c3 Mon Sep 17 00:00:00 2001 From: "Michael J. Radwin" Date: Thu, 23 Jul 2020 19:51:22 -0700 Subject: [PATCH] Fix abs2greg() for edge case of December 31 during years 1-99 CE --- package.json | 2 +- src/greg.js | 12 ++++++++++-- src/greg.spec.js | 17 +++++++++++++++++ src/hdate.spec.js | 12 ++++++++++++ src/hebcal.js | 2 +- src/hebcal.spec.js | 17 ++++++++++++++++- 6 files changed, 57 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index cf3de67..c0922a8 100644 --- a/package.json +++ b/package.json @@ -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)", diff --git a/src/greg.js b/src/greg.js index 82dec27..86d1ffb 100644 --- a/src/greg.js +++ b/src/greg.js @@ -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; }, }; diff --git a/src/greg.spec.js b/src/greg.spec.js index a8d508f..d0edc82 100644 --- a/src/greg.spec.js +++ b/src/greg.spec.js @@ -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); diff --git a/src/hdate.spec.js b/src/hdate.spec.js index 3ed5f1d..38f08b4 100644 --- a/src/hdate.spec.js +++ b/src/hdate.spec.js @@ -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); diff --git a/src/hebcal.js b/src/hebcal.js index c51841a..7085cee 100644 --- a/src/hebcal.js +++ b/src/hebcal.js @@ -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) { diff --git a/src/hebcal.spec.js b/src/hebcal.spec.js index c8dbb92..3df6a21 100644 --- a/src/hebcal.spec.js +++ b/src/hebcal.spec.js @@ -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'; @@ -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) => {