0

I have created a 4pm datetime object this way:

dt = pytz.timezone('America/New_York').localize(datetime(1970, 1, 1, hour=16))

Now I am replacing the day month and year:

newDt = dt.replace(year=2019, month=7, day=23)

printing the newDt object yields, which is odd because its -4 GMT today:

2019-07-23 16:00:00-05:00    

Is there a way to force the recalculation for the DST offset?

delita
  • 1,571
  • 1
  • 19
  • 25

1 Answers1

0

Somehow it seems to be related to the timezone itself. Just like pytc.utc is different to 'Europe/London', in this case 'America/New_York' is not the same as 'US/Eastern'.

newDt = dt.replace(year=2019, month=7, day=23) #2019-07-23 16:00:00-05:00
newDt.astimezone(pytz.timezone('US/Eastern'))  #2019-07-23 17:00:00-04:00

However, according to Wikipedia they should behave the exact same way.

CORRECTION

My answer above does not seem to be right.

Apparently the issue seems to be related to some odd behavior from the replace function, where it does not carry the dst information if the original date was not during the daylight saving period. Some people have mentioned it here.

You would need to re-localize that date using astimezone

print (newDt.astimezone(pytz.timezone('America/New_York')))

Out[14]:
2019-07-23 17:00:00-04:00
realr
  • 3,652
  • 6
  • 23
  • 34