From 62542a44c0f84ebfe2c9037667973a11ae89276b Mon Sep 17 00:00:00 2001 From: genishernandez Date: Sun, 6 Sep 2026 14:07:25 -0400 Subject: [PATCH] Fix OPC UA DT conversion from nanoseconds --- .../src/drivers/plugins/python/opcua/opcua_utils.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/core/src/drivers/plugins/python/opcua/opcua_utils.py b/core/src/drivers/plugins/python/opcua/opcua_utils.py index bab74b43..e78f21ed 100644 --- a/core/src/drivers/plugins/python/opcua/opcua_utils.py +++ b/core/src/drivers/plugins/python/opcua/opcua_utils.py @@ -222,12 +222,21 @@ def convert_value_for_opcua(datatype: str, value: Any) -> Any: tv_sec, tv_nsec = value try: dt = datetime.fromtimestamp(tv_sec, tz=timezone.utc) - dt = dt.replace(microsecond=tv_nsec // 1000) - return dt + return dt.replace(microsecond=tv_nsec // 1000) except (OSError, OverflowError, ValueError): return datetime(1970, 1, 1, tzinfo=timezone.utc) elif isinstance(value, datetime): return value + elif isinstance(value, int): + # Some OpenPLC debug interfaces expose DT as Unix time + # in nanoseconds instead of an IEC_TIMESPEC tuple. + try: + return datetime.fromtimestamp( + value / 1_000_000_000, + tz=timezone.utc + ) + except (OSError, OverflowError, ValueError): + return datetime(1970, 1, 1, tzinfo=timezone.utc) return datetime(1970, 1, 1, tzinfo=timezone.utc) else: