{"record":{"id":"d1e5d14b561e020c","repo":"python/cpython","slug":"timedelta-of-days-is-too-large-d","errorCode":null,"errorMessage":"timedelta # of days is too large: %d","messagePattern":"timedelta # of days is too large: (.+?)","errorType":"exception","errorClass":"OverflowError","httpStatus":null,"severity":"error","filePath":"Lib/_pydatetime.py","lineNumber":751,"sourceCode":"            s += seconds\n            microseconds = round(microseconds + usdouble)\n        assert isinstance(s, int)\n        assert isinstance(microseconds, int)\n        assert abs(s) <= 3 * 24 * 3600\n        assert abs(microseconds) < 3.1e6\n\n        # Just a little bit of carrying possible for microseconds and seconds.\n        seconds, us = divmod(microseconds, 1000000)\n        s += seconds\n        days, s = divmod(s, 24*3600)\n        d += days\n\n        assert isinstance(d, int)\n        assert isinstance(s, int) and 0 <= s < 24*3600\n        assert isinstance(us, int) and 0 <= us < 1000000\n\n        if abs(d) > 999999999:\n            raise OverflowError(\"timedelta # of days is too large: %d\" % d)\n\n        self = object.__new__(cls)\n        self._days = d\n        self._seconds = s\n        self._microseconds = us\n        self._hashcode = -1\n        return self\n\n    def __repr__(self):\n        args = []\n        if self._days:\n            args.append(\"days=%d\" % self._days)\n        if self._seconds:\n            args.append(\"seconds=%d\" % self._seconds)\n        if self._microseconds:\n            args.append(\"microseconds=%d\" % self._microseconds)\n        if not args:\n            args.append('0')","sourceCodeStart":733,"sourceCodeEnd":769,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydatetime.py#L733-L769","documentation":"Raised by timedelta.__new__ after normalization when the absolute day count |d| exceeds 999999999. timedelta's internal representation bounds days to ±999999999 (about ±2.7 million years); any combination of components (including weeks*7 or multiplication later via __mul__) that normalizes beyond that raises OverflowError.","triggerScenarios":"timedelta(days=10**9); timedelta(weeks=10**9); timedelta(hours=1) * 10**9; accumulating timedeltas in a loop that multiplies a large factor.","commonSituations":"Multiplying durations by huge counts (e.g. timedelta(seconds=1) * ns_per_century); unit errors passing microseconds where days expected; computing date.max + big delta via timedelta arithmetic.","solutions":["Check the magnitude before constructing: keep |days| <= 999999999","Catch OverflowError when multiplying or exponentiating timedeltas","Switch to relative-date arithmetic on date objects within 1..9999 instead of giant timedeltas"],"exampleFix":"// before\nd = timedelta(microseconds=1) * 10**17  # OverflowError\n// after\ntry:\n    d = timedelta(microseconds=1) * factor\nexcept OverflowError:\n    d = timedelta.max  # or handle out-of-range explicitly","handlingStrategy":"try-catch","validationCode":"if abs(total_days) > 999_999_999:\n    raise OverflowError('duration out of representable range')","typeGuard":null,"tryCatchPattern":"try:\n    d = base_delta * factor\nexcept OverflowError:\n    d = timedelta.max  # or clamp / report\nelse:\n    use(d)","preventionTips":["Bound multiplication factors before scaling timedeltas","Prefer date arithmetic within year 1..9999 over giant timedeltas","Catch OverflowError wherever user input scales a duration"],"tags":["datetime","overflowerror","timedelta","arithmetic"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}