From 877eceee67dc8775b9ed71ecd3d68ff1d7e9ce3d Mon Sep 17 00:00:00 2001 From: Trung Dang Date: Fri, 10 Aug 2018 16:19:39 +0800 Subject: [PATCH] fix issue when cronTime _getNextDateFrom not calculate correctly --- lib/cron.js | 7 +++++++ tests/test-crontime.js | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/cron.js b/lib/cron.js index 92823fea..d2051203 100644 --- a/lib/cron.js +++ b/lib/cron.js @@ -183,6 +183,13 @@ */ _getNextDateFrom: function(start) { var date = moment(start); + if (!this.realDate) { + const milliseconds = (start.milliseconds && start.milliseconds()) || (start.getMilliseconds && start.getMilliseconds()) || 0; + if (milliseconds > 0) { + date.milliseconds(0); + date.seconds(date.seconds() + 1); + } + } if (date.toString() == 'Invalid date') { console.log('ERROR: You specified an invalid date.'); diff --git a/tests/test-crontime.js b/tests/test-crontime.js index ebcb70c7..1b1677ea 100644 --- a/tests/test-crontime.js +++ b/tests/test-crontime.js @@ -190,4 +190,16 @@ describe('crontime', function() { }).to.throw(Error); }); }); + + it('should strip off millisecond', ()=>{ + const cronTime = new cron.CronTime('0 */10 * * * *'); + const x = cronTime._getNextDateFrom(new Date("2018-08-10T02:20:00.999Z")); + expect(x.toISOString()).to.equal('2018-08-10T02:30:00.000Z'); + }); + + it('should strip off millisecond (2)', ()=>{ + const cronTime = new cron.CronTime('0 */10 * * * *'); + const x = cronTime._getNextDateFrom(new Date("2018-08-10T02:19:59.999Z")); + expect(x.toISOString()).to.equal('2018-08-10T02:20:00.000Z'); + }); });