Skip to main content
 首页 » 编程设计

python之夏令时更改会影响保存和加载 icalendar 文件的结果吗

2025年05月04日9zhenyulu

我有一些单元测试在夏令时切换后今天开始失败。

我们正在使用 iCalendar python module加载和保存 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>) 

这里是 whole script .

所以,问题: - 为什么我的当前时间(以及我所在的 DST 的哪一部分)会影响时间戳的加载/保存/解析?我希望它不会。 - 如果它是一个错误,你将如何对这种错误进行单元测试?显然,我不希望我的单元测试重置计算机上的时钟。

请您参考如下方法:

不看你的代码(我的大脑现在无法理解引用的测试运行脚本) 我注意到你试图获得一个与你所在的时区不同的时间。 (将 DST 视为另一个时区,而不是当前时区的 +-1 小时)。 这可能(取决于你如何做)导致工作时间的增加或减少。 (就像你坐飞机的时候,你一次出发,然后在出发前到达你的位置,都是本地时间)