Pytz: Why do these different methods give different UTC offsets?

When creating an object datetimein a specific time zone using pytz, I get a different UTC offset depending on whether I use datetime.datetime()or datetime.datetime.now().

now()seems to give the correct UTC offset for the time zone, datetime()gives an offset that I don't recognize.

Why are they different? What is the offset value that it datetime()assigns?

Here is my code:

import datetime
import pytz

la_paz = pytz.timezone('America/La_Paz')

a = datetime.datetime.now(la_paz)
print a, a.utcoffset()

# 2011-03-22 05:30:13-04:00 -1 day, 20:00:00
# -4 hours is the correct UTC offset for La Paz

b = datetime.datetime(2011, 03, 22, 5, 30, tzinfo=la_paz)
print b, b.utcoffset()

# 2011-03-22 05:30:00-04:33 -1 day, 19:27:00
# What is the significance of -4:33?
+3
source share
2 answers

It seems that it datetime()will use the first recorded time zone for the region by default, and in many cases (for example, in La Paz) it is old and no longer valid.

datetime , :

b = la_paz.localize(datetime.datetime(2011, 03, 22, 5, 30))
print b, b.utcoffset()

now() .

+2

pytz :

. localize(), pytz. datetime (datetime ):

>>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
>>> print loc_dt.strftime(fmt)
2002-10-27 06:00:00 EST-0500

- astimezone():

>>> ams_dt = loc_dt.astimezone(amsterdam)
>>> ams_dt.strftime(fmt)
'2002-10-27 12:00:00 CET+0100'

:

b = datetime.datetime(2011, 03, 22, 5, 30, tzinfo=la_paz)

pytz

+2

All Articles