Skip to content

Cron appears to repeat a date #5675

@tmcw

Description

@tmcw

What version of Effect is running?

3.18.4

What steps can reproduce the bug?

This test case (pasted into Cron.test.ts if you want) I think demos the issue:

it.only.for([
    ["5 2 * * 4"]
  ])("equivalence test %s", ([str]) => {
    const start = new Date("2020-01-01 0:00:01")
    const end = new Date("2021-01-01 0:00:01")

    const getYear = (generator: IterableIterator<Date>) => {
      const res: Array<Date> = []
      for (const date of generator) {
        if (date > start && date < end) {
          res.push(date)
        } else {
          return res
        }
      }
      return res
    }

    const fwd = getYear(Cron.sequence(Cron.unsafeParse(str), start))
    for (let i = 0; i < fwd.length; i++) {
      console.log(fwd[i], fwd[i] - fwd[i - 1])
    }
  })

What is the expected behavior?

I would expect this cron expression, which in plain english would repeat "at 02:05 on Thursday", to consistently repeat once every thursday at 2:05.

What do you see instead?

This is the sequence of generated instances, with the "milliseconds since the last cron run" printed in the second column.

2020-01-02T07:05:00.000Z NaN
2020-01-09T07:05:00.000Z 604800000
2020-01-16T07:05:00.000Z 604800000
2020-01-23T07:05:00.000Z 604800000
2020-01-30T07:05:00.000Z 604800000
2020-02-06T07:05:00.000Z 604800000
2020-02-13T07:05:00.000Z 604800000
2020-02-20T07:05:00.000Z 604800000
2020-02-27T07:05:00.000Z 604800000
2020-03-05T07:05:00.000Z 604800000
2020-03-12T07:05:00.000Z 604800000
2020-03-19T06:05:00.000Z 601200000
2020-03-26T06:05:00.000Z 604800000
2020-04-02T06:05:00.000Z 604800000
2020-04-09T06:05:00.000Z 604800000
2020-04-16T06:05:00.000Z 604800000
2020-04-23T06:05:00.000Z 604800000
2020-04-30T06:05:00.000Z 604800000
2020-05-07T06:05:00.000Z 604800000
2020-05-14T06:05:00.000Z 604800000
2020-05-21T06:05:00.000Z 604800000
2020-05-28T06:05:00.000Z 604800000
2020-06-04T06:05:00.000Z 604800000
2020-06-11T06:05:00.000Z 604800000
2020-06-18T06:05:00.000Z 604800000
2020-06-25T06:05:00.000Z 604800000
2020-07-02T06:05:00.000Z 604800000
2020-07-09T06:05:00.000Z 604800000
2020-07-16T06:05:00.000Z 604800000
2020-07-23T06:05:00.000Z 604800000
2020-07-30T06:05:00.000Z 604800000
2020-08-06T06:05:00.000Z 604800000
2020-08-13T06:05:00.000Z 604800000
2020-08-20T06:05:00.000Z 604800000
2020-08-27T06:05:00.000Z 604800000
2020-09-03T06:05:00.000Z 604800000
2020-09-10T06:05:00.000Z 604800000
2020-09-17T06:05:00.000Z 604800000
2020-09-24T06:05:00.000Z 604800000
2020-10-01T06:05:00.000Z 604800000
2020-10-08T06:05:00.000Z 604800000
2020-10-15T06:05:00.000Z 604800000
2020-10-22T06:05:00.000Z 604800000
2020-10-29T06:05:00.000Z 604800000
2020-11-05T06:05:00.000Z 604800000
2020-11-05T07:05:00.000Z 3600000
2020-11-12T07:05:00.000Z 604800000
2020-11-19T07:05:00.000Z 604800000
2020-11-26T07:05:00.000Z 604800000
2020-12-03T07:05:00.000Z 604800000
2020-12-10T07:05:00.000Z 604800000
2020-12-17T07:05:00.000Z 604800000
2020-12-24T07:05:00.000Z 604800000
2020-12-31T07:05:00.000Z 604800000

Note these lines:

2020-11-05T06:05:00.000Z 604800000
2020-11-05T07:05:00.000Z 3600000
2020-11-12T07:05:00.000Z 604800000

This is the odd behavior. The cron runs on November 5th at 6:05 utc, and then at 7:05 utc.

Yes, daylight savings changed in 2020 on November 1st, and that's probably the culprit, but this particular hour was not repeated, and you can test this with a variety of weekday expressions and you'll get the unusual repeated instance for all of them.

Additional information

This was done on my computer which is in EDT time zone. Parsing the cron expression as "utc" gives consistent (correct) results, with no repeated time.

Using a local timezone gives the unusual repeated results regardless of which day-of-week is specified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions