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

Added union to DateTimeRange class #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions datetimerange/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,28 @@ def truncate(self, percentage):
self.__start_datetime += discard_time
self.__end_datetime -= discard_time

def union(self, x):
"""
Returns the timedelta of the union of the input and current
DateTimeRange.

:param DateTimeRange x: List of DateTimeRanges.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x is not a List of DateTimeRange instances judging from what the method does.


:Sample Code:
.. code:: python

from datetimerange import DateTimeRange
dtr0 = DateTimeRange("2015-03-22T10:00:00+0900", "2015-03-22T10:10:00+0900")
dtr1 = DateTimeRange("2015-03-22T10:05:00+0900", "2015-03-22T10:15:00+0900")
union = dtr0.union(dtr1)
print(union)
"""

if self.is_intersection(x):
return self.encompass(x).timedelta
else:
return self.timedelta + x.timedelta

def __validate_value(self, data_prop):
if data_prop.typecode not in [typepy.Typecode.DATETIME, typepy.Typecode.NONE]:
raise ValueError("invalid datetime value: {}".format(data_prop))
84 changes: 84 additions & 0 deletions test/test_dtr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,90 @@ def test_exception(self, lhs, rhs, expected):
with pytest.raises(expected):
lhs.encompass(rhs)

class TestDateTimeRange_union:
@pytest.mark.parametrize(
["lhs", "rhs", "expected"],
[
[
DateTimeRange(TEST_START_DATETIME, TEST_END_DATETIME),
DateTimeRange(TEST_START_DATETIME, TEST_END_DATETIME),
timedelta(minutes=10),
],
[
DateTimeRange("2015-01-22T09:50:00 JST", "2015-01-22T10:00:00 JST"),
DateTimeRange("2015-01-22T10:10:00 JST", "2015-03-22T10:20:00 JST"),
timedelta(days=59, minutes=10, seconds=600),
],
[
DateTimeRange("2015-01-22T09:50:00 JST", "2015-01-22T10:00:00 JST"),
DateTimeRange("2015-01-22T10:00:00 JST", "2015-03-22T10:20:00 JST"),
timedelta(days=59, seconds=1800),
],
[
DateTimeRange("2015-01-22T09:50:00 JST", "2015-01-22T10:05:00 JST"),
DateTimeRange("2015-01-22T10:00:00 JST", "2015-03-22T10:20:00 JST"),
timedelta(days=59, seconds=1800),
],
[
DateTimeRange("2015-01-22T10:00:00 JST", "2015-03-22T10:20:00 JST"),
DateTimeRange("2015-01-22T09:50:00 JST", "2015-01-22T10:05:00 JST"),
timedelta(days=59, seconds=1800),
],
[
DateTimeRange("2014-01-22T10:00:00 JST", "2016-03-22T10:20:00 JST"),
DateTimeRange("2015-01-22T09:50:00 JST", "2015-01-22T10:05:00 JST"),
timedelta(days=790, seconds=1200),
],
[
DateTimeRange("2015-01-12T10:00:00 JST", "2015-02-22T10:10:00 JST"),
DateTimeRange("2015-01-22T10:00:00 JST", "2015-03-22T10:10:00 JST"),
timedelta(days=69, seconds=600),
],
[
DateTimeRange("2014-01-12T09:30:00 JST", "2014-02-22T10:00:00 JST"),
DateTimeRange("2015-01-10T10:00:00 JST", "2015-03-22T10:30:00 JST"),
timedelta(days=112, seconds=3600)
]
],
)

def test_normal(self, lhs, rhs, expected):
lhs_org = deepcopy(lhs)

assert lhs.union(rhs) == expected
assert lhs == lhs_org

@pytest.mark.parametrize(
["lhs", "rhs", "expected"],
[
[DateTimeRange(None, None), DateTimeRange(None, None), TypeError],
[
DateTimeRange(None, TEST_END_DATETIME),
DateTimeRange(TEST_START_DATETIME, TEST_END_DATETIME),
TypeError,
],
[
DateTimeRange(TEST_END_DATETIME, TEST_START_DATETIME),
DateTimeRange(TEST_START_DATETIME, TEST_END_DATETIME),
ValueError,
],
[
DateTimeRange(TEST_START_DATETIME, TEST_END_DATETIME),
DateTimeRange(None, TEST_END_DATETIME),
TypeError,
],
[
DateTimeRange(TEST_START_DATETIME, TEST_END_DATETIME),
DateTimeRange(TEST_END_DATETIME, TEST_START_DATETIME),
ValueError,
],
],
)
def test_exception(self, lhs, rhs, expected):
with pytest.raises(expected):
lhs.intersection(rhs)



class TestDateTimeRange_truncate:
@pytest.mark.parametrize(
Expand Down