Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folding time when transitioning out of DST is not considered #56

Closed
uranusjr opened this issue Dec 3, 2023 · 1 comment
Closed

Folding time when transitioning out of DST is not considered #56

uranusjr opened this issue Dec 3, 2023 · 1 comment

Comments

@uranusjr
Copy link

uranusjr commented Dec 3, 2023

When a DST period exits, usually the wall clock time would “fold” because it is turned back, causing a period to “happen” twice. For example, for 2023 in Switzerland, DST ends (from offset +2 to +1) on 29 Oct at wall clock time 3am, so the [2:00, 3:00) period of the day would occur twice before 3am is reached. This is indicated by the fold attribute on datetime in Python 3.6 onward.

The attribute, however, does not seem to be considered correctly, as shown by the following example:

from datetime import datetime
from zoneinfo import ZoneInfo
from croniter import croniter

it = croniter("*/5 * * * *")  # Every 5 minutes

d1 = datetime(2023, 10, 29, 2, 55, fold=0, tzinfo=ZoneInfo("Europe/Zurich"))
print(d1)  # 2023-10-29 02:55:00+02:00

it.set_current(d1)
d2 = it.get_next(datetime)

print(d2)  # 2023-10-29 03:00:00+01:00
# Wrong! We were in the non-folded 2:55, the next run should be folded to 2:00+01.

print((d2.timestamp() - d1.timestamp()) / 60)  # 65.0, an entire hour is skipped.

Similarly, when DST ended but we’re still in fold…

d3 = datetime(2023, 10, 29, 2, 30, fold=1, tzinfo=ZoneInfo("Europe/Zurich"))
print(d3)  # 2023-10-29 02:30:00+01:00

it.set_current(d3)
d4 = it.get_next(datetime)

print(d4)  # 2023-10-29 02:35:00+02:00
# We already exited DST, now this takes us back in it!

print((d4.timestamp() - d3.timestamp()) / 60)  # -55.0, we travelled back in time.
@kiorky
Copy link
Owner

kiorky commented Dec 9, 2023

duplicates #1

@kiorky kiorky closed this as completed Dec 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants