From 2c2a8d03c8706cc24f1f45be945e7c4cf139aa43 Mon Sep 17 00:00:00 2001 From: Frank Tobia Date: Wed, 2 Jan 2019 14:03:34 -0500 Subject: [PATCH 1/3] Fix issue #168: intervals greater than one day were incorrect. --- maya/core.py | 4 +++- tests/test_maya_interval.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/maya/core.py b/maya/core.py index cdf018b..e66bbd3 100644 --- a/maya/core.py +++ b/maya/core.py @@ -806,4 +806,6 @@ def intervals(start, end, interval): while current_timestamp.epoch < end.epoch: yield current_timestamp - current_timestamp = current_timestamp.add(seconds=interval.seconds) + current_timestamp = current_timestamp.add( + seconds=interval.total_seconds() + ) diff --git a/tests/test_maya_interval.py b/tests/test_maya_interval.py index 1d040a4..1e1362b 100755 --- a/tests/test_maya_interval.py +++ b/tests/test_maya_interval.py @@ -570,3 +570,16 @@ def test_interval_from_iso8601_duration(): assert interval.start == s assert interval.end == e + + +def test_issue_168_regression(): + start = maya.now() + end = start.add(weeks=1) + gen = maya.intervals(start=start, end=end, interval=60 * 60 * 24) + # Since the bug causes the generator to never end, first sanity + # check that two results are not the same. + assert next(gen) != next(gen) + assert ( + len(list(maya.intervals(start=start, end=end, interval=60 * 60 * 24))) + == 7 + ) From 9abd61808331df008f25b8b887bfe61379f49841 Mon Sep 17 00:00:00 2001 From: Frank Tobia Date: Thu, 3 Jan 2019 13:58:06 -0500 Subject: [PATCH 2/3] Add basic tests for maya.intervals() --- tests/test_maya_interval.py | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/test_maya_interval.py b/tests/test_maya_interval.py index 1e1362b..bfa6f5e 100755 --- a/tests/test_maya_interval.py +++ b/tests/test_maya_interval.py @@ -572,6 +572,50 @@ def test_interval_from_iso8601_duration(): assert interval.end == e +@pytest.mark.parametrize( + "start_string,end_string,interval,expected_count", + [ + ("2019-01-03 11:40:00Z", "2019-01-03 11:40:20Z", 2, 10), + ( + "2019-01-03 11:40:00Z", + "2019-01-03 11:40:30Z", + timedelta(seconds=2), + 15, + ), + ("2019-01-03 11:40:00Z", "2019-01-03 11:45:00Z", 2 * 60, 3), + ( + "2019-01-03 11:40:00Z", + "2019-01-03 11:51:00Z", + timedelta(minutes=1), + 11, + ), + ("2019-01-03 11:40:00Z", "2019-01-03 21:40:00Z", 3 * 60 * 60, 4), + ( + "2019-01-03 11:40:00Z", + "2019-01-03 13:41:00Z", + timedelta(hours=1), + 3, + ), + ("2019-01-03 11:40:00Z", "2019-01-09 11:40:00Z", 3 * 60 * 60 * 24, 2), + ("2019-01-03 11:40:00Z", "2019-01-05 12:00:00Z", timedelta(days=2), 2), + ], + ids=( + "seconds", + "seconds-timedelta", + "minutes", + "minutes-timedelta", + "hours", + "hours-timedelta", + "days", + "days-timedelta", + ), +) +def test_intervals(start_string, end_string, interval, expected_count): + start = maya.parse(start_string) + end = maya.parse(end_string) + assert len(list(maya.intervals(start, end, interval))) == expected_count + + def test_issue_168_regression(): start = maya.now() end = start.add(weeks=1) From 42cc61e7a838e5eef6e07b89ea777f17c1b9b91d Mon Sep 17 00:00:00 2001 From: Frank Tobia Date: Thu, 3 Jan 2019 14:00:47 -0500 Subject: [PATCH 3/3] Update authors. --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 5d13d12..3d377f0 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -25,3 +25,4 @@ In chronological order: - Dima Spivak (`@dimaspivak `_) - Tom Barron (`@dtbarron `_) - Alex Ward (`@alxwrd `_) +- Frank Tobia (`@ftobia `_)