Search code examples
python-3.xdatetimedate-conversionpython-dateutil

Python timezone conversion adding minutes to the hour?


So I'm trying to convert a bunch of hours (10:00:00, 14:00:00, etc) from a given timezone to UTC.

When I do so, I keep maddeningly getting things back like "15:51:00".

When you get to that line, and print what value it's using, it's using something like:

1900-01-01 12:00:00-05:51

Which is fine, except for the -05:51 bit. I have no idea why that -05:51 is there, and it's causing me problems. UTC conversion is hour to hour, yes? I think it's got something to do with my timezone conversions, but I really don't get why they would be doing that.

Here's a minimal example that has the same erroneous output; it returns 15:51:00 when it should just return a flat hour, no minutes.

import datetime
from dateutil import tz

jj = datetime.datetime.strptime("10:00:00", "%H:%M:%S")

tzz = tz.gettz('US/Central')

def constructstring(tzz,x):
    inter2 = x.replace(tzinfo=tzz) #ERROR HAPPENS HERE (I'm pretty sure anyways)
    inter3 = inter2.astimezone(tz.tzutc())
    return inter3

print(constructstring(tzz,jj).strftime("%H:%M:%S"))

Solution

  • You are not specifying a date when you create the jj datetime object, so the default date of 1900-01-01 is used. Timezones are not fixed entities; they change over time, and the US/Central timezone used a different offset back in 1900.

    At the very least, use a recent date, like today for example:

    # use today's date, with the time from jj, and a given timezone.
    datetime.datetime.combine(datetime.date.today(), jj.time(), tzinfo=tzz)
    

    If all you need is a time, then don't create datetime objects to store those; the datetime module has a dedicated time() object. I'd also not use strftime() to create objects from literals. Just use the constructor to pass in integers:

    jj = datetime.time(10, 0, 0)  # or just .time(10)
    

    Other good rules of thumb: If you have to deal with dates with timezones, try to move those to datetime objects in UTC the moment your code receives or loads them. If you only have a time of day, but still need timezone support, attach them to today's date, so you get the right timezone. Only convert to strings again as late as possible.