Question

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'
Was it helpful?

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

OTHER TIPS

To convert POSIX timestamp to an timezone-aware datetime object in a timezone specified by its utc offset, you could create an aware utc datetime object and convert it to the fixed offset timezone:

from datetime import datetime

utc_dt = datetime.fromtimestamp(utc_time * 1e-3, utc)
dt = utc_dt.astimezone(FixedOffset(timezoneoffset * 1e-3 / 60, None))

print(dt.isoformat(' '))
# -> 2014-03-10 08:00:00-04:00

where utc and FixedOffset are defined in datetime docs:

from datetime import tzinfo, timedelta

class FixedOffset(tzinfo):
    """Fixed offset in minutes east from UTC."""

    def __init__(self, offset, name):
        self.__offset = timedelta(minutes = offset)
        self.__name = name

    def utcoffset(self, dt):
        return self.__offset

    def tzname(self, dt):
        return self.__name

    def dst(self, dt):
        return timedelta(0)

utc = FixedOffset(0, "UTC")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top