Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting mountain standard time to eastern standard time in python

I have a time datetime object.. which has both date and time..

so for example

    d = (2011,11,1,8,11,22)  (24 hour time time format)

But this time stamp is in mountain standard time.. (Arizona. phoenix)

Now I want to convert this time in EST...

Now that is just the time delta adjustment..

But then there is this daylight saving issues as well.

I was wondering if there is an inbuilt method to take care of daylight saving for adjusting time zones..

like image 483
frazman Avatar asked Nov 08 '12 18:11

frazman


2 Answers

Use pytz for timezone conversions. pytz takes daylight savings in consideration check this. U need a helper function like:

def convert(dte, fromZone, toZone):
    fromZone, toZone = pytz.timezone(fromZone), pytz.timezone(toZone)
    return fromZone.localize(dte, is_dst=True).astimezone(toZone)
like image 191
Emmanuel N Avatar answered Sep 30 '22 09:09

Emmanuel N


The library you're looking for is pytz, specifically the localize() method.

Pytz isn't in the standard library but you can get it with pip or easy_install.

like image 21
Philip Avatar answered Sep 30 '22 10:09

Philip