From 0cc9646e2af31c08e15b344384815428a8d0f496 Mon Sep 17 00:00:00 2001 From: chr-sy Date: Thu, 27 Apr 2023 09:38:17 +0200 Subject: [PATCH 1/2] Update v4_blocks.py fixed local timestamps --- src/asammdf/blocks/v4_blocks.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/asammdf/blocks/v4_blocks.py b/src/asammdf/blocks/v4_blocks.py index b933bd89a..f1c7be6c0 100644 --- a/src/asammdf/blocks/v4_blocks.py +++ b/src/asammdf/blocks/v4_blocks.py @@ -5707,13 +5707,17 @@ def start_time(self) -> datetime: """ timestamp = self.abs_time / 10**9 + tz_local = False if self.time_flags & v4c.FLAG_HD_LOCAL_TIME: - tz = dateutil.tz.tzlocal() + tz = timezone.utc + tz_local = True else: tz = timezone(timedelta(minutes=self.tz_offset + self.daylight_save_time)) try: timestamp = datetime.fromtimestamp(timestamp, tz) + if tz_local: + timestamp = timestamp.replace(tzinfo=None) except OverflowError: timestamp = datetime.fromtimestamp(0, tz) + timedelta(seconds=timestamp) @@ -5724,7 +5728,7 @@ def start_time(self) -> datetime: def start_time(self, timestamp: datetime) -> None: if timestamp.tzinfo is None: self.time_flags = v4c.FLAG_HD_LOCAL_TIME - self.abs_time = int(timestamp.timestamp() * 10**9) + self.abs_time = int(timestamp.replace(tzinfo=timezone.utc).timestamp() * 10**9) self.tz_offset = 0 self.daylight_save_time = 0 From c89e87d4b8d843ce9355f0c238ea407cc15db795 Mon Sep 17 00:00:00 2001 From: chr-sy Date: Thu, 27 Apr 2023 11:05:55 +0200 Subject: [PATCH 2/2] Update v4_blocks.py changed timestamp calculation for local times in file history block, fixed string representation of start_time --- src/asammdf/blocks/v4_blocks.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/asammdf/blocks/v4_blocks.py b/src/asammdf/blocks/v4_blocks.py index f1c7be6c0..de90a3eaa 100644 --- a/src/asammdf/blocks/v4_blocks.py +++ b/src/asammdf/blocks/v4_blocks.py @@ -5448,13 +5448,17 @@ def time_stamp(self) -> datetime: """ timestamp = self.abs_time / 10**9 + tz_local = False if self.time_flags & v4c.FLAG_HD_LOCAL_TIME: - tz = dateutil.tz.tzlocal() + tz_local = True + tz = timezone.utc else: tz = timezone(timedelta(minutes=self.tz_offset + self.daylight_save_time)) try: timestamp = datetime.fromtimestamp(timestamp, tz) + if tz_local: + timestamp = timestamp.replace(tzinfo=None) except OverflowError: timestamp = datetime.fromtimestamp(0, tz) + timedelta(seconds=timestamp) @@ -5465,7 +5469,7 @@ def time_stamp(self) -> datetime: def time_stamp(self, timestamp: datetime) -> None: if timestamp.tzinfo is None: self.time_flags = v4c.FLAG_HD_LOCAL_TIME - self.abs_time = int(timestamp.timestamp() * 10**9) + self.abs_time = int(timestamp.replace(tzinfo=timezone.utc).timestamp() * 10**9) self.tz_offset = 0 self.daylight_save_time = 0 @@ -5763,6 +5767,9 @@ def start_time_string(self): else: tzinfo = self.start_time.tzinfo + if tzinfo is None: + return f'local time = {self.start_time.strftime("%d-%b-%Y %H:%M:%S + %fu")} (no timezone info available)' + dst = tzinfo.dst(self.start_time) if dst is not None: dst = int(tzinfo.dst(self.start_time).total_seconds() / 3600)