sdispater / pendulum

Python datetimes made easy
https://pendulum.eustace.io
MIT License
6.28k stars 386 forks source link

fold value behaves differently for different timedelta addition values #853

Open Mcklmo opened 1 month ago

Mcklmo commented 1 month ago

Issue

The fold attribute behaves weirdly when adding different timedeltas to a datetime object. Adding minutes changes the fold from 0 to 1. Adding days keeps it at 1.

On the other hand, when setting fold to 0, adding minutes preserves it, and adding days changes it.

It seems like adding minutes forces the fold attribute to be 0 and days forces it to be 1.

import pendulum

year, month, day = 2024, 5, 1
dt = pendulum.datetime(year, month, day, tz="CET")

print(dt.fold)
print(dt.add(minutes=1).fold)
print(dt.add(days=1).fold)

"""Output:
1
0
1
"""

dt = pendulum.datetime(year, month, day, tz="CET").replace(fold=0)

print(dt.fold)
print(dt.add(minutes=1).fold)
print(dt.add(days=1).fold)

"""Output:
0
0
1
"""
jakobdo commented 1 month ago

I have a similar issue.

When working with pendulum and dst, I ran into this issue:

str(pendulum.parse("2024-10-27 02:00:00", tz="Europe/Copenhagen")) '2024-10-27 02:00:00+01:00' str(pendulum.parse("2024-10-27 02:00:00", tz="Europe/Copenhagen").subtract(minutes=15)) '2024-10-27 02:45:00+02:00' <-- Why does this end as 02:00 ? If "2024-10-27 02:00:00" = '2024-10-27 02:00:00+01:00' I would expect str(pendulum.parse("2024-10-27 02:00:00", tz="Europe/Copenhagen").subtract(minutes=15)) to be equal = '2024-10-27 01:45:00+01:00'

I can get the expected value using: str(pendulum.parse("2024-10-27 02:00:00", tz="Europe/Copenhagen").replace(fold=0).subtract(minutes=15)) '2024-10-27 01:45:00+02:00'

But I seems like a "stupid" hack.