From c7436185ec814828acbb34f8b8bb2d36dc9d1440 Mon Sep 17 00:00:00 2001 From: cwasicki <126617870+cwasicki@users.noreply.github.com> Date: Mon, 9 Dec 2024 22:24:14 +0100 Subject: [PATCH] Ensure tz-aware timestamps in response The API response contains only tz-naive UTC timestamps, which are converted to tz-aware timestamps to avoid misinterpretation. Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com> --- src/frequenz/client/reporting/_client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/frequenz/client/reporting/_client.py b/src/frequenz/client/reporting/_client.py index c9580fb..270e641 100644 --- a/src/frequenz/client/reporting/_client.py +++ b/src/frequenz/client/reporting/_client.py @@ -6,7 +6,7 @@ from collections import namedtuple from collections.abc import AsyncIterator, Iterable, Iterator from dataclasses import dataclass -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from typing import cast import grpc.aio as grpcaio @@ -90,6 +90,9 @@ def __iter__(self) -> Iterator[MetricSample]: cid = cdata.component_id for msample in cdata.metric_samples: ts = msample.sampled_at.ToDatetime() + # Ensure tz-aware timestamps, + # as the API returns tz-naive UTC timestamps + ts = ts.replace(tzinfo=timezone.utc) met = Metric.from_proto(msample.metric).name value = ( msample.value.simple_metric.value