>>> 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
pytz will help you to tell if an date is under DST influence by checking dst() method.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With