Skip to content

Commit 2b53d83

Browse files
committed
Add Dispatch.started_at(now)
This way users can use a stable unchanging `now` across calls and be sure that the result is always in respect to the same `now` Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 38e5749 commit 2b53d83

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
## New Features
1212

13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
13+
* `Dispatch.started_at(now: datetime)` was added as alternative to the `started` property for when users want to use the same `now` for multiple calls, ensuring deterministic return values with respect to the same `now`.
1414

1515
## Bug Fixes
1616

src/frequenz/client/dispatch/types.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,26 @@ def started(self) -> bool:
427427
return False
428428

429429
now = datetime.now(tz=timezone.utc)
430+
return self.started_at(now)
431+
432+
def started_at(self, now: datetime) -> bool:
433+
"""Check if the dispatch has started.
434+
435+
A dispatch is considered started if the current time is after the start
436+
time but before the end time.
437+
438+
Recurring dispatches are considered started if the current time is after
439+
the start time of the last occurrence but before the end time of the
440+
last occurrence.
441+
442+
Args:
443+
now: time to use as now
444+
445+
Returns:
446+
True if the dispatch is started
447+
"""
448+
if not self.active:
449+
return False
430450

431451
if now < self.start_time:
432452
return False

0 commit comments

Comments
 (0)