Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26642

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?

Upvotes: 3

Views: 2804

Answers (2)

Mike Müller
Mike Müller

Reputation: 85532

Explicit is better than implicit. Set the timezone for now:

now = datetime.datetime.now(tz=pytz.timezone('Europe/Stockholm'))

Upvotes: 5

Busturdust
Busturdust

Reputation: 2495

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'))

Upvotes: 3

Related Questions