diff --git a/msticpy/common/timespan.py b/msticpy/common/timespan.py index 166236c5..e7e8c3a5 100644 --- a/msticpy/common/timespan.py +++ b/msticpy/common/timespan.py @@ -68,23 +68,23 @@ def __init__( *args, timespan=timespan, start=start, end=end, period=period ) - if not start and not period: + if not start and period is None: raise ValueError( "start, period", "At least one of 'start' or 'period' must be specified.", ) self._period = None - if period: + if period is not None: self._period = self._parse_timedelta(period) self._end = self._parse_time(end, "end") self._start = self._parse_time(start, "start") - if self._start and self._period: + if self._start and self._period is not None: self._end = self._start + self._period if self._end is None: self._end = datetime.now(timezone.utc) - if self._start is None and self._period: + if self._start is None and self._period is not None: self._start = self._end - self._period def __eq__(self, value): @@ -143,7 +143,7 @@ def period(self) -> timedelta: Period timedelta. """ - if not self._period: + if self._period is None: self._period = self.end - self.start return self._period @@ -162,7 +162,8 @@ def _process_args(*args, timespan, start, end, period): if _is_timespan_type(timespan): start = start or getattr(timespan, "start", None) end = end or getattr(timespan, "end", None) - period = period or getattr(timespan, "period", None) + if period is None: + period = getattr(timespan, "period", None) elif isinstance(timespan, tuple): start = timespan[0] end = timespan[1] diff --git a/tests/common/test_timespan.py b/tests/common/test_timespan.py index f5791d3f..02e108de 100644 --- a/tests/common/test_timespan.py +++ b/tests/common/test_timespan.py @@ -57,6 +57,17 @@ def test_timespan_parms(): _validate_timespan(tspan, period=period) +@pytest.mark.parametrize("period", [timedelta(0), "0s"]) +@pytest.mark.parametrize("endpoint", ["start", "end"]) +def test_zero_period(endpoint, period): + """A zero-length span starts and ends at the supplied timestamp.""" + timestamp = datetime(2024, 1, 1, tzinfo=timezone.utc) + span = TimeSpan(**{endpoint: timestamp}, period=period) + + assert span.start == span.end == timestamp + assert span.period == timedelta(0) + + def test_timespan_eq(): """Test creating Timespan from another Timespan.""" period = timedelta(days=1) @@ -73,6 +84,14 @@ def test_timespan_eq(): check.equal(tspan2, tspan) +def test_zero_period_override(): + """An explicit zero period takes precedence when copying a span.""" + original = TimeSpan(start="2024-01-01", period="1D") + span = TimeSpan(timespan=original, period=timedelta(0)) + + assert span.start == span.end == original.start + + def test_timespan_timeselector(): """Test timespan with a time selector object.""" end = datetime.now(timezone.utc)