https://prefect.io logo
#prefect-community
Title
# prefect-community
m

Mikkel Duif

05/15/2022, 11:07 AM
Got a questions in regards to handling DST correctly. If i specify the anchor_date in winther time, it will be offset by 1 hour. is there a way to handle this correctly?
Copy code
import asyncio
import pendulum
from datetime import timedelta
from prefect.orion.schemas.schedules import IntervalSchedule

winter_schedule = IntervalSchedule(
   interval=timedelta(hours=24),
   anchor_date=pendulum.datetime(2022, 1, 1, 0, 30, 0, tz="Europe/Copenhagen")
)

summer_schedule = IntervalSchedule(
   interval=timedelta(hours=24),
   anchor_date=pendulum.datetime(2022, 4, 1, 0, 30, 0, tz="Europe/Copenhagen")
)


print(asyncio.run(winter_schedule.get_dates(1))[0])
print(asyncio.run(summer_schedule.get_dates(1))[0])

>>> "2022-05-16T01:30:00+02:00"
>>> "2022-05-16T00:30:00+02:00"
1
a

Anna Geller

05/15/2022, 11:56 AM
Interesting question! Generally, with `IntervalSchedule`: • intervals >= 24h will automatically adjust based on DST in the time zone you specified e.g. daily schedule at 1 AM will always run at 1 AM in this time zone already DST-adjusted • intervals < 24h follow UTC convention e.g. hourly schedule will fire every hour even if technically 2 runs could have been scheduled for the same hour when clocks are reset Does it answer your question? Maybe you can use UTC time if this is too confusing?
what you see in this call is already adjusted time based on our current CET time, which is currently 2 hours apart from UTC, while in winter this will be just 1 hour apart when you execute it then
m

Mikkel Duif

05/15/2022, 12:03 PM
what i want is that it always fires at e.g. 0:30am in copenhagen time, regardless of DST. so i want it to automatically adjust for DST. But from what i understand from these results, it does not adjust itself.
a

Anna Geller

05/15/2022, 12:06 PM
Maybe you can use CronSchedule then?
Copy code
schedule = CronSchedule(cron="30 0 * * *", timezone="Europe/Copenhagen")
Copy code
print(asyncio.run(schedule.get_dates(10, start=pendulum.datetime(2022, 1, 1, 0, 30, 0, tz="Europe/Copenhagen"))))
works exactly as you wanted:
Copy code
Cron's rules for DST are based on schedule times, not intervals. This means that an hourly cron schedule will fire on every new
    schedule hour, not every elapsed hour; for example, when clocks are set back
    this will result in a two-hour pause as the schedule will fire *the first
    time* 1am is reached and *the first time* 2am is reached, 120 minutes later.
    Longer schedules, such as one that fires at 9am every morning, will
    automatically adjust for DST.
m

Mikkel Duif

05/15/2022, 12:13 PM
yes, cronschedule indeed seems to work. thanks. so is this a bug with intervalschedule or does it work as intended?
a

Anna Geller

05/15/2022, 12:15 PM
I'd say it works as intended because it adjusts to the time zone and DST automatically based on elapsed interval, but I'll open an issue to investigate and be 100% sure
@Marvin open "Validate whether DST is handled correctly in the
IntervalSchedule
in Prefect 2.0"
5 Views