pythondatetimetimezoneepochpytz

Convert Local time to UTC before 1970


I'm trying to get UTC times for dates before 1970 but tz and tzinfo in Python only contain timezone databases for dates post-1970.

If I'm trying to find the UTC time for a date before 1970, say, Bill Clinton's birthday:

August 16, 1946 8:51AM Hope, AK

datetime and tzinfo will return 16 Aug 1946 13:51 UTC

But it turns out that that is incorrect because it uses Chicago's timezone (which had its own timezone)

How can I find the proper time conversion and trust it for dates before 1970 in Python?


Solution

  • Time zone rules as we know them today came into play around 1900, see e.g. P. Eggert's tz repo. So Bill Clinton's birthday should not suffer from missing tz rules.

    Ex, using pytz (deprecated):

    from datetime import datetime
    import pytz
    
    bday = "August 16, 1946 8:51AM Hope, AK"
    # Hope, AK is not a time zone, so strip it
    bday = bday.rstrip(" Hope, AK")
    
    # Hope in Arkansas uses US Central time, so we can use
    tz = pytz.timezone("America/Chicago")
    
    bday_dt = tz.localize(datetime.strptime(bday, "%B %d, %Y %I:%M%p"))
    
    print(bday_dt)
    # 1946-08-16 08:51:00-05:00
    print(bday_dt.astimezone(pytz.UTC))
    # 1946-08-16 13:51:00+00:00
    

    ...or using Python 3.9's zoneinfo

    from zoneinfo import ZoneInfo
    
    bday_dt = datetime.strptime(bday, "%B %d, %Y %I:%M%p").replace(tzinfo=ZoneInfo("America/Chicago"))
    
    print(bday_dt)
    # 1946-08-16 08:51:00-05:00
    print(bday_dt.astimezone(ZoneInfo("UTC")))
    # 1946-08-16 13:51:00+00:00