夏令时变更影响iCalendar文件的保存与加载结果?

2 投票
1 回答
753 浏览
提问于 2025-04-11 18:17

我有一些单元测试,今天开始失败了,因为刚刚进入夏令时。

我们正在使用iCalendar的Python模块来加载和保存ics文件。

下面的脚本是我们测试的简化版本。这个脚本在“夏天”运行得很好,但在“冬天”就失败了,从今天早上开始。通过手动把时钟调回去,可以重现这个失败。以下是脚本的输出:

[root@ana icalendar]# date 10250855
Sat Oct 25 08:55:00 CEST 2008
[root@ana icalendar]# python dst.py
DTSTART should represent datetime.datetime(2015, 4, 4, 8, 0, tzinfo=tzfile('/usr/share/zoneinfo/Europe/Brussels')) Brussels time
DTSTART should represent datetime.datetime(2015, 4, 4, 6, 0, tzinfo=<icalendar.prop.UTC object at 0x956b5cc>) UTC
DTSTART represents datetime.datetime(2015, 4, 4, 6, 0, tzinfo=<icalendar.prop.UTC object at 0x956b5cc>) Brussels time
[root@ana icalendar]# date 10260855
Sun Oct 26 08:55:00 CET 2008
[root@ana icalendar]# python dst.py
DTSTART should represent datetime.datetime(2015, 4, 4, 8, 0, tzinfo=tzfile('/usr/share/zoneinfo/Europe/Brussels')) Brussels time
DTSTART should represent datetime.datetime(2015, 4, 4, 6, 0, tzinfo=<icalendar.prop.UTC object at 0x96615cc>) UTC
DTSTART represents datetime.datetime(2015, 4, 4, 7, 0, tzinfo=<icalendar.prop.UTC object at 0x96615cc>) Brussels time
Traceback (most recent call last):
  File "dst.py", line 58, in <module>
    start.dt, startUTCExpected)
AssertionError: calendar's datetime.datetime(2015, 4, 4, 7, 0, tzinfo=<icalendar.prop.UTC object at 0x96615cc>) != expected datetime.datetime(2015, 4, 4, 6, 0, tzinfo=<icalendar.prop.UTC object at 0x96615cc>)

这是完整脚本

所以,有几个问题: - 为什么我当前的时间(以及我处于夏令时的哪个阶段)会影响时间戳的加载、保存和解析?我本来认为这不应该有影响。 - 如果这真的是个bug,你会怎么进行单元测试?显然,我不想让我的单元测试去重置我电脑上的时钟。

1 个回答

1

我没有看到你的代码(而且我现在也搞不懂你引用的测试运行脚本),但我注意到你试图获取一个与你所在时区不同的时间。
可以把夏令时(DST)想象成另一个时区,而不是简单的加减一小时。这可能会导致你在计算时间时出现多或少的小时数。
就像你在飞行时,你从一个地方出发,结果到达目的地的时间比你出发的时间还要早,这一切都是以当地时间来计算的。

撰写回答