Skip to content

Commit

Permalink
WIP: add tests to cover DST behaviour for localize
Browse files Browse the repository at this point in the history
  • Loading branch information
benthorner committed Dec 5, 2024
1 parent 2363944 commit acc415a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/test_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,38 @@ def test_converts_timezone(self):
datetime.datetime(2020, 1, 10, tzinfo=target_tz),
)

def test_errors_converting_over_dst_gain_hour(self):
utc_tz = zoneinfo.ZoneInfo("UTC")
london_tz = zoneinfo.ZoneInfo("Europe/London")

# Create a range in London over the hour that is "gained"
# when Daylight Savings Time (DST) starts - at 1AM.
dt_range = ranges.FiniteDatetimeRange(
datetime.datetime(2020, 3, 29, hour=1, tzinfo=london_tz),
datetime.datetime(2020, 3, 29, hour=2, tzinfo=london_tz),
)

# Converting to UTC should error due to the period being
# empty: removing the "fake hour" means 2AM => 1AM.
with pytest.raises(ValueError):
assert dt_range.localize(utc_tz)

def test_errors_converting_over_dst_loss_hour(self):
utc_tz = zoneinfo.ZoneInfo("UTC")
london_tz = zoneinfo.ZoneInfo("Europe/London")

# Create a range in UTC over the hour before Daylight Savings
# Time (DST) ends - at 2AM.
dt_range = ranges.FiniteDatetimeRange(
datetime.datetime(2020, 10, 25, hour=0, tzinfo=utc_tz),
datetime.datetime(2020, 10, 25, hour=1, tzinfo=utc_tz),
)

# Converting to London timezone should error due to the period
# being empty: both times map to 1AM.
with pytest.raises(ValueError):
assert dt_range.localize(london_tz)

def test_errors_if_naive(self):
tz = zoneinfo.ZoneInfo("Europe/London")

Expand Down

0 comments on commit acc415a

Please sign in to comment.