From 609180c82ea5809ac4ceff3aa4a97a14a01e0891 Mon Sep 17 00:00:00 2001 From: Esteban Zimanyi Date: Fri, 13 Nov 2026 23:51:32 +0100 Subject: [PATCH] Shift and scale dates by whole days, refusing a part below a day DateSet, DateSpan and DateSpanSet shift_scale, and the shift and scale that call it, read a timedelta through the new whole_days of pymeos/collections/time/timecollection.py: the number of days of a timedelta of whole days, an int as given, a ValueError for a timedelta with a part below a day. They state a shift or a width to MEOS whenever one is given, so shift(0) answers a value equal to self. Why. MEOS shifts and scales dates by a whole number of days (dateset_shift_scale, datespan_shift_scale, datespanset_shift_scale take an int), as SQL shift(datespan, integer) and PostgreSQL date + integer do, and has no cast of an interval to a number of days; a binding reaches that int through its host type alone, and a timedelta holding hours cannot state it (doc/specs/meos-bindings-algebra.md of MobilityDB, section 5). TT-6 of the plan of the time types, MobilityDB #3666. Measured. Against the libmeos of MobilityDB master f4d735a8d9, PyMEOS master 622ccd1 answers, for each of DateSet, DateSpan and DateSpanSet: shift(timedelta(hours=12)) raises the MEOS error "At least one of the arguments shift or width must be given", the half day floored to zero; shift(timedelta(hours=-1)) moves every date one day earlier, timedelta(hours=-1).days being -1; shift(timedelta(days=1, hours=23)) moves by one day; scale(timedelta(hours=36)) scales to a width of one day; shift(0) and shift(timedelta(0)) raise that MEOS error, a shift of zero being sent as no shift. This change answers a ValueError for each timedelta with a part below a day and a value equal to self for a shift of zero days. Witness. tests/collections/time/dateset_test.py and datespanset_test.py gain test_shift_zero and test_shift_scale_below_a_day, which fail on master (10 cases) and pass here; the suite passes but for the 4 cases of test_oo_codegen_coverage.py, which read the catalog CI stages and fail the same way on master without it; ruff check and ruff format pass. --- pymeos/collections/time/dateset.py | 8 +++--- pymeos/collections/time/datespan.py | 8 +++--- pymeos/collections/time/datespanset.py | 8 +++--- pymeos/collections/time/timecollection.py | 29 ++++++++++++++++++++- tests/collections/time/dateset_test.py | 17 ++++++++++++ tests/collections/time/datespanset_test.py | 30 ++++++++++++++++++++-- 6 files changed, 85 insertions(+), 15 deletions(-) diff --git a/pymeos/collections/time/dateset.py b/pymeos/collections/time/dateset.py index 0fea2dc0..962c7f18 100644 --- a/pymeos/collections/time/dateset.py +++ b/pymeos/collections/time/dateset.py @@ -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 @@ -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 ------------------------ diff --git a/pymeos/collections/time/datespan.py b/pymeos/collections/time/datespan.py index 7fb93ddc..c725cb36 100644 --- a/pymeos/collections/time/datespan.py +++ b/pymeos/collections/time/datespan.py @@ -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 @@ -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 ------------------------ diff --git a/pymeos/collections/time/datespanset.py b/pymeos/collections/time/datespanset.py index 6db98f0e..9159021f 100644 --- a/pymeos/collections/time/datespanset.py +++ b/pymeos/collections/time/datespanset.py @@ -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 @@ -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 ------------------------ diff --git a/pymeos/collections/time/timecollection.py b/pymeos/collections/time/timecollection.py index e047e802..65affeab 100644 --- a/pymeos/collections/time/timecollection.py +++ b/pymeos/collections/time/timecollection.py @@ -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: diff --git a/tests/collections/time/dateset_test.py b/tests/collections/time/dateset_test.py index 2b03e913..b69b6aef 100644 --- a/tests/collections/time/dateset_test.py +++ b/tests/collections/time/dateset_test.py @@ -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}") diff --git a/tests/collections/time/datespanset_test.py b/tests/collections/time/datespanset_test.py index 082b6167..73f47d95 100644 --- a/tests/collections/time/datespanset_test.py +++ b/tests/collections/time/datespanset_test.py @@ -1,6 +1,8 @@ -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 @@ -8,6 +10,30 @@ 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