How to convert GMT time to EST time using Python

47,650

Solution 1

Time zones aren't built into standard Python - you need to use another library. pytz is a good choice.

>>> gmt = pytz.timezone('GMT')
>>> eastern = pytz.timezone('US/Eastern')
>>> time = "Tue, 12 Jun 2012 14:03:10 GMT"
>>> date = datetime.datetime.strptime(time, '%a, %d %b %Y %H:%M:%S GMT')
>>> date
datetime.datetime(2012, 6, 12, 14, 3, 10)
>>> dategmt = gmt.localize(date)
>>> dategmt
datetime.datetime(2012, 6, 12, 14, 3, 10, tzinfo=<StaticTzInfo 'GMT'>)
>>> dateeastern = dategmt.astimezone(eastern)
>>> dateeastern
datetime.datetime(2012, 6, 12, 10, 3, 10, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)

Solution 2

Using pytz

from datetime import datetime
from pytz import timezone
fmt = "%Y-%m-%d %H:%M:%S %Z%z"
now_time = datetime.now(timezone('US/Eastern'))
print now_time.strftime(fmt)

Solution 3

With Python 3.9, time zone handling is built into the standard lib with the zoneinfo module (for older Python versions, use zoneinfo via backports.zoneinfo).

Ex:

from zoneinfo import ZoneInfo
from datetime import datetime, timezone

time = "Tue, 12 Jun 2012 14:03:10 GMT"

# parse to datetime, using %Z for the time zone abbreviation
dtobj = datetime.strptime(time, '%a, %d %b %Y %H:%M:%S %Z')

# note that "GMT" (=UTC) is ignored:
# datetime.datetime(2012, 6, 12, 14, 3, 10)

# ...so let's correct that:
dtobj = dtobj.replace(tzinfo=timezone.utc)
# datetime.datetime(2012, 6, 12, 14, 3, 10, tzinfo=datetime.timezone.utc)

# convert to US/Eastern (EST or EDT, depending on time of the year)
dtobj = dtobj.astimezone(ZoneInfo('US/Eastern'))
# datetime.datetime(2012, 6, 12, 10, 3, 10, tzinfo=zoneinfo.ZoneInfo(key='US/Eastern'))
print(dtobj)
# 2012-06-12 10:03:10-04:00

And there's also dateutil, which follows the same semantics:

import dateutil
# no strptime needed...
# correctly localizes to GMT (=UTC) directly
dtobj = dateutil.parser.parse(time) 

dtobj = dtobj.astimezone(dateutil.tz.gettz('US/Eastern'))
# datetime.datetime(2012, 6, 12, 10, 3, 10, tzinfo=tzfile('US/Eastern'))
print(dtobj)
# 2012-06-12 10:03:10-04:00
Share:
47,650
FObersteiner
Author by

FObersteiner

Research Engineer at KIT. Engineering experimental atmospheric research, from Automation and Data Processing to Zener diodes (if you force me...). Passion for coding, learning, and helping others. Will this software never run on a space ship that is orbiting a black hole?

Updated on July 31, 2022

Comments

  • FObersteiner
    FObersteiner over 1 year

    I want convert GMT time to EST time and get a timestamp. I tried the following but don't know how to set time zone.

    time = "Tue, 12 Jun 2012 14:03:10 GMT"
    timestamp2 = time.mktime(time.strptime(time, '%a, %d %b %Y %H:%M:%S GMT'))
    
    • Wooble
      Wooble almost 12 years
      There's no way that the code you posted runs at all, since you named a string time and then tried to use a function from the time package which is no longer accessible at that name.
  • Mark Ransom
    Mark Ransom over 2 years
    Thank you for mentioning backports.zoneinfo, because of my choice of OS I'm stuck at Python 3.8.