Specify dst_rule for ambiguous dates in pendulum.parse

301 Views Asked by At

I have to parse time-series data from CSV files in an application. The timestamps sometimes are in locale time with DST transitions. I want to use python-pendulum for the parsing of the date strings. Is it possible to specify the dst_rule when calling the pendulum.parse method? It seems to be ignored:

>>> 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'

I need the first string to be '2017-10-29T02:30:00+02:00' in my application. Is there a way to achieve this?

1

There are 1 best solutions below

0
On

I found a solution by instantiating the DateTime instance again after parsing:

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
)

Output now is '2017-10-29T02:30:00+02:00'