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

Rastio
  • 1,506
  • 1
  • 13
  • 18

1 Answers1

5

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.

Savir
  • 17,568
  • 15
  • 82
  • 136
  • 2
    Oh, well, it would help if I knew October is 10th month. Thanks @BorrajaX – Rastio Oct 27 '16 at 22:08
  • But why is this giving me an output that is ~ 30 mins off? I feel I'm missing something dumb – roganjosh Oct 27 '16 at 22:11
  • `2016-11-27 14:46:00 America/Vancouver 2016-11-27 22:46:00+00:00 2016-10-27 22:07:25.008000` run from UK – roganjosh Oct 27 '16 at 22:12
  • @roganjosh, it looks to me like the conversion is properly done... However, in the OP's example, the last chunk printed is `utcnow`. Maybe you're looking at that? – Savir Oct 27 '16 at 22:13
  • I mean... In the OPs example, instead of doing `print t, tz, utc, now`, do `print t, tz, utc` – Savir Oct 27 '16 at 22:14
  • @roganjosh, it's 15:15 here in Vancouver, constructed time is 14:46, that's the half hour you are looking at? Because of last print is utcnow() – Rastio Oct 27 '16 at 22:16
  • 1
    Haha!! No prob!! I had my more than fair share with timezones at my work, and I can feel the pain (I know how confusing they can be) **:-)** – Savir Oct 27 '16 at 22:16
  • Oh, @Rastio! I hadn't seen that you already mentioned the DST in your question's title... Boh... If I had seen that I wouldn't have gone on and on about DST and all that... Sorry **:-S** – Savir Oct 27 '16 at 22:19
  • I guess DSTs traumatized me in my last job... Haha – Savir Oct 27 '16 at 22:19