diff --git a/lib/date.js b/lib/date.js index 16247ff7..34f36d77 100644 --- a/lib/date.js +++ b/lib/date.js @@ -15,15 +15,27 @@ CronDate.prototype.addDay = function() { }; CronDate.prototype.addHour = function() { + const prev = this.getTime(); this._date.add(1, 'hour').startOf('hour'); + if (this.getTime() <= prev) { + this._date.add(1, 'hour'); + } }; CronDate.prototype.addMinute = function() { + const prev = this.getTime(); this._date.add(1, 'minute').startOf('minute'); + if (this.getTime() < prev) { + this._date.add(1, 'hour'); + } }; CronDate.prototype.addSecond = function() { + const prev = this.getTime(); this._date.add(1, 'second').startOf('second'); + if (this.getTime() < prev) { + this._date.add(1, 'hour'); + } }; CronDate.prototype.getDate = function() { diff --git a/test/timezone.js b/test/timezone.js index 87966b84..bd5a4d97 100644 --- a/test/timezone.js +++ b/test/timezone.js @@ -264,6 +264,113 @@ test('It works on DST end', function(t) { t.throws(function() { date = interval.next(); }); + + options = { + currentDate : new Date('Sun Oct 29 2016 01:00:00 GMT+0200') + } + + interval = CronExpression.parse('0 12 * * *', options); + t.ok(interval, 'Interval parsed'); + + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 29, '29th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 30, '30th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 31, '31st'); + + options = { + currentDate : new Date('Sun Oct 29 2016 02:59:00 GMT+0200') + } + + interval = CronExpression.parse('0 12 * * *', options); + t.ok(interval, 'Interval parsed'); + + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 29, '29th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 30, '30th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 31, '31st'); + + options = { + currentDate : new Date('Sun Oct 29 2016 02:59:59 GMT+0200') + } + + interval = CronExpression.parse('0 12 * * *', options); + t.ok(interval, 'Interval parsed'); + + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 29, '29th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 30, '30th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 31, '31st'); + + options = { + currentDate : new Date('Sun Oct 30 2016 01:00:00 GMT+0200') + } + + interval = CronExpression.parse('0 12 * * *', options); + t.ok(interval, 'Interval parsed'); + + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 30, '30th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 31, '31st'); + + options = { + currentDate : new Date('Sun Oct 30 2016 01:59:00 GMT+0200') + } + + interval = CronExpression.parse('0 12 * * *', options); + t.ok(interval, 'Interval parsed'); + + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 30, '30th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 31, '31st'); + + options = { + currentDate : new Date('Sun Oct 30 2016 01:59:59 GMT+0200') + } + + interval = CronExpression.parse('0 12 * * *', options); + t.ok(interval, 'Interval parsed'); + + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 30, '30th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 31, '31st'); + + options = { + currentDate : new Date('Sun Oct 30 2016 02:59:00 GMT+0200') + } + + interval = CronExpression.parse('0 12 * * *', options); + t.ok(interval, 'Interval parsed'); + + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 30, '30th'); + date = interval.next(); + t.equal(date.getHours(), 12, '12'); + t.equal(date.getDate(), 31, '31st'); } catch (err) { t.ifError(err, 'Interval parse error'); }