Convert datetime to local python
I have been reading quite some time answers and couldn't really drive into results.
I have the following code:
>>>from datetime import datetime >>>a = '2013-08-23T23:37:38+0000' >>>dt = datetime.strptime(a,'%Y-%m-%dT%H:%M:%S+0000') >>>print dt.date() 2013-08-23 >>>print dt.time() 23:37:38
What is the simplest way to output this result (assuming of course that a is unknown) given that we live in Central Europe. So it should be "daylight saving-proof" as well.
If it only needs default libraries it would be great.
Some more info
I dived into the libraries after my question. My above 3rd line should be better be: dt = datetime.strptime(a,'%Y-%m-%dT%H:%M:%S%z'). However a bug came out in 2.7.5 python (OS X if it matters) and had some trouble finding the %z. If you have trouble change version, if not ignore this. Of course the strptime() is just simpler level of dateutil.parser mentioned in the two answers so it can better be used instead of my code above.
you can use the useful dateutil package and the pytz one for exemple convert to paris timezone
from dateutil import parser import pytz FR = pytz.timezone('Europe/Paris') # there is the summer offset changing in this zone date = parser.parse("2013-08-23T23:37:38+0000") datefr = date.astimezone(FR)
from dateutil import parser import pytz date = parser.parse('2013-08-23T23:37:38+0000') CET = pytz.timezone('CET') date = date.astimezone(CET) print date.date() # prints 2013-08-24 print date.time() # prints 01:37:38