Skip to content

Commit 84e100f

Browse files
authored
Set milliseconds to 0 before returning schedule (fixes #363) (#368)
* test: add failing tests * fix: set milliseconds to 0 before returning schedule * refactor: only set milliseconds to 0 if they're not already * perf: remove redundant setMilliseconds call
1 parent c4a3213 commit 84e100f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/CronExpression.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,15 @@ export class CronExpression {
396396
if (startTimestamp === currentDate.getTime()) {
397397
if (dateMathVerb === 'Add' || currentDate.getMilliseconds() === 0) {
398398
currentDate.applyDateOperation(dateMathVerb, TimeUnit.Second, this.#fields.hour.values.length);
399-
} else {
400-
currentDate.setMilliseconds(0);
401399
}
402400
continue;
403401
}
404402
break;
405403
}
406404

405+
if (currentDate.getMilliseconds() !== 0) {
406+
currentDate.setMilliseconds(0);
407+
}
407408
this.#currentDate = currentDate;
408409
return currentDate;
409410
}

tests/CronExpression.test.ts

+16
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ describe('CronExpression', () => {
8484
expect(cronExpression.toString()).toBe('0 0 0 1 1 0');
8585
});
8686

87+
test('should set milliseconds to 0 on next scheduled date (issue #363)', () => {
88+
const cronExpression = CronExpressionParser.parse('* * * * *', {
89+
currentDate: '2020-03-06T10:02:01.002Z',
90+
});
91+
const nextDate = cronExpression.next();
92+
expect(nextDate.toISOString()).toBe('2020-03-06T10:03:00.000Z');
93+
});
94+
95+
test('should set milliseconds to 0 on previous scheduled date (issue #363)', () => {
96+
const cronExpression = CronExpressionParser.parse('* * * * *', {
97+
currentDate: '2020-03-06T10:02:01.002Z',
98+
});
99+
const prevDate = cronExpression.prev();
100+
expect(prevDate.toISOString()).toBe('2020-03-06T10:02:00.000Z');
101+
});
102+
87103
describe('stringify', () => {
88104
test('stringify cron expression all stars no seconds 0 * * * * *', () => {
89105
const expected = '0 * * * * *';

0 commit comments

Comments
 (0)