From: Sergey Matveev Date: Wed, 4 Dec 2024 13:17:07 +0000 (+0300) Subject: Simpler and more robust datetime overflow check X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=4e2eaeeb0879d314dd91affece352052ce76cc6b94a5eaeb3f497ff368b371af;p=keks.git Simpler and more robust datetime overflow check --- diff --git a/pyac/pyac.py b/pyac/pyac.py index 4cb9c53..5502d16 100755 --- a/pyac/pyac.py +++ b/pyac/pyac.py @@ -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: