Skip to content

Commit

Permalink
add a 24 hour bound to _findPreviousDSTJump + test.
Browse files Browse the repository at this point in the history
  • Loading branch information
GijsvandenHoven authored and intcreator committed Mar 5, 2023
1 parent df420d7 commit d905864
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,27 @@ function CronTime(luxon) {
* When the jump is found, the range of the jump is investigated to check for acceptable cron times.
* A pair is returned, whose first is a boolean representing if an acceptable time was found inside the jump,
* and whose second is a DateTime representing the first millisecond after the jump.
*
* The input date is expected to be decently close to a DST jump.
* Up to a day in the past is checked before an error is thrown.
* @param date
*/
_findPreviousDSTJump: function (date) {
/** @type number */
let expectedMinute, expectedHour, actualMinute, actualHour;
/** @type DateTime */
let maybeJumpingPoint;

// representing one day of backwards checking. If this is hit, the input must be wrong.
const iterationLimit = 60 * 24;
let iteration = 0;
do {
if (iteration++ > iterationLimit) {
throw new Error(
`ERROR: This DST checking related function assumes the input DateTime (${date.toISO()}) is within 24 hours of a DST jump.`
);
}

expectedMinute = date.minute - 1;
expectedHour = date.hour - 1;

Expand Down
15 changes: 14 additions & 1 deletion tests/crontime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,20 @@ describe('crontime', () => {
expect(nextDate - currentDate).toEqual(1000 * 60);
});
// Do not think a similar test for secondly job is necessary, the minutely one already ensured no double hits in the overlap zone.

it('Should throw when dates that are not within 24 hours of DST jumps are checked for past DST jumps', () => {
const cronTime = new cron.CronTime('* * * * *');
const tryFindPastDST = isoTime => () => {
const maybeBadDate = luxon.DateTime.fromISO(isoTime, {
zone: 'Asia/Amman'
});
expect(maybeBadDate.invalid).toEqual(null);
cronTime._findPreviousDSTJump(maybeBadDate);
};

// This timezone jumps from 0:00 to 1:00 on March 30th, so the cutoff is March 31st 1:00:00
expect(tryFindPastDST('2018-03-31T00:59:00')).not.toThrow();
expect(tryFindPastDST('2018-03-31T01:00:00')).toThrow();
});
// The following few DST related tests do not need specific dates that are actually DST,
// the functions they are calling assume the given parameters encapsulate a DST jump,
// and use the raw hour and minute data to check it from there.
Expand Down

0 comments on commit d905864

Please sign in to comment.