jaraco / tempora

MIT License
20 stars 5 forks source link

Need timezone-aware command in scheduler #5

Closed jaraco closed 6 years ago

jaraco commented 6 years ago

tempora provides the scheduler.PeriodicCommandFixedDelay.daily_at, which will create an event at a point in time (timezone-aware), and then schedule follow up events at a fixed delay of 1 day after each. But across a DST boundary, 9am one day may be 10am 24 hours later (such as happened in the US this past Sunday).

The scheduler should offer an option to execute the command at a particular time of day, honoring timezone effects.

jaraco commented 6 years ago

I think I have a pretty straightforward fix. Relying on pytz's localize function and dateutils relativedelta:

>>> tz = pytz.timezone('US/Eastern')
>>> tz.localize(datetime.datetime(2018,3,10,9))
datetime.datetime(2018, 3, 10, 9, 0, tzinfo=<DstTzInfo 'US/Eastern' EST-1 day, 19:00:00 STD>)
>>> sat9am = tz.localize(datetime.datetime(2018,3,10,9))
>>> sun9am = tz.localize((sat9am + rd.relativedelta(days=1)).replace(tzinfo=None))
>>> sun9am
datetime.datetime(2018, 3, 11, 9, 0, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)
>>> print(sun9am - sat9am)
23:00:00

It may be possible to apply that technique to a function that dynamically returns a different 'delay' based on the relativedelta.