How to convert EST / EDT to GMT?

I have several records inside a column that are EST or EDT Time. I need to convert these times to GMT. Time format:

10/1/2010 0:0:0 10/1/2010 0:6:0 ... 10/1/2010 23:54:0 ... 10/3/2010 0:0:0 ... 

Can anyone help me out here? thanks

+6
python gmt
source share
5 answers

The easiest and most reliable way that I know for converting between time zones is to use third-party pytz :

 import pytz import datetime as dt utc=pytz.utc eastern=pytz.timezone('US/Eastern') fmt='%Y-%m-%d %H:%M:%S %Z%z' text='''\ 10/1/2010 0:0:0 10/1/2010 0:6:0 10/1/2010 23:54:0 10/3/2010 0:0:0 ''' for datestring in text.splitlines(): date=dt.datetime.strptime(datestring,"%m/%d/%Y %H:%M:%S") date_eastern=eastern.localize(date,is_dst=None) date_utc=date_eastern.astimezone(utc) print(date_utc.strftime(fmt)) 

gives:

 2010-10-01 04:00:00 UTC+0000 2010-10-01 04:06:00 UTC+0000 2010-10-02 03:54:00 UTC+0000 2010-10-03 04:00:00 UTC+0000 

Please note, however, that your data does not indicate whether the datetime is in the EST or EDT time zone. There are several times that are ambiguous unless you specify EST or EDT. For example, "10/27/2002 1:30:00" would be ambiguous:

 >>> eastern.localize(datetime(2002, 10, 27, 1, 30, 00), is_dst=None) AmbiguousTimeError: 2002-10-27 01:30:00 

since this time happened twice due to daylight saving time. Also, some dates, such as 2002-04-07 02:30:00, do not exist. See this link for a discussion of these and even more bizarre issues that arise when working with local events.

If you are ready to overlook these nodal corner cases, and if your computer is configured in the local time zone (for example, EST / EDT), there is a way to convert between local and UTC time clocks, which does not require the installation of pytz . The idea is to convert date-time -> timetuple -> timestamp -> UTC datetime. The conversion chain is performed using

 dt.datetime.utcfromtimestamp(time.mktime(date.timetuple())) 

For instance:

 import time import datetime as dt import pytz utc=pytz.utc eastern=pytz.timezone('US/Eastern') fmt='%Y-%m-%d %H:%M:%S %Z%z' text='''\ 10/1/2010 0:0:0 10/1/2010 0:6:0 10/1/2010 23:54:0 10/3/2010 0:0:0 3/13/2011 1:55:0 3/13/2011 3:00:0 ''' for datestring in text.splitlines(): date=dt.datetime.strptime(datestring,"%m/%d/%Y %H:%M:%S") date_est=eastern.localize(date,is_dst=None) date_utc=date_est.astimezone(utc) date_utc2=dt.datetime.utcfromtimestamp(time.mktime(date.timetuple())) print('{d} --> {d_utc} {d_utc2}'.format( d=date.strftime(fmt), d_utc=date_utc.strftime(fmt), d_utc2=date_utc2.strftime(fmt), )) assert date_utc.hour == date_utc2.hour 

gives

 2010-10-01 00:00:00 EDT-0400 --> 2010-10-01 04:00:00 UTC+0000 2010-10-01 04:00:00 2010-10-01 00:06:00 EDT-0400 --> 2010-10-01 04:06:00 UTC+0000 2010-10-01 04:06:00 2010-10-01 23:54:00 EDT-0400 --> 2010-10-02 03:54:00 UTC+0000 2010-10-02 03:54:00 2010-10-03 00:00:00 EDT-0400 --> 2010-10-03 04:00:00 UTC+0000 2010-10-03 04:00:00 2011-03-13 01:55:00 EST-0500 --> 2011-03-13 06:55:00 UTC+0000 2011-03-13 06:55:00 2011-03-13 03:00:00 EDT-0400 --> 2011-03-13 07:00:00 UTC+0000 2011-03-13 07:00:00 

The last two dates above show that the conversion works correctly even at moments close to the switch between EST and EDT.


In conclusion, using an alternative method (without pytz), here's how to convert datetime objects representing local time to datetime objects representing GMT time and vice versa:

 In [83]: import datetime as dt In [84]: import time In [85]: import calendar In [86]: date=dt.datetime(2010,12,1,0,0,0) In [87]: date Out[87]: datetime.datetime(2010, 12, 1, 0, 0) In [88]: date_utc=dt.datetime.utcfromtimestamp(time.mktime(date.timetuple())) In [89]: date_utc Out[89]: datetime.datetime(2010, 12, 1, 5, 0) In [90]: date_local=dt.datetime.fromtimestamp(calendar.timegm(date_utc.timetuple())) In [91]: date_local Out[91]: datetime.datetime(2010, 12, 1, 0, 0) 
+11
source share

Pseudocode for each entry:

enter the timestamp line: field [0] .strip () + "" + field [1] .strip ()

use datetime.datetime.strptime () to convert this to an instance of datetime.datetime

add timedelta for example. timedelta (hours = -4) per timestamp

use timestamp.strftime () to create any string representation you want for output.

In case the time field is empty: If it means 0: 0: 0, change the above value. If it means "unknown time", you will need to do something else ...

+2
source share

Without an associated time, the time zone does not matter ... and the date cannot be transferred to another time zone. Is there any related time in another column?

EDIT: Well, now that there is time, I will let the Python guru take over .;]

+1
source share

I had to create a custom function in Python to convert EST to GMT, here is the code I wrote:

 #convert est time to gmt. Make sure you assign the current EST values #to the following variables est_year est_month est_day est_hour est_min gmt_year = est_year gmt_month = est_month gmt_day = est_day gmt_hour = est_hour + 5 #gmt is ahead by 5 hrs gmt_min = est_min if gmt_hour > 23: gmt_hour = gmt_hour - 23 gmt_day = est_day + 1 days_in_month = calendar.monthrange(est_year,est_month)[1] #in case the no days becomes 32.. if gmt_day > days_in_month: gmt_day = 1 gmt_month = gmt_month + 1 if gmt_month > 12: gmt_month = 1 gmt_year = gmt_year + 1 gmttime = datetime.datetime(gmt_year, gmt_month, gmt_day, gmt_hour, gmt_min, 0) 

I have not added EDT support. Now in February and est. Any changes or corrections are welcome!

+1
source share

Suppose we have a date and time string like "2019-04-09T23: 59: 55ET" in US / Eastern time. Here is the function to convert a string to UTC:

 from datetime import datetime import pytz eastern = pytz.timezone('US/Eastern') def convent_est_to_utc(datetime_str): dt = datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%SET') return dt.replace(tzinfo=eastern).astimezone(pytz.utc) # testing convent_est_to_utc("2019-04-09T23:59:55ET") # The result: 2019-04-10 04:55:55+00:00 
0
source share

All Articles