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.
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
foldattribute on datetime in Python 3.6 onward.The attribute, however, does not seem to be considered correctly, as shown by the following example:
Similarly, when DST ended but we’re still in fold…