Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-cron-next-missing-day-overflow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix `Cron.next` skipping earlier matching days when the upcoming day-of-month does not exist in the current month.
4 changes: 4 additions & 0 deletions packages/effect/src/Cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,10 @@ const stepCron = (cron: Cron, now: DateTime.DateTime.Input | undefined, directio
} else {
b = daysInMonth(current) - currentDay + boundary.day
}
} else if (!reverse && nextDay > daysInMonth(current)) {
// The next matching day does not exist in the current month. Setting it
// directly would overflow and skip earlier matching days next month.
b = daysInMonth(current) - currentDay + boundary.day
} else {
b = nextDay - currentDay
}
Expand Down
15 changes: 15 additions & 0 deletions packages/effect/test/Cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ describe("Cron", () => {
deepStrictEqual(next(Cron.parseUnsafe("5 0 8 2 *", london), after), DateTime.toDateUtc(amsterdamTime))
})

it("next does not skip earlier days when the upcoming day is missing from the month", () => {
const tz = DateTime.zoneMakeNamedUnsafe("UTC")
const cron = Cron.parseUnsafe("0 0 1,16,31 * *", tz)
// From Feb 18 the next matching day in February would be the 31st, which does
// not exist. Rolling onto it must not overshoot past March 1 (also a match).
deepStrictEqual(next(cron, new Date("2020-02-18T00:00:00.000Z")), new Date("2020-03-01T00:00:00.000Z"))
// `*/15` expands to days [1, 16, 31] and exhibits the same wrap.
deepStrictEqual(
next(Cron.parseUnsafe("0 0 */15 * *", tz), new Date("2020-02-18T00:00:00.000Z")),
new Date("2020-03-01T00:00:00.000Z")
)
// 30-day month: from the 20th the next day is the 31st (missing) -> July 1.
deepStrictEqual(next(cron, new Date("2024-06-20T00:00:00.000Z")), new Date("2024-07-01T00:00:00.000Z"))
})

it("prev", () => {
const utc = DateTime.zoneMakeNamedUnsafe("UTC")
const before = new Date("2024-01-04T16:21:00Z")
Expand Down
Loading