将来时区倒退时如何处理

How to handle when timezone goes backwards in the future

我在单独的循环迭代中生成了两组重复事件,但在比较生成的冲突结果时遇到了冲突。这似乎是时代倒退了,我不知道如何解决这个问题?

第一次重复活动将:

第二次重复活动将:

要生成我每天在本地时区循环的事件 "Europe/Stockholm" 使用 Nodatime 像这样:

String timeZone = "Europe/Stockholm";
for (ZonedDateTime date_Local = repeatSeriesStartDate_Local; date_Local <= LoopEndDate_Local; date_Local = new ZonedDateTime(Instant.FromDateTimeUtc(date_Local.ToDateTimeUtc().AddDays(1).ToUniversalTime()),timeZone))

我的问题出现在 2016 年 10 月 29 日/30 日,当时时钟倒转,第二条规则与第一条规则冲突。 http://www.timeanddate.com/time/change/sweden/stockholm?year=2016

冲突次数如下:

我正在使用类似这样的算法来测试冲突

我应该如何处理这些时移冲突?

虽然如果您能澄清问题会很有帮助,但我现在先做一些假设。如有必要,我可以稍后编辑问题。

您可能想要做的是这样的事情:

for (LocalDate date = startDate; date <= endDate; date = date.PlusDays(1))
{
    ZonedDateTime zdt = date.At(eventTime).InZone(tz, SchedulingResolver);
    Console.WriteLine(zdt); // or whatever you want to do from here
}

SchedulingResolver 实现是 here, and is only necessary if you are using the 1.x version of Noda Time. If you are using 2.x, then you can just use InZoneLeniently(tz) instead, as the behavior of the lenient resolver in 2.x has changed to match (see "lenient resolver changes" in the 2.x migration guide)。

重点是:

  • ZonedDateTime 通常最好用作中间类型。

    • 您有基于本地日期的每日事件,因此 LocalDate 更合适。

    • 如果您的事件基于固定的 24 小时轮换(即 UTC 日),那么 Instant 会更合适。

  • 解析器用于将不明确或无效的 LocalDateTime 值映射回特定时刻。我推荐用于调度目的的解析器是:

    • 当时钟向前 (spring) 时,DST 偏差(通常为 1 小时)提前
    • 在时钟倒退(下降)时选择第一个实例

虽然正如 Jon 提到的那样 - 您的需求可能会有所不同,但实际上我们无法回答您应该做什么。确实有业务需要与我推荐的不同的解析器规则。