Skip to content
Open
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
13 changes: 7 additions & 6 deletions msticpy/common/timespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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]
Expand Down
19 changes: 19 additions & 0 deletions tests/common/test_timespan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down