From a676951568213938920bb68f24afc4f7c6a33172 Mon Sep 17 00:00:00 2001 From: chenpingchao Date: Mon, 26 Aug 2019 10:07:20 +0800 Subject: [PATCH 1/3] fixing the bug on datetime.timestamp() --- Lib/datetime.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Lib/datetime.py b/Lib/datetime.py index 0adf1dd67dfb637..309d96cc27b6353 100644 --- a/Lib/datetime.py +++ b/Lib/datetime.py @@ -1767,10 +1767,13 @@ def local(u): # We found one solution, but it may not be the one we need. # Look for an earlier solution (if `fold` is 0), or a # later one (if `fold` is 1). - u2 = u1 + (-max_fold_seconds, max_fold_seconds)[self.fold] - b = local(u2) - u2 - if a == b: + if u1 < max_fold_seconds: return u1 + else: + u2 = u1 + (-max_fold_seconds, max_fold_seconds)[self.fold] + b = local(u2) - u2 + if a == b: + return u1 else: b = t1 - u1 assert a != b From a5413324535a76b5b1b692655d37e73f03774008 Mon Sep 17 00:00:00 2001 From: chenpingchao Date: Sat, 7 Sep 2019 21:36:58 +0800 Subject: [PATCH 2/3] The test for datetime.timestamp() near the epoch --- test.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 test.py diff --git a/test.py b/test.py new file mode 100644 index 000000000000000..0562011196b6538 --- /dev/null +++ b/test.py @@ -0,0 +1,17 @@ +# datetime.timestamp() fails when the 'datetime' is close to the Epoch +# Here is the test runs on win10 os, python3.7-3.9. My time is 'utc +8' + +from datetime import datetime + +def test(year,month,day,hour=0,minute=0,second=0): + t = datetime(year,month,day,hour) + try: + print(t.timestamp()) + except Exception as e: + print(e) + return + +test(1970,1,1) +test(1970,1,1,8) +test(1970,1,2,7,59,59) +test(1970,1,2,8) # this time is just okay From d26d03c0745d22f39020308e98b2ac1484e49527 Mon Sep 17 00:00:00 2001 From: Oleg Iarygin Date: Wed, 22 Mar 2023 22:51:58 +0400 Subject: [PATCH 3/3] Add a news entry --- .../next/Library/2023-03-22-22-51-33.gh-issue-81708.nH0xss.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-22-22-51-33.gh-issue-81708.nH0xss.rst diff --git a/Misc/NEWS.d/next/Library/2023-03-22-22-51-33.gh-issue-81708.nH0xss.rst b/Misc/NEWS.d/next/Library/2023-03-22-22-51-33.gh-issue-81708.nH0xss.rst new file mode 100644 index 000000000000000..c3c267f30c135ee --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-22-22-51-33.gh-issue-81708.nH0xss.rst @@ -0,0 +1,2 @@ +:meth:`datetime.datetime.timestamp` with dates near The Unix Epoch no longer +raises ``OSError: [Errno 22] Invalid argument`` on Windows.