Rastio Rastio - 1 month ago 9
Python Question

Converting local time to UTC using pytz adds DST?

>>> t = datetime.datetime(2016, 11, 27, 14, 46, 0, 0)
tz = pytz.timezone('America/Vancouver')
utc = tz.localize(t).astimezone(pytz.utc)
now = datetime.datetime.utcnow()

>>> print t, tz, utc, now
2016-11-27 14:46:00 America/Vancouver 2016-11-27 22:46:00+00:00 2016-10-27 21:49:33.723605


Why is utc == 2016-11-27 22:46:00+00:00
instead of 2016-11-27 21:46:00+00:00

Thanks

Answer

Well, that's because Vancouver observes daylight savings time (See this)

Between March 13th 2016 and November 6th, Vancouver is UTC-7. After November 6, it's UTC-8. So 2:46 PM (14:46) today (October 27th 2016) still falls in the DST part of the timezone, and that would be 14 + 7 = 21 (9:46 PM) in UTC.

However, in November 27th (the date you're converting) Vancouver is already back to the "regular" (non DST) time, UTC-8, therefore, 14:46 PM in Vancouver on November 27th 2016 is 14 + 8 = 22 (10:46 PM). As a matter of fact, it'll have been like that for any date after November 6th.