1

I am looking for a canonical way to discover the daylight savings status of a local timestamp that is missing timezone info.

I have a naive timestamp:

>>> datetime.datetime(2017, 3, 12, 5, 0)

I know that this timestamp was generated by a server at US/Pacific that is daylight savings aware -- so it could be at utc-0700 or utc-0800 depending on the time of year.

Is there an easy way to do this? Or do I need to use a hack like this:

from datetime import datetime, timedelta
from pytz import timezone

tz = timezone('US/Pacific')

# get all the transition times for the pacific timezone
ttimes = list(map(lambda x: x-timedelta(hours=8), tz._utc_transition_times))
summers = {}
for n, t in enumerate(ttimes):
    try:
        # find the pairs of transition times that start in the spring
        if t.month < 7:
            summers[t.year] = (ttimes[n], ttimes[n+1])
    except:
        pass

def is_daylight_savings(dt):
    return summers[dt.year][0] <= dt < summers[dt.year][1]

So:

>>> is_daylight_savings(datetime(2016, 3, 13, 5, 0))
True
>>> is_daylight_savings(datetime(2016, 3, 13, 1, 0))
False
zemekeneng
  • 1,660
  • 2
  • 15
  • 26
  • Based on the duplicate, it sounds like all you need is [localize](http://pytz.sourceforge.net/#localized-times-and-date-arithmetic) and [dst](https://docs.python.org/3/library/datetime.html#datetime.datetime.dst). – Scott Mermelstein Jun 07 '17 at 18:24

0 Answers0