在pendulum.parse中为不明确的日期指定dst_rule

时间:2018-05-31 09:34:29

标签: python dst pendulum

我必须解析应用程序中CSV文件的时间序列数据。 时间戳有时是在DST转换的区域设置时间内。 我想使用python-pendulum来解析日期字符串。 调用dst_rule方法时是否可以指定pendulum.parse?它似乎被忽略了:

>>> str(pendulum.parse('2017-10-29 02:30:00', tz='Europe/Berlin', dst_rule=pendulum.PRE_TRANSITION))
'2017-10-29T02:30:00+01:00'
>>> str(pendulum.parse('2017-10-29 02:30:00', tz='Europe/Berlin', dst_rule=pendulum.POST_TRANSITION))
'2017-10-29T02:30:00+01:00'

我需要在我的应用程序中将第一个字符串设置为“2017-10-29T02:30:00 + 02 :00”。 有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:0)

我通过解析后再次实例化DateTime实例找到了解决方案:

dt = pendulum.parse('2017-10-29 02:30:00', tz='Europe/Berlin')
dt = pendulum.datetime(
    dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.microsecond,
    tz='Europe/Berlin', dst_rule=pendulum.PRE_TRANSITION
)

现在输出为'2017-10-29T02:30:00+02:00'