Convert UTC datetime string to local datetime Convert UTC datetime string to local datetime python python

Convert UTC datetime string to local datetime


If you don't want to provide your own tzinfo objects, check out the python-dateutil library. It provides tzinfo implementations on top of a zoneinfo (Olson) database such that you can refer to time zone rules by a somewhat canonical name.

from datetime import datetimefrom dateutil import tz# METHOD 1: Hardcode zones:from_zone = tz.gettz('UTC')to_zone = tz.gettz('America/New_York')# METHOD 2: Auto-detect zones:from_zone = tz.tzutc()to_zone = tz.tzlocal()# utc = datetime.utcnow()utc = datetime.strptime('2011-01-21 02:37:21', '%Y-%m-%d %H:%M:%S')# Tell the datetime object that it's in UTC time zone since # datetime objects are 'naive' by defaultutc = utc.replace(tzinfo=from_zone)# Convert time zonecentral = utc.astimezone(to_zone)

Edit Expanded example to show strptime usage

Edit 2 Fixed API usage to show better entry point method

Edit 3 Included auto-detect methods for timezones (Yarin)


Here's a resilient method that doesn't depend on any external libraries:

from datetime import datetimeimport timedef datetime_from_utc_to_local(utc_datetime):    now_timestamp = time.time()    offset = datetime.fromtimestamp(now_timestamp) - datetime.utcfromtimestamp(now_timestamp)    return utc_datetime + offset

This avoids the timing issues in DelboyJay's example. And the lesser timing issues in Erik van Oosten's amendment.

As an interesting footnote, the timezone offset computed above can differ from the following seemingly equivalent expression, probably due to daylight savings rule changes:

offset = datetime.fromtimestamp(0) - datetime.utcfromtimestamp(0) # NO!

Update: This snippet has the weakness of using the UTC offset of the present time, which may differ from the UTC offset of the input datetime. See comments on this answer for another solution.

To get around the different times, grab the epoch time from the time passed in. Here's what I do:

def utc2local(utc):    epoch = time.mktime(utc.timetuple())    offset = datetime.fromtimestamp(epoch) - datetime.utcfromtimestamp(epoch)    return utc + offset


See the datetime documentation on tzinfo objects. You have to implement the timezones you want to support yourself. The are examples at the bottom of the documentation.

Here's a simple example:

from datetime import datetime,tzinfo,timedeltaclass Zone(tzinfo):    def __init__(self,offset,isdst,name):        self.offset = offset        self.isdst = isdst        self.name = name    def utcoffset(self, dt):        return timedelta(hours=self.offset) + self.dst(dt)    def dst(self, dt):            return timedelta(hours=1) if self.isdst else timedelta(0)    def tzname(self,dt):         return self.nameGMT = Zone(0,False,'GMT')EST = Zone(-5,False,'EST')print datetime.utcnow().strftime('%m/%d/%Y %H:%M:%S %Z')print datetime.now(GMT).strftime('%m/%d/%Y %H:%M:%S %Z')print datetime.now(EST).strftime('%m/%d/%Y %H:%M:%S %Z')t = datetime.strptime('2011-01-21 02:37:21','%Y-%m-%d %H:%M:%S')t = t.replace(tzinfo=GMT)print tprint t.astimezone(EST)

Output

01/22/2011 21:52:09 01/22/2011 21:52:09 GMT01/22/2011 16:52:09 EST2011-01-21 02:37:21+00:002011-01-20 21:37:21-05:00a


matomo