Skip to content Skip to sidebar Skip to footer

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 : pri

Solution 1:

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

Solution 2:

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

classFixedOffset(tzinfo):
    """Fixed offset in minutes east from UTC."""def__init__(self, offset, name):
        self.__offset = timedelta(minutes = offset)
        self.__name = name

    defutcoffset(self, dt):
        return self.__offset

    deftzname(self, dt):
        return self.__name

    defdst(self, dt):
        return timedelta(0)

utc = FixedOffset(0, "UTC")

Post a Comment for "Localize Datetime (timezone Aware) From Timezone Offset"