Skip to content

Commit

Permalink
Merge pull request #857 from chr-sy/tz_conversion
Browse files Browse the repository at this point in the history
Update v4_blocks.py
  • Loading branch information
danielhrisca authored May 21, 2024
2 parents 75e9b69 + c89e87d commit e27b4b7
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/asammdf/blocks/v4_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5458,13 +5458,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)
Expand All @@ -5475,7 +5479,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

Expand Down Expand Up @@ -5747,13 +5751,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)
Expand All @@ -5764,7 +5772,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

Expand Down Expand Up @@ -5799,6 +5807,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)
Expand Down

0 comments on commit e27b4b7

Please sign in to comment.