Converting localized string date representation to UTC in python -


how convert following localized datetime string utc datetime. string has date, time , timezone mentioned in e.g. may 15,2015, 04.24am ist, here ist indian standard time. can time zone. tried using pytz couldn't make work.

the thing it's quite difficult parse string abbreviated timezone information. but, if know timezone, can it's name recognized pytz. can list timezone names pytz.all_timezones.

in case 'asia/calcutta' , should work convert utc. strip timezone information string , add later:

import pytz import datetime  # string without timezone info str = "may 15, 2015, 04.24am" # parse naive time dt = datetime.datetime.strptime(str, "%b %d, %y, %i.%m%p") # localize asia/calcutta dt_localized = pytz.timezone('asia/calcutta').localize(dt) # convert utc dt_utc = dt_localized.astimezone(pytz.timezone('utc')) 

and get:

>>> dt datetime.datetime(2015, 5, 15, 4, 24)  >>> dt_localized datetime.datetime(2015, 5, 15, 4, 24, tzinfo=<dsttzinfo 'asia/calcutta' ist+5:30:00 std>)  >>> dt_utc datetime.datetime(2015, 5, 14, 22, 54, tzinfo=<utc>) 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

php - Find a regex to take part of Email -

javascript - Function overwritting -