From 168426a4c6a347062b9099ce024ef70184ab706d Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sat, 5 Nov 2022 17:34:44 -0500 Subject: [PATCH] Make the 'is tai' check not depend on object identity .. because the identity of the 'tai' object can be lost when the multiprocessing module gets involved. --- leapseconddata/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/leapseconddata/__init__.py b/leapseconddata/__init__.py index 9dc5142..4101b08 100755 --- a/leapseconddata/__init__.py +++ b/leapseconddata/__init__.py @@ -59,6 +59,11 @@ def _from_ntp_epoch(value: int) -> datetime.datetime: return NTP_EPOCH + datetime.timedelta(seconds=value) +def datetime_is_tai(when: datetime.datetime) -> bool: + """Return true if the datetime is in the TAI timescale""" + return when.tzname() == "TAI" + + @dataclass(frozen=True) class LeapSecondData: """Represent the list of known and scheduled leapseconds @@ -113,7 +118,7 @@ class LeapSecondData: the offset of the last list entry. """ - is_tai = when.tzinfo is tai + is_tai = datetime_is_tai(when) if not is_tai: when = self._utc_datetime(when) if check_validity: @@ -142,7 +147,7 @@ class LeapSecondData: :param check_validity: Check whether the database is valid for the given moment Naive timestamps are assumed to be UTC. A TAI timestamp is returned unchanged.""" - if when.tzinfo is tai: + if datetime_is_tai(when): return when when = self._utc_datetime(when) return (when + self.tai_offset(when, check_validity)).replace(tzinfo=tai)