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

Exclude start and end time when using is_intersection #43

Closed usman5251 closed 1 year ago

usman5251 commented 2 years ago

Sorry the title is a bit unclear but I'll try to describe what I want to do as clear as possible. So like in this example code, this returns True:

from datetimerange import DateTimeRange
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
x = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
time_range.is_intersection(x)

But if x has same start (or) end minute same as time_range I want it to return False, Currently it returns true.

x = DateTimeRange("2015-03-22T10:10:00+0900", "2015-03-22T10:15:00+0900")
time_range.is_intersection(x)

So, if start/end time for x == start/end time for time_range, I want it to return False on is_intersection(), is there a simple way to achieve this?

thombashi commented 2 years ago

@usman5251 For a time being, you could check such cases by using get_timedelta_second method. for example:

    if not time_range.is_intersection(x):
        return False

    if time_range.intersection(x).get_timedelta_second() == 0:
        # get_timedelta_second will return 0 if start/end time for x == start/end time for time_range,
        return False

    return True

Probably, adding an argument to is_intersection method that makes it possible to exclude start/end time would be preferable in the future

usman5251 commented 2 years ago

Oh that's super nice and clean!! Thanks alot @thombashi.

thombashi commented 1 year ago

Since DateTimeRange 2.0.0, you can write as follows:

time_range.is_intersection(x, intersection_threshold=datetime.timedelta(seconds=1)):