]> Cypherpunks repositories - keks.git/commitdiff
Simpler and more robust datetime overflow check
authorSergey Matveev <stargrave@stargrave.org>
Wed, 4 Dec 2024 13:17:07 +0000 (16:17 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 4 Dec 2024 13:17:07 +0000 (16:17 +0300)
pyac/pyac.py

index 4cb9c5321e70acd3bf772a11f3b21d617581ee3f3e67b198f8d16e33026bb7aa..5502d16e15c71929c34aa59d676a4c22647620d56801934d47f0c6f56b6371b4 100755 (executable)
@@ -317,10 +317,13 @@ def loads(v, sets=False, leapsecUTCAllow=False):
         secs = tai2utc(secs - TAI64Base, leapsecUTCAllow)
         if secs is None:
             return Raw(v[0], v[1:1+l]), v[1+l:]
-        if (abs(secs) > (1 << 60)) or (asecs > 0) or ((nsecs % 1000) > 0):
-            # Python can represent neither big values, nor nanoseconds
+        if (asecs > 0) or ((nsecs % 1000) > 0):
+            # Python can represent neither attoseconds, nor nanoseconds
+            return Raw(v[0], v[1:1+l]), v[1+l:]
+        try:
+            dt = datetime(1970, 1, 1) + timedelta(seconds=secs)
+        except OverflowError:
             return Raw(v[0], v[1:1+l]), v[1+l:]
-        dt = datetime(1970, 1, 1) + timedelta(seconds=secs)
         dt += timedelta(microseconds=nsecs // 1000)
         return dt, v[1+l:]
     if (v[0] & TagStr) > 0: