How to convert UTC-4 to US/Eastern in python?

I read time stamps from text file. These time stamps are in UTC-4. I need to convert them to US/Eastern.

import datetime datetime_utc4 = datetime.datetime.strptime("12/31/2012 16:15", "%m/%d/%Y %H:%M") 

How do I convert it to US/Eastern? One-line answer would be best.

Note: my original question stated EST to EDT. But it does not change the essence of the question, which is how to go from one time zone to another. Upon some reading (following comments) I gather that python (pytz in particular) does not treat EST and EDT as separate time zones, rather as two flavors of US/Eastern. But this is an implementation detail. It is common to refer to EST and EDT as two different time zones, see e.g. here.

5

1 Answer

Based on your update and comments, I now understand that you have data that is fixed at UTC-4 and you want to correct this so that it is valid in US Eastern Time, including both EST/EDT where appropriate. Here is how you do that with pytz.

from datetime import datetime import pytz dt = datetime.strptime("12/31/2012 16:15", "%m/%d/%Y %H:%M") \ .replace(tzinfo = pytz.FixedOffset(-240)) \ .astimezone(pytz.timezone('America/New_York')) 

Note that I used the America/New_York time zone id. This is the most correct form of identifier. You could instead use US/Eastern and it would work just fine, but be aware that this is an alias, and it is just there for backwards compatibility.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like