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
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.