Search code examples
datetimetimezonepytz

pytz adding extra minutes


What's the difference between US/Mountain and AZ timezone. Why is it adding an extra 28 min?

>>> strtime = datetime.datetime.strptime('10:00pm', '%I:%M%p')
>>> tz = timezone('US/Mountain').localize(strtime)
>>> print tz
1900-01-01 22:00:00-07:00
>>> tz = timezone(us.states.lookup('AZ').capital_tz).localize(strtime)
>>> print tz
1900-01-01 22:00:00-07:28 <<-----

Solution

  • this is most likely due to the fact that your year is 1900 (see also this question); it works fine if you add a current year:

    import datetime
    from pytz import timezone
    import us
    
    strtime = datetime.datetime.strptime('2020 10:00pm', '%Y %I:%M%p')
    tz = timezone('US/Mountain').localize(strtime)
    print(tz)
    # 2020-01-01 22:00:00-07:00
    tz = timezone(us.states.lookup('AZ').capital_tz).localize(strtime)
    print(tz)
    # 2020-01-01 22:00:00-07:00
    

    (I'm using Python3 but that shouldn't make a difference, I get the same 28 min offset for year 1900)