Skip to content

Commit 6d16fd7

Browse files
committed
fixup! Implement GetMicrogridMetadata
Add active_at() method and use timezone-aware timestamps.
1 parent f634cc0 commit 6d16fd7

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/frequenz/client/microgrid/_lifetime.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
from dataclasses import dataclass
8-
from datetime import datetime
8+
from datetime import datetime, timezone
99
from 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))

0 commit comments

Comments
 (0)