From 048b03c91d87024f8ea488bbe3f76da30c8ac7f0 Mon Sep 17 00:00:00 2001 From: jbolognini <115586252+jbolognini@users.noreply.github.com> Date: Fri, 13 Oct 2023 22:06:53 -0400 Subject: [PATCH 1/2] Update logix_driver.py get_plc_time() Changed get_plc_time() method to use attribute 6 for utc system time instead of 11 which was local system time. Added timezone parameter to choose timezone returned. Updated docstring. --- pycomm3/logix_driver.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pycomm3/logix_driver.py b/pycomm3/logix_driver.py index df8c1f0..5a75ea5 100644 --- a/pycomm3/logix_driver.py +++ b/pycomm3/logix_driver.py @@ -335,24 +335,26 @@ def get_plc_info(self) -> dict: except Exception as err: raise ResponseError("Failed to get PLC info") from err - def get_plc_time(self, fmt: str = "%A, %B %d, %Y %I:%M:%S%p") -> Tag: + def get_plc_time(self, fmt: str = "%A, %B %d, %Y %I:%M:%S%p", tz: datetime.timezone = None) -> Tag: """ - Gets the current time of the PLC system clock. The ``value`` attribute will be a dict containing the time in - 3 different forms, *datetime* is a Python datetime.datetime object, *microseconds* is the integer value epoch time, - and *string* is the *datetime* formatted using ``strftime`` and the ``fmt`` parameter. + Gets the current time of the PLC system clock. UTC is returned unless ``tz`` is specified. The ``value`` attribute will + be a dict containing the time in 3 different forms, *datetime* is a Python datetime.datetime object, *microseconds* + is the integer value epoch time, and *string* is the *datetime* formatted using ``strftime`` and the ``fmt`` parameter. :param fmt: format string for converting the time to a string + :param tz: specific datetime.timezone, local system time if omitted or None :return: a Tag object with the current time """ tag = self.generic_message( service=Services.get_attribute_list, class_code=ClassCode.wall_clock_time, instance=b"\x01", - request_data=b"\x01\x00\x0B\x00", + request_data=b"\x01\x00\x06\x00", data_type=Struct(n_bytes(6), ULINT("µs")), ) if tag: _time = datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=tag.value["µs"]) + _time = _time.replace(tzinfo=datetime.timezone.utc).astimezone(tz=tz) value = { "datetime": _time, "microseconds": tag.value["µs"], From cfa4794aa6199622265f5113967dd00bfba09a28 Mon Sep 17 00:00:00 2001 From: jbolognini <115586252+jbolognini@users.noreply.github.com> Date: Thu, 9 Nov 2023 22:05:56 -0500 Subject: [PATCH 2/2] Update logix_driver.py Added clarification to set_plc_time microseconds param documentation that it is relative to UTC epoch --- pycomm3/logix_driver.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pycomm3/logix_driver.py b/pycomm3/logix_driver.py index 5a75ea5..55aff94 100644 --- a/pycomm3/logix_driver.py +++ b/pycomm3/logix_driver.py @@ -368,7 +368,8 @@ def set_plc_time(self, microseconds: Optional[int] = None) -> Tag: """ Set the time of the PLC system clock. - :param microseconds: None to use client PC clock, else timestamp in microseconds to set the PLC clock to + :param microseconds: None to use client PC clock, else timestamp in microseconds to set the PLC clock to. + Timestamp is uS since epoch 1970-1-1, 00:00 UTC :return: Tag with status of request """ if microseconds is None: