https://github.com/python/cpython/commit/470941782f74288823b445120f6383914b659f23
commit: 470941782f74288823b445120f6383914b659f23
branch: main
author: John Keith Hohm <j...@hohm.net>
committer: vstinner <vstin...@python.org>
date: 2025-05-19T22:48:55+02:00
summary:

gh-88994: Change `datetime.datetime.now` to half-even rounding (#134258)

Change `datetime.datetime.now` to half-even rounding
for consistency with `datetime.fromtimestamp`.

files:
A Misc/NEWS.d/next/Library/2025-05-19-18-12-42.gh-issue-88994.7avvVu.rst
M Misc/ACKS
M Modules/_datetimemodule.c

diff --git a/Misc/ACKS b/Misc/ACKS
index 5653c52c9e354e..1b500870dec472 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -795,6 +795,7 @@ Albert Hofkamp
 Chris Hogan
 Tomas Hoger
 Jonathan Hogg
+John Keith Hohm
 Vladyslav Hoi
 Kamilla Holanda
 Steve Holden
diff --git 
a/Misc/NEWS.d/next/Library/2025-05-19-18-12-42.gh-issue-88994.7avvVu.rst 
b/Misc/NEWS.d/next/Library/2025-05-19-18-12-42.gh-issue-88994.7avvVu.rst
new file mode 100644
index 00000000000000..554a0c3bcb26bf
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-05-19-18-12-42.gh-issue-88994.7avvVu.rst
@@ -0,0 +1,3 @@
+Change :func:`datetime.datetime.now` to half-even rounding for
+consistency with :func:`datetime.datetime.fromtimestamp`.  Patch by
+John Keith Hohm.
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 313a72e3fe0668..eb90be81c8d34c 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -5551,8 +5551,9 @@ datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject 
*tzinfo)
     time_t secs;
     int us;
 
-    if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_FLOOR) < 0)
+    if (_PyTime_AsTimevalTime_t(ts, &secs, &us, _PyTime_ROUND_HALF_EVEN) < 0) {
         return NULL;
+    }
     assert(0 <= us && us <= 999999);
 
     return datetime_from_timet_and_us(cls, f, secs, us, tzinfo);

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to