thombashi / DateTimeRange

DateTimeRange is a Python library to handle a time range. e.g. check whether a time is within the time range, get the intersection of time ranges, truncate a time range, iterate through a time range, and so forth.
https://datetimerange.rtfd.io/
MIT License
106 stars 16 forks source link

is_intersection() with intersection_threshold given fails when ranges are on different dates #48

Closed wernersa closed 6 months ago

wernersa commented 7 months ago

Related to issue #43

Using the is_intersection(x, intersection_threshold=datetime.timedelta(seconds=1) function, it fails with Assertion Error if the two DateTimeRange are on different days.

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
x1 = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
x2 = DateTimeRange("2015-03-23T10:05:00+0900", "2015-03-23T10:15:00+0900") # +1 day

time_range.is_intersection(x1, intersection_threshold=datetime.timedelta(seconds=1)) # No problem
time_range.is_intersection(x2, intersection_threshold=datetime.timedelta(seconds=1)) # Assertion error

The workaround (that is not pythonic or pretty):

time_range.is_intersection(x1, intersection_threshold=datetime.timedelta(seconds=1)
if time_range.is_intersection(x2): # False
    time_range.is_intersection(x2, intersection_threshold=datetime.timedelta(seconds=1)
thombashi commented 6 months ago

@wernersa Thank you for your report. The problem is fixed at DateTimeRange 2.2.2. Please try to upgrade the package.