Search code examples
pythonpytzpython-dateutilpython-datetime

Localize datetime (timezone aware) from timezone offset


I have a UTC timestamp and a timezone offset timestamp (both in milliseconds):

utc_time = 1394452800000
timezoneoffset = -14400000

If I wanted to get the datetime I would do :

print datetime.utcfromtimestamp(utc_time/1000)
>>>2014-03-10 12:00:00

How can I localize this datetime but also the final object be timezone aware?

If I divide timezoneoffset, -14400000/(3600*1000) = -4 (hours). So the final output should be:

>>>2014-03-10 08:00:00-04:00

My try:

from pytz import timezone
from dateutil.tz import tzoffset

utc_time = 1394452800000
timezoneoffset = -14400000

tzinfooff = tzoffset(None, timezoneoffset/1000)

print timezone(tzinfooff).localize( datetime.utcfromtimestamp(utc_time/1000) )

>>>Traceback (most recent call last):
  File "/Users/dionysis_lorentzos/Desktop/cmdline copy.py", line 25, in <module>
    print timezone(tzinfo2).localize( datetime.utcfromtimestamp(time/1000) ).isoformat()
  File "/usr/local/lib/python2.7/site-packages/pytz/__init__.py", line 162, in timezone
    if zone.upper() == 'UTC':
AttributeError: 'tzoffset' object has no attribute 'upper'

Solution

  • You need to use just the dateutil.tz.tzoffset() type; pytz.timezone only takes names, not dateutil.tz objects.

    The .localize() method is only needed for pytz-supplied timezones as they contain historic offsets as well, and they need to be applied to a datetime object using a little more care than just .replace() can do.

    If the timestamp is a UNIX epoch value in UTC, then use fromtimestap with the timezone as a second argument:

    >>> print datetime.fromtimestamp(utc_time/1000, tzinfooff)
    2014-03-10 08:00:00-04:00
    

    Or you could translate from UTC, using datetime.astimezone():

    >>> from dateutil.tz impor tzutc
    >>> dt_utc = datetime.utcfromtimestamp(utc_time/1000).replace(tzinfo=tzutc())
    >>> print dt_utc.astimezone(tzinfooff)
    2014-03-10 08:00:00-04:00