1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-30 00:00:11 +01:00

python: Always use UTC time when handling Timestamps

Signed-off-by: Philipp Jungkamp <Philipp.Jungkamp@opal-rt.com>
This commit is contained in:
Philipp Jungkamp 2023-09-21 13:29:39 +02:00 committed by Steffen Vogel
parent 396dd7642e
commit 86bba9c5e8
2 changed files with 5 additions and 4 deletions

View file

@ -8,7 +8,7 @@ SPDX-License-Identifier: Apache-2.0
import hashlib import hashlib
from ctypes import c_double, c_float, sizeof from ctypes import c_double, c_float, sizeof
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime from datetime import datetime, timezone
from functools import total_ordering from functools import total_ordering
from sys import byteorder as native from sys import byteorder as native
from typing import Iterable from typing import Iterable
@ -25,6 +25,7 @@ Signal = bool | int | float | complex
class Timestamp: class Timestamp:
""" """
A VILLASnode timestamp. Based on the C struct timespec. A VILLASnode timestamp. Based on the C struct timespec.
These timestamps are always UTC.
""" """
seconds: int seconds: int
@ -51,7 +52,7 @@ class Timestamp:
return float(self) return float(self)
def datetime(self) -> datetime: def datetime(self) -> datetime:
return datetime.fromtimestamp(self.timestamp()) return datetime.fromtimestamp(self.timestamp(), tz=timezone.utc)
def __float__(self): def __float__(self):
return float(self.seconds) + float(self.nanoseconds) * 1e-9 return float(self.seconds) + float(self.nanoseconds) * 1e-9

View file

@ -4,7 +4,7 @@ SPDX-FileCopyrightText: 2023 OPAL-RT Germany GmbH
SPDX-License-Identifier: Apache-2.0 SPDX-License-Identifier: Apache-2.0
""" # noqa: E501 """ # noqa: E501
from villas.node.sample import Sample, Timestamp, Frame from villas.node.sample import Sample, Timestamp, Frame
from datetime import datetime from datetime import datetime, timezone
from cmath import sqrt from cmath import sqrt
@ -21,7 +21,7 @@ def test_timestamp_conversion():
assert ts.timestamp() == fl assert ts.timestamp() == fl
assert fl_ts == Timestamp.fromtimestamp(fl) assert fl_ts == Timestamp.fromtimestamp(fl)
dt = datetime(1973, 11, 29, 22, 33, 9, 123457) dt = datetime(1973, 11, 29, 21, 33, 9, 123457, tzinfo=timezone.utc)
dt_ts = Timestamp(123456789, 123457000) dt_ts = Timestamp(123456789, 123457000)
assert ts.datetime() == dt assert ts.datetime() == dt
assert dt_ts == Timestamp.fromdatetime(dt) assert dt_ts == Timestamp.fromdatetime(dt)