Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update v4_blocks.py #857

Merged
merged 2 commits into from
May 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/asammdf/blocks/v4_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -5707,13 +5711,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 @@ -5724,7 +5732,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 @@ -5759,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)
Expand Down