Skip to content

Commit

Permalink
fix negative timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Apr 22, 2024
1 parent cf3d8f8 commit 3a9470a
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/nd2/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,15 @@ def is_new_format(path: str) -> bool:
with open(path, "rb") as fh:
return fh.read(4) == NEW_HEADER_MAGIC

JDN_UNIX_EPOCH = 2440587.5
SECONDS_PER_DAY = 86400

def jdn_to_datetime(jdn: float, tz: timezone = timezone.utc) -> datetime:
# astimezone() without arguments will use the system's local timezone
t = (jdn - 2440587.5) * 86400.0
if os.name == "nt" and t < 0:
# Windows does not always support negative timestamps
dt = datetime.fromtimestamp(0, tz) + timedelta(seconds=t)
else:
dt = datetime.fromtimestamp(t, tz)
seconds_since_epoch = (jdn - JDN_UNIX_EPOCH) * SECONDS_PER_DAY
# very negative values can cause OverflowError on Windows, and are meaningless
dt = datetime.fromtimestamp(max(seconds_since_epoch, 0), tz)
with suppress(ValueError, OSError):
# astimezone() without arguments will use the system's local timezone
return dt.astimezone()
return dt

Expand Down

0 comments on commit 3a9470a

Please sign in to comment.