From f1c9b2951392cdc8eb07608b6f03f1d72850870a Mon Sep 17 00:00:00 2001 From: Tattoo Date: Tue, 17 Oct 2023 14:16:09 +0300 Subject: [PATCH] Fix time problems on Windows 10 Due to Windows 10[1], Robot Interfaces did produce timestamps that were before epoch and thus failed when Robot tried to convert them into results. Now, if we try to put a timestamp that is before epoch, it is just reset to epoch. Also, RF3 interface did not have parity with the RF4 interface -- now it does [1] https://stackoverflow.com/a/58203399 --- src/oxygen/oxygen.py | 1 - src/oxygen/robot3_interface.py | 5 +++++ src/oxygen/robot4_interface.py | 3 +++ tests/utest/robot_interface/test_time_conversions.py | 11 +++++++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/oxygen/oxygen.py b/src/oxygen/oxygen.py index 825c146..dce6f85 100644 --- a/src/oxygen/oxygen.py +++ b/src/oxygen/oxygen.py @@ -99,7 +99,6 @@ def output_file(self, path): result.visit(OxygenVisitor(self.run_time_data)) result.save() - class OxygenLibrary(OxygenCore): '''Oxygen is a tool to consolidate different test tools' reports together as a single Robot Framework log and report. ``oxygen.OxygenLibrary`` diff --git a/src/oxygen/robot3_interface.py b/src/oxygen/robot3_interface.py index 22b0385..87fe6c1 100644 --- a/src/oxygen/robot3_interface.py +++ b/src/oxygen/robot3_interface.py @@ -290,6 +290,11 @@ def ms_to_timestamp(self, milliseconds): time_object = datetime.fromtimestamp(int(milliseconds / 1000)) - tz_delta milliseconds_delta = timedelta(milliseconds=(milliseconds % 1000)) time_object = (time_object + milliseconds_delta) + if time_object.year < 1970: + time_object = datetime.fromtimestamp(0) + # fromtimestamp() loses milliseconds, add them back + milliseconds_delta = timedelta(milliseconds=(milliseconds % 1000)) + time_object = (time_object + milliseconds_delta) time_format = self.get_time_format() diff --git a/src/oxygen/robot4_interface.py b/src/oxygen/robot4_interface.py index d1b0cce..a8cbc36 100644 --- a/src/oxygen/robot4_interface.py +++ b/src/oxygen/robot4_interface.py @@ -289,6 +289,9 @@ def ms_to_timestamp(self, milliseconds): tz_delta = self.get_timezone_delta() time_object = datetime.fromtimestamp(int(milliseconds / 1000)) - tz_delta + if time_object.year < 1970: + time_object = datetime.fromtimestamp(0) + # fromtimestamp() loses milliseconds, add them back milliseconds_delta = timedelta(milliseconds=(milliseconds % 1000)) time_object = (time_object + milliseconds_delta) diff --git a/tests/utest/robot_interface/test_time_conversions.py b/tests/utest/robot_interface/test_time_conversions.py index 2d96e3d..660c2bb 100644 --- a/tests/utest/robot_interface/test_time_conversions.py +++ b/tests/utest/robot_interface/test_time_conversions.py @@ -29,6 +29,17 @@ def test_should_be_associative(self): timestamp = self.interface.result.ms_to_timestamp(milliseconds) assert timestamp == '20180807 07:01:24.300000' + def _validate_timestamp(self, result): + timestamp = result.ms_to_timestamp(-10) + self.assertEqual(timestamp, '19700101 02:00:00.990000') + + def test_ms_before_epoch_are_reset_to_epoch(self): + from oxygen.robot4_interface import RobotResultInterface as RF4ResultIface + self._validate_timestamp(RF4ResultIface()) + + from oxygen.robot3_interface import RobotResultInterface as RF3ResultIface + self._validate_timestamp(RF3ResultIface()) + class TestTimestampToMs(TestCase): def setUp(self):