Repository: harrisiirak/cron-parser (ts)

Issue:
v5 regression: `next()` doesn't set milli-seconds correctly
v5 doesn't set the milli-seconds to zero for the next date. They are carried over from `currentDate` instead.

```ts
const cronExpression = CronExpressionParser.parse('* * * * *', {
   currentDate: '2020-03-06T10:02:01.002Z',
   tz: 'Europe/Berlin'
 })
 const res = cronExpression.next().toISOString()
```
`res` is "2020-03-06T10:03:00.002Z" in v5, but should be "2020-03-06T10:03:00.000Z". With v4 the milli-seconds were correct.

Pull request description:
Set milliseconds to 0 before returning schedule (fixes #363)
This is my first attempt at a real code change in the repository. Please let me know if anything needs changing.

Fixes #363

Patch:
diff --git a/src/CronExpression.ts b/src/CronExpression.ts
index aed7127..85235fa 100644
--- a/src/CronExpression.ts
+++ b/src/CronExpression.ts
@@ -396,14 +396,15 @@ export class CronExpression {
       if (startTimestamp === currentDate.getTime()) {
         if (dateMathVerb === 'Add' || currentDate.getMilliseconds() === 0) {
           currentDate.applyDateOperation(dateMathVerb, TimeUnit.Second, this.#fields.hour.values.length);
-        } else {
-          currentDate.setMilliseconds(0);
         }
         continue;
       }
       break;
     }
 
+    if (currentDate.getMilliseconds() !== 0) {
+      currentDate.setMilliseconds(0);
+    }
     this.#currentDate = currentDate;
     return currentDate;
   }


Test patch:
diff --git a/tests/CronExpression.test.ts b/tests/CronExpression.test.ts
index 714d882..82b4672 100644
--- a/tests/CronExpression.test.ts
+++ b/tests/CronExpression.test.ts
@@ -84,6 +84,22 @@ describe('CronExpression', () => {
     expect(cronExpression.toString()).toBe('0 0 0 1 1 0');
   });
 
+  test('should set milliseconds to 0 on next scheduled date (issue #363)', () => {
+    const cronExpression = CronExpressionParser.parse('* * * * *', {
+      currentDate: '2020-03-06T10:02:01.002Z',
+    });
+    const nextDate = cronExpression.next();
+    expect(nextDate.toISOString()).toBe('2020-03-06T10:03:00.000Z');
+  });
+
+  test('should set milliseconds to 0 on previous scheduled date (issue #363)', () => {
+    const cronExpression = CronExpressionParser.parse('* * * * *', {
+      currentDate: '2020-03-06T10:02:01.002Z',
+    });
+    const prevDate = cronExpression.prev();
+    expect(prevDate.toISOString()).toBe('2020-03-06T10:02:00.000Z');
+  });
+
   describe('stringify', () => {
     test('stringify cron expression all stars no seconds 0 * * * * *', () => {
       const expected = '0 * * * * *';