Skip to content

GetNextOccurrence calculated incorrect when switching from DST #71

@redstarty

Description

@redstarty

Hello there!

This issue was initially spotted using Hangfire jobs scheduler, but I managed to investigate it to the following point:

So, imagine we have a cron 0 0 2 * * 0 (run at 2:00 each Sunday) and a specific time zone W. Europe Standard Time (supports DST, UTC+1 when non-DST, UTC+2 when DST). And Sunday 27th of October is the day when we move from DST to non-DST which leads to time 02:00 in this timezone happen twice (at 00:00 UTC and 01:00 UTC).

We request next occurrence exactly at 00:30 UTC and expect it to be 01:00 UTC time, but it is actually skipped.

Here, I draw a picture:
dst drawio

And here is a code to reproduce it:

using Cronos;
using TimeZoneConverter;

var cron = CronExpression.Parse("0 0 2 * * 0", CronFormat.Standard | CronFormat.IncludeSeconds);
var requestedAt = new DateTime(2024, 10, 27, 0, 30, 0, DateTimeKind.Utc);
var nextOccurrence = cron.GetNextOccurrence(requestedAt, TZConvert.GetTimeZoneInfo("W. Europe Standard Time"), inclusive: false);

Console.WriteLine(nextOccurrence?.ToString("o"));

The code output is 2024-11-03T01:00:00.0000000Z - next week. And I would expect it to be 2024-10-27T01:00:00.0000000Z

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions