Search code examples
pythondatetimepytzpython-babel

Why 1 hour wrong?


The time is 15:0x in Stockholm (Europe). But I'm getting the localized time 1 hour wrong:

>>> datetime.datetime.now()
datetime.datetime(2015, 12, 17, 15, 2, 42, 633000)
>>> babel.dates.format_datetime(datetime.datetime.now(), 'full', tzinfo=pytz.timezone('Europe/Stockholm'),locale='en')
u'Thursday, December 17, 2015 at 4:02:49 PM Central European Standard Time'
>>>

What is the mistake?


Solution

  • The problem appears to have been the baked in local time of the datetime.datetime.now() call. As mentioned in the comments, use datetime.datetime.utcnow() for a timezone agnostic coordinated universal time, or input the desired timezone info into the datetime call datetime.datetime.now(tz=pytz.timezone('Europe/Stockholm'))