Search code examples
pythondatetimepython-datetimepytzpython-dateutil

datetime ignores tzinfo?


I'm probably misunderstanding the purpose of tzinfo, but I have a datetime object dt I managed to get into this form:

datetime.datetime(2017, 7, 2, 20, 0, tzinfo=tzoffset('PDT', -7))

I'm trying to represent the above date from July 2nd 2017 20:00 PDT.

Now, I'd like to convert that time to UTC, but when I do so, it outputs the UTC timestamp for July 2nd 2017 20:00 UTC, it doesn't apply the 7 hour difference.

For example:

>>> dt.timestamp()
1499025607.0

Which is : Sunday, July 2, 2017 8:00:07 PM

Also

>>> dt.isoformat()
'2017-07-02T20:00:00-00:00:07'

I've tried

>>> dt.astimezone(pytz.UTC).timestamp()
1499025607.0

Notice that's the same timestamp as dt.timestamp()


Solution

  • According to the dateutil docs, your parameter to the tzoffset() function is wrong.

    tzinfo=dateutil.tz.tzoffset('PDT', -7)
    

    creates a timezone with an offset of 7 seconds.

    tzinfo=dateutil.tz.tzoffset('PDT', -7*60*60)
    

    creates a timezone with an offset of 7 hours.