Skip to content

Commit

Permalink
Merge pull request #169 from ftobia/fix_intervals
Browse files Browse the repository at this point in the history
Fix issue #168: intervals greater than one day were incorrect.
  • Loading branch information
timofurrer authored Jan 4, 2019
2 parents bdf2bb3 + 42cc61e commit 4e58e64
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ In chronological order:
- Dima Spivak <[email protected]> (`@dimaspivak <https://github.com/dimaspivak>`_)
- Tom Barron <[email protected]> (`@dtbarron <https://github.com/tbarron>`_)
- Alex Ward <[email protected]> (`@alxwrd <https://github.com/alxwrd>`_)
- Frank Tobia <[email protected]> (`@ftobia <https://github.com/ftobia>`_)
4 changes: 3 additions & 1 deletion maya/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
57 changes: 57 additions & 0 deletions tests/test_maya_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,60 @@ def test_interval_from_iso8601_duration():

assert interval.start == s
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)
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
)

0 comments on commit 4e58e64

Please sign in to comment.