Skip to content
Merged
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
8 changes: 4 additions & 4 deletions pymeos/collections/time/dateset.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
)

from ..base import Set
from .timecollection import TimeCollection
from .timecollection import TimeCollection, whole_days

if TYPE_CHECKING:
from .datespan import DateSpan
Expand Down Expand Up @@ -238,9 +238,9 @@ def shift_scale(
dateset_shift_scale
"""
assert shift is not None or duration is not None, "shift and scale deltas must not be both None"
shift = shift.days if isinstance(shift, timedelta) else int(shift) if shift is not None else 0
duration = duration.days if isinstance(duration, timedelta) else int(duration) if duration is not None else 0
tss = dateset_shift_scale(self._inner, shift, duration, shift != 0, duration != 0)
shift_days = whole_days(shift) if shift is not None else 0
width_days = whole_days(duration) if duration is not None else 0
tss = dateset_shift_scale(self._inner, shift_days, width_days, shift is not None, duration is not None)
return DateSet(_inner=tss)

# ------------------------- Topological Operations ------------------------
Expand Down
8 changes: 4 additions & 4 deletions pymeos/collections/time/datespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)

from ..base import Span
from .timecollection import TimeCollection
from .timecollection import TimeCollection, whole_days

if TYPE_CHECKING:
from .dateset import DateSet
Expand Down Expand Up @@ -243,10 +243,10 @@ def shift_scale(
"""
assert shift is not None or duration is not None, "shift and scale deltas must not be both None"

shift = shift.days if isinstance(shift, timedelta) else int(shift) if shift is not None else 0
duration = duration.days if isinstance(duration, timedelta) else int(duration) if duration is not None else 0
shift_days = whole_days(shift) if shift is not None else 0
width_days = whole_days(duration) if duration is not None else 0

modified = datespan_shift_scale(self._inner, shift, duration, shift != 0, duration != 0)
modified = datespan_shift_scale(self._inner, shift_days, width_days, shift is not None, duration is not None)
return DateSpan(_inner=modified)

# ------------------------- Topological Operations ------------------------
Expand Down
8 changes: 4 additions & 4 deletions pymeos/collections/time/datespanset.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)

from ..base.spanset import SpanSet
from .timecollection import TimeCollection
from .timecollection import TimeCollection, whole_days

if TYPE_CHECKING:
from .datespan import DateSpan
Expand Down Expand Up @@ -323,10 +323,10 @@ def shift_scale(
"""
assert shift is not None or duration is not None, "shift and scale deltas must not be both None"

shift = shift.days if isinstance(shift, timedelta) else int(shift) if shift is not None else 0
duration = duration.days if isinstance(duration, timedelta) else int(duration) if duration is not None else 0
shift_days = whole_days(shift) if shift is not None else 0
width_days = whole_days(duration) if duration is not None else 0

modified = datespanset_shift_scale(self._inner, shift, duration, shift != 0, duration != 0)
modified = datespanset_shift_scale(self._inner, shift_days, width_days, shift is not None, duration is not None)
return DateSpanSet(_inner=modified)

# ------------------------- Topological Operations ------------------------
Expand Down
29 changes: 28 additions & 1 deletion pymeos/collections/time/timecollection.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
from abc import ABC
from datetime import date
from datetime import date, timedelta
from typing import TypeVar

from ..base.collection import Collection

TimeClass = TypeVar("TimeClass", bound=date)

_DAY = timedelta(days=1)


def whole_days(delta: timedelta | int) -> int:
"""
Returns the days of a shift or a width of dates, which MEOS takes as a whole number of days.

The collections of timestamps pass a :class:`datetime.timedelta` through the cast
#timedelta_to_interval of pymeos_cffi; MEOS has no cast of an interval to a number of days,
so the collections of dates read the days of the :class:`datetime.timedelta` here.

Args:
delta: :class:`int` number of days or :class:`datetime.timedelta` of whole days

Returns:
The number of days as an :class:`int`

Raises:
ValueError: when ``delta`` is a :class:`datetime.timedelta` with a part below a day
"""
if isinstance(delta, timedelta):
days, rest = divmod(delta, _DAY)
if rest:
raise ValueError(f"A shift or a width of dates is a whole number of days: {delta!r}")
return days
return int(delta)


class TimeCollection(Collection[TimeClass], ABC):
def is_before(self, other) -> bool:
Expand Down
17 changes: 17 additions & 0 deletions tests/collections/time/dateset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,23 @@ def test_shift_scale(self, shift, scale):
shifted_scaled = self.dateset.shift_scale(shift, scale)
assert shifted_scaled == DateSet("{2020-01-05, 2020-01-06, 2020-01-10}")

# A shift of no days, as #test_shift shifts by a number of days
@pytest.mark.parametrize("delta", [timedelta(0), 0], ids=["timedelta", "int"])
def test_shift_zero(self, delta):
assert self.dateset.shift(delta) == self.dateset

# A timedelta with a part below a day, which #test_shift and #test_scale take in whole days
@pytest.mark.parametrize(
"delta",
[timedelta(hours=12), timedelta(hours=-1), timedelta(days=1, hours=23)],
ids=["half a day", "minus one hour", "one day and 23 hours"],
)
def test_shift_scale_below_a_day(self, delta):
with pytest.raises(ValueError):
self.dateset.shift(delta)
with pytest.raises(ValueError):
self.dateset.scale(delta)


class TestDateSetMiscFunctions(TestDateSet):
dateset = DateSet("{2020-01-01, 2020-01-02, 2020-01-04}")
Expand Down
30 changes: 28 additions & 2 deletions tests/collections/time/datespanset_test.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
from datetime import date
from datetime import date, timedelta

from pymeos import DateSpanSet
import pytest

from pymeos import DateSpan, DateSpanSet
from tests.conftest import TestPyMEOS


class TestDateSpanSet(TestPyMEOS):
date_spanset = DateSpanSet("{[2019-09-08, 2019-09-10], [2019-09-12, 2019-09-14]}")


class TestDateSpanSetTransformations(TestDateSpanSet):
# The shifts of a date span set and of a date span, as
# #TestDateSetFunctionsFunctions.test_shift_zero and
# #TestDateSetFunctionsFunctions.test_shift_scale_below_a_day shift a date set
date_span = DateSpan("[2019-09-08, 2019-09-14]")

@pytest.mark.parametrize("delta", [timedelta(0), 0], ids=["timedelta", "int"])
def test_shift_zero(self, delta):
assert self.date_spanset.shift(delta) == self.date_spanset
assert self.date_span.shift(delta) == self.date_span

@pytest.mark.parametrize(
"delta",
[timedelta(hours=12), timedelta(hours=-1), timedelta(days=1, hours=23)],
ids=["half a day", "minus one hour", "one day and 23 hours"],
)
def test_shift_scale_below_a_day(self, delta):
for value in (self.date_spanset, self.date_span):
with pytest.raises(ValueError):
value.shift(delta)
with pytest.raises(ValueError):
value.scale(delta)


class TestDateSpanSetAccessors(TestDateSpanSet):
# The dates of a date span set are the bounds of its spans, answered as a
# list as #TestDateSetAccessors.test_elements answers the elements of a set
Expand Down
Loading