0

Here is my example:

import datetime
from pytz import timezone
EST_TZ = timezone('US/Eastern')

t1 = datetime.datetime.now(EST_TZ)
t2 = datetime.datetime.combine(datetime.datetime.now(EST_TZ).date(), datetime.time(6, 0, 0)).replace(tzinfo=EST_TZ)

(t2-t1).total_seconds()

t1 is roughly datetime.datetime(2023, 3, 15, 5, 54, 31, 438854, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)

t2 is roughly datetime.datetime(2023, 3, 15, 6, 0, tzinfo=<DstTzInfo 'US/Eastern' LMT-1 day, 19:04:00 STD>)

by difference t2-t1 = 3688.561146 has an extra hour due to day light savings (my guess).

Is there an easy fix?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
user1700890
  • 7,144
  • 18
  • 87
  • 183
  • 1
    Use `localize` to set a pytz timezone object, not replace. Or simpler: avoid the issue by not using pytz in the first place. The Python standard library has `zoneinfo` since 3.9, see also my answer to the linked duplicate. – FObersteiner Mar 15 '23 at 16:28

0 Answers0