Skip to content

Commit

Permalink
Merge pull request #12 from guyzmo/master
Browse files Browse the repository at this point in the history
Added support for datetimerange inclusion
  • Loading branch information
thombashi committed Mar 15, 2016
2 parents bbd1739 + ae3be5a commit 43e9859
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions datetimerange/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __isub__(self, other):
def __contains__(self, x):
"""
:param datetime.datetime/str x:
datetime to compare.
datetime or datetimerange to compare.
Parse and convert to datetime if the value type is string.
:return: ``True`` if the ``x`` is within the time range
:rtype: bool
Expand All @@ -115,6 +115,8 @@ def __contains__(self, x):
time_range = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
print "2015-03-22T10:05:00+0900" in time_range
print "2015-03-22T10:15:00+0900" in time_range
time_range_smaller = DateTimeRange("2015-03-22T10:03:00+0900", "2015-03-22T10:07:00+0900")
print time_range_smaller in time_range
.. parsed-literal::
Expand All @@ -128,6 +130,9 @@ def __contains__(self, x):

self.validate_time_inversion()

if isinstance(x, DateTimeRange):
return x.start_datetime >= self.start_datetime and x.end_datetime <= self.end_datetime

try:
value = dateutil.parser.parse(x)
except AttributeError:
Expand Down Expand Up @@ -647,7 +652,7 @@ def encompass(self, x):
.. parsed-literal::
2015-03-22T10:00:00+0900 - 2015-03-22T10:15:00+0900
2015-03-22T10:00:00+0900 - 2015-03-22T10:15:00+0900
"""

self.validate_time_inversion()
Expand Down
2 changes: 2 additions & 0 deletions test/test_datetimerange.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ class Test_DateTimeRange_contains:
[END_DATETIME_TEXT, True],
[TEST_START_DATETIME, True],
[TEST_END_DATETIME, True],
[DateTimeRange("2015-03-22 10:05:00" + TIMEZONE, "2015-03-22 10:06:00" + TIMEZONE), True],
[DateTimeRange("2015-03-22 10:10:01" + TIMEZONE, "2015-03-22 10:11:01" + TIMEZONE), False],
["2015-03-22 09:59:59" + TIMEZONE, False],
["2015-03-22 10:10:01" + TIMEZONE, False],
])
Expand Down

0 comments on commit 43e9859

Please sign in to comment.