Skip to content

Commit bff51d9

Browse files
Convert timestamp to UTC
Signed-off-by: Matthias Wende <[email protected]>
1 parent 04f5253 commit bff51d9

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/frequenz/client/electricity_trading/_client.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from datetime import datetime, timedelta, timezone
1313
from decimal import Decimal, InvalidOperation
1414
from typing import Any, AsyncIterator, Awaitable, Callable, cast
15+
from zoneinfo import ZoneInfo
1516

1617
import grpc
1718
from frequenz.api.common.v1.pagination.pagination_params_pb2 import PaginationParams
@@ -1002,10 +1003,32 @@ def receive_public_order_book(
10021003
grpc.RpcError: If an error occurs while streaming public orders.
10031004
"""
10041005

1005-
def dt_to_pb_timestamp(dt: datetime) -> Timestamp:
1006-
ts = Timestamp()
1007-
ts.FromDatetime(dt)
1008-
return ts
1006+
def dt_to_pb_timestamp_utc(dt: datetime.datetime) -> Timestamp:
1007+
"""
1008+
Converts a Python datetime object to a UTC Protobuf Timestamp.
1009+
1010+
The input datetime 'dt' MUST be timezone-aware.
1011+
1012+
Args:
1013+
dt: The Python datetime object to convert.
1014+
1015+
Returns:
1016+
A Protobuf Timestamp object representing the time in UTC.
1017+
1018+
Raises:
1019+
TypeError: If dt is not a datetime.datetime object.
1020+
ValueError: If the input datetime object is naive (lacks timezone info).
1021+
"""
1022+
if not isinstance(dt, datetime):
1023+
raise TypeError("Input must be a datetime.datetime object")
1024+
1025+
if dt.tzinfo is None or dt.tzinfo.utcoffset(dt) is None:
1026+
raise ValueError("Input datetime object must be timezone-aware.")
1027+
1028+
timestamp = Timestamp()
1029+
timestamp.FromDatetime(dt.astimezone(ZoneInfo("UTC")))
1030+
1031+
return timestampo
10091032

10101033
public_order_filter = PublicOrderBookFilter(
10111034
delivery_period=delivery_period,
@@ -1025,12 +1048,14 @@ def dt_to_pb_timestamp(dt: datetime) -> Timestamp:
10251048
electricity_trading_pb2.ReceivePublicOrderBookStreamRequest(
10261049
filter=public_order_filter.to_pb(),
10271050
start_time=(
1028-
dt_to_pb_timestamp(start_time)
1051+
dt_to_pb_timestamp_utc(start_time)
10291052
if start_time
10301053
else None
10311054
),
10321055
end_time=(
1033-
dt_to_pb_timestamp(end_time) if end_time else None
1056+
dt_to_pb_timestamp_utc(end_time)
1057+
if end_time
1058+
else None
10341059
),
10351060
),
10361061
metadata=self._metadata,

0 commit comments

Comments
 (0)