File tree Expand file tree Collapse file tree 1 file changed +19
-5
lines changed
src/frequenz/client/microgrid Expand file tree Collapse file tree 1 file changed +19
-5
lines changed Original file line number Diff line number Diff line change 55
66
77from dataclasses import dataclass
8- from datetime import datetime
8+ from datetime import datetime , timezone
99from functools import cached_property
1010
1111
@@ -36,10 +36,24 @@ def __post_init__(self) -> None:
3636 if self .start is not None and self .end is not None and self .start > self .end :
3737 raise ValueError ("Start must be before or equal to end." )
3838
39+ def active_at (self , timestamp : datetime ) -> bool :
40+ """Check whether this lifetime is active at a specific timestamp.
41+
42+ Args:
43+ timestamp: The timestamp to check activity for.
44+
45+ Returns:
46+ True if active at the given timestamp, False otherwise.
47+ """
48+ # Handle start time - it's not active if start is in the future
49+ if self .start is not None and self .start > timestamp :
50+ return False
51+ # Handle end time - active up to and including end time
52+ if self .end is not None :
53+ return self .end >= timestamp
54+ return True
55+
3956 @cached_property
4057 def active (self ) -> bool :
4158 """Whether this lifetime is currently active."""
42- now = datetime .now ()
43- if self .start is not None and self .start > now :
44- return False
45- return self .end is None or self .end >= now
59+ return self .active_at (datetime .now (timezone .utc ))
You can’t perform that action at this time.
0 commit comments