Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse order of min in FiniteDatetimeRange.intersection #196

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/test_ranges.py
Original file line number Diff line number Diff line change
@@ -13,6 +13,10 @@
from xocto import localtime, ranges


TZ_LONDON = zoneinfo.ZoneInfo("Europe/London")
TZ_BERLIN = zoneinfo.ZoneInfo("Europe/Berlin")


@composite
def valid_integer_range(draw):
boundaries = draw(sampled_from(ranges.RangeBoundaries))
@@ -1174,6 +1178,20 @@ def test_union_with_continuum(self):
assert ranges._is_datetime_range(range | other)
assert range | other == other | range == ranges.DatetimeRange.continuum()

def test_preference_on_equal_end(self):
r1 = ranges.FiniteDatetimeRange(
datetime.datetime(2020, 1, 1, tzinfo=TZ_LONDON),
datetime.datetime(2022, 1, 1, tzinfo=TZ_LONDON),
)
r2 = ranges.FiniteDatetimeRange(
datetime.datetime(2021, 1, 1, hour=1, tzinfo=TZ_BERLIN),
datetime.datetime(2022, 1, 1, hour=1, tzinfo=TZ_BERLIN),
)
assert r1.end == r2.end
union = r1 | r2
# r1.end was picked over r2.end.
assert union.start.tzinfo == union.end.tzinfo == TZ_LONDON

class TestIntersection:
def test_intersection_of_touching_ranges(self):
range = ranges.FiniteDatetimeRange(
@@ -1237,6 +1255,20 @@ def test_intersection_with_half_finite_range(self):
)
)

def test_preference_on_equal_end(self):
r1 = ranges.FiniteDatetimeRange(
datetime.datetime(2020, 1, 1, tzinfo=TZ_LONDON),
datetime.datetime(2022, 1, 1, tzinfo=TZ_LONDON),
)
r2 = ranges.FiniteDatetimeRange(
datetime.datetime(2021, 1, 1, hour=1, tzinfo=TZ_BERLIN),
datetime.datetime(2022, 1, 1, hour=1, tzinfo=TZ_BERLIN),
)
assert r1.end == r2.end
intersection = r1 & r2
# r2.end was picked over r1.end.
assert intersection.start.tzinfo == intersection.end.tzinfo == TZ_BERLIN

class TestLocalize:
def test_converts_timezone(self):
# Create a datetime range in Sydney, which is
2 changes: 1 addition & 1 deletion xocto/ranges.py
Original file line number Diff line number Diff line change
@@ -861,7 +861,7 @@ def intersection(
if left.end <= right.start:
return None
else:
return FiniteDatetimeRange(right.start, min(left.end, right.end))
return FiniteDatetimeRange(right.start, min(right.end, left.end))

base_intersection = super().intersection(other)
if base_intersection is None: