Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 444
Rastio Avatar asked Oct 27 '16 21:10

Rastio


People also ask

Does pytz handle DST?

pytz will help you to tell if an date is under DST influence by checking dst() method.

How do you convert local daylight savings time to UTC?

So for example, 2430 becomes 0030 the day after. Daylight Saving Time: When converting from Daylight Saving Time to UTC, the conversions are similar but the UTC Time is one hour less than when converting than its Standard Time counterpart. Local AM/PM time must be converted to a 24-hour clock.

What does pytz UTC localize do?

localize() is the correct function to use for creating datetime aware objects with an initial fixed datetime value. The resulting datetime aware object will have the original datetime value.

What is pytz UTC?

pytz is a Python module mostly used for resolving the issues related to time zone differences. This module determines the time of any region using the local timezone name taking UTC time as the reference. The datetime is a python built-in library. This library can perform almost all the operations on date and time.


1 Answers

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.

like image 72
BorrajaX Avatar answered Sep 18 '22 16:09

BorrajaX