Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16773

Why are these two datetimes not equal?

$
0
0
from datetime import datetime, timedeltafrom zoneinfo import ZoneInfochi = ZoneInfo("America/Chicago")nyc = ZoneInfo("America/New_York")dt1 = datetime(2024, 3, 10, 3, 30, tzinfo=chi)dt2 = datetime(2024, 3, 10, 2, 30, tzinfo=chi)print(dt1 == dt1.astimezone(nyc))print(dt2 == dt2.astimezone(nyc))

Actual result:

TrueFalse

Expected result:

TrueTrue

Why is False returned in one of those cases? In both cases it's comparing the same datetime, only adjusted to a different zone.

The same thing also happens in the fold:

from datetime import datetime, timedeltafrom zoneinfo import ZoneInfochi = ZoneInfo("America/Chicago")nyc = ZoneInfo("America/New_York")dt1 = datetime(2024, 11, 3, 2, 30, tzinfo=chi)dt2 = datetime(2024, 11, 3, 1, 30, tzinfo=chi)print(dt1 == dt1.astimezone(nyc))print(dt2 == dt2.astimezone(nyc))

Viewing all articles
Browse latest Browse all 16773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>