When working with Python time-zones that utilize daylight-savings time, what tzinfo should be passed?

Viewed 433

I'm trying to convert datestrings to datetimes, but cannot find any reliable documentation involving which timezone codes map to which timezones, in particular I am dealing with timezones that utilize daylight savings time.

I have 4 potential timezone codes, and I am trying to determine what to pass to tz.gettz for the daylight savings ones.

import dateutil.parser as p
import dateutil.tz as tz

tzinfos = {"CST": tz.gettz("America/Chicago"),
           "CDT": tz.gettz("?"),                # HERE
           "EST": tz.gettz("America/Eastern"),
           "EDT": tz.gettz("?")                 # HERE
           }

date_strings = ["Wed Mar 03 09:44:59 CST 2021",
                "Wed Mar 03 09:44:59 CDT 2021",
                "Wed Mar 03 09:44:59 EST 2021",
                "Wed Mar 03 09:44:59 EDT 2021"]

for s in date_strings:
    print(p.parse(s, tzinfos=tzinfos))

The datestrings are coming in via an API provided by my states department of transportation which gets the data from sensors built into the state highways, and the end-goal is to convert them all to UTC time, but I have no way of knowing what their UTC times should be, so I can't just guess the timezones and then verify them.

I've been going through this wikipedia page of timezones and it appears as if CDT for example, would be the same as CST, being tz.gettz("America/Chicago") but the application I am dealing with will be used to dispatch first responders to traffic accidents, so it's vital that the time conversion is handled correctly. I'm hoping to find documentation regarding what should be passed to the tz.gettz() function?

1 Answers

Take a look at the documentation of dateutil.tz:

help(dateutil.tz)
...
     |  Using the US Eastern time zone as an example, we can see that a ``tzfile``
     |  provides time zone information for the standard Daylight Saving offsets:
     |  
     |  .. testsetup:: tzfile
     |  
     |      from dateutil.tz import gettz
     |      from datetime import datetime
     |  
     |  .. doctest:: tzfile
     |  
     |      >>> NYC = gettz('America/New_York')
     |      >>> NYC
     |      tzfile('/usr/share/zoneinfo/America/New_York')
     |  
     |      >>> print(datetime(2016, 1, 3, tzinfo=NYC))     # EST
     |      2016-01-03 00:00:00-05:00
     |  
     |      >>> print(datetime(2016, 7, 7, tzinfo=NYC))     # EDT
     |      2016-07-07 00:00:00-04:00
...

Time zones account for daylight savings time offsets according to the date, automatically. So your tzinfos should be:

tzinfos = {"CST": tz.gettz("America/Chicago"),
           "CDT": tz.gettz("America/Chicago"),                
           "EST": tz.gettz("America/Eastern"),
           "EDT": tz.gettz("America/Eastern")                 
           }

Really take the time to read the entire documentation, it's pretty neat. The "America/New_York" time zone even accounts for the year-round daylight savings time during WWII.

Related