Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert UTC to EST with Python and take care of daylight saving automatically?

If I have a bunch of data with date & time in UTC format, how can I convert them to EST.

It can determine when they will be -4(in summer) and -5(in winter) automatically every year? Thanks

like image 334
saga Avatar asked Jan 24 '18 06:01

saga


People also ask

How do you convert UTC to DST?

Examples of how to convert UTC to your local time To convert 18:00 UTC (6:00 p.m.) into your local time, subtract 6 hours, to get 12 noon CST. During daylight saving (summer) time, you would only subtract 5 hours, so 18:00 UTC would convert to 1:00 p.m CDT. Note that the U.S. uses a 12-hour format with a.m. and p.m.

What is the function to offset the date for daylight saving Python?

You should use datetime. datetime. utcnow(). astimezone(tz) -- This gets the time in UTC and then offsets it from UTC according to whatever rules apply in the timezone tz.

How do I get the current time in Python est?

from datetime import datetime from pytz import timezone tz = timezone('EST') datetime. now(tz) ## this returns a datetime object pointing to right now ## according to the timezone info object handed in as the tz variable.


4 Answers

You'll need to use the pytz module (available from PyPI):

import pytz
from datetime import datetime

est = pytz.timezone('US/Eastern')
utc = pytz.utc
fmt = '%Y-%m-%d %H:%M:%S %Z%z'

winter = datetime(2016, 1, 24, 18, 0, 0, tzinfo=utc)
summer = datetime(2016, 7, 24, 18, 0, 0, tzinfo=utc)

print(winter.strftime(fmt))
print(summer.strftime(fmt))

print(winter.astimezone(est).strftime(fmt))
print(summer.astimezone(est).strftime(fmt))

which will print:

2016-01-24 18:00:00 UTC+0000
2016-07-24 18:00:00 UTC+0000
2016-01-24 13:00:00 EST-0500
2016-07-24 14:00:00 EDT-0400

The reason why you'll need to use 'US/Eastern' and not 'EST' is exemplified in the last two lines of output.

like image 54
thebjorn Avatar answered Oct 21 '22 16:10

thebjorn


If you have a pandas series with object datatype, you can first convert it into a DateTime series using pd.to_datetime()

df[col] = pd.to_datetime(your_series, format = '%Y-%m-%d %H:%M:%S', errors ='coerce')

Check if it is timezone aware or not by using series.dt.tz

df[col].dt.tz

If it's not timezone aware, we should make it timezone aware by using series.dt.tz_localize(). Also, do read about the ambiguous and nonexistent parameters of this function

df[col] = your_series[col].dt.tz_localize('UTC')

Now convert this series into the required timezone by series.dt.tz_convert()

df[col] = your_series[col].dt.tz_convert('US/Eastern')

The above method will take care of daylight savings time. If you want to check more timezones you can pip install pytz and

import pytz
pytz.common_timezones
like image 25
hasan najeeb Avatar answered Oct 21 '22 15:10

hasan najeeb


In case you just want the normalized hour offset for your existing timedelta shifting:

from datetime import datetime
import pytz

def curr_est_offset():
    tz_est = pytz.timezone('US/Eastern')
    offset = tz_est.utcoffset(datetime.utcnow())
    offset_seconds = (offset.days * 86400) + offset.seconds
    offset_hours = offset_seconds // 3600

    return offset_hours # -4 or -5
like image 43
mork Avatar answered Oct 21 '22 16:10

mork


As mentioned above, you can use pandas.DataFrame.tz_convert() like this:

import pandas as pd
from datetime import datetime

df = pd.read_csv("your_data_file_path.csv", index_col=False, engine='python')
df['Date'] = pd.to_datetime(df['Date'])
df['Date'] = df['Date'].dt.tz_localize('US/Eastern').dt.tz_convert('UTC')
df['Date'] = df['Date'].apply(lambda x: datetime.replace(x, tzinfo=None))

What the last row does is removing the timezone info from the datetime object, so you can operate with the date and time only (don't worry, that doesn't change the timezone again, it just strips it from the timestamp string).

like image 24
tsveti_iko Avatar answered Oct 21 '22 17:10

tsveti_iko