15

I have a date and a time that I'm attempting to combine in Python. The time is timezone aware.

However, when I try and combine them, I get the wrong time.

import pytz
from datetime import time, date
NYC_TIME = pytz.timezone('America/New_York')

start_date = date(2012, 7, 7)
start_time = time(hour = 0, tzinfo = NYC_TIME)
combined = datetime.combine(start_date, start_time)
print combined
print NYC_TIME.normalize(combined)

This prints 2012-07-07 00:00:00-05:00, which normalizes to 2012-07-07 01:00:00-04:00. Why is this happening? How can I avoid it?

Chris B.
  • 85,731
  • 25
  • 98
  • 139
  • I assume by "avoid it" you want the local time to remain fixed even if the UTC offset changes due to Daylight Saving? – Mark Ransom Jun 28 '12 at 22:14
  • I was expecting, if I have a time `MIDNIGHT = time(hour = 0, tzinfo = NYC_TIME)`, that it would remain midnight no matter what date I slotted it into. Apparently, that is not the case. – Chris B. Jun 28 '12 at 22:24

1 Answers1

3

A time without a date attached must assume it's not in the Daylight Saving period. Once you attach a date to it, that assumption can be corrected. The zone offset changes, and the time changes as well to keep it at the same UTC equivalent.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • 1
    That's strange, since if you compare two timezone-aware times that don't have a static offset, they raise a naive timezone error—that is, they refuse to assume anything about whether they're in Daylight Savings or not. – Chris B. Jun 28 '12 at 22:16