-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
gh-80620: Support negative values in fromtimestamp on Windows using 0 + timedelta
#134461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jhohm
wants to merge
14
commits into
python:main
Choose a base branch
from
jhohm:gh-80620
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−25
Open
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6704876
Support negative values in `fromtimestamp` on Windows using 0 + `time…
jhohm a3e5749
Fixes from actually building on Windows.
jhohm 7453c3c
Support negative integer timestamps too; skip tests that would genera…
jhohm 15c5520
Avoid double-adding microseconds in _pydatetime datetime.fromtimestamp.
jhohm 329db66
Untabify _datetimemodule.c
jhohm 34c999f
Free intermediate objects in _datetimemodule.
jhohm 0dd521f
Reorder initialization to be consistent between added code.
jhohm 44bcfd9
Handle all errors; switch to more common os.name == 'nt' check
jhohm e675d63
Explain in news that negative timestamps are dates before the epoch.
jhohm 02082f7
Fix OS check to import os.
jhohm cb2f911
Untabify; copy error handling code instead of goto.
jhohm 057dd79
No need to XDECREF non-NULL objects.
jhohm 5d9db9d
Reuse `fromtimestamp` for 0 values, and `timedelta()` instead of cast…
jhohm 77a8b11
Merge branch 'main' into gh-80620
jhohm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Windows/2025-05-21-12-29-59.gh-issue-80620.WKel4J.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Support negative values in :meth:`datetime.date.fromtimestamp` and | ||
| :meth:`datetime.datetime.fromtimestamp` on Windows, by substituting 0 and | ||
jhohm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| using :class:`datetime.timedelta` to go back in time. Patch by John Keith | ||
| Hohm. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3241,6 +3241,13 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw) | |||||||||||
| return self; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, | ||||||||||||
| PyDateTime_Delta *delta, | ||||||||||||
| int factor); | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove the duplicated definition line 3762? Can you also move these two definitions at line 154? In the "Forward declarations" block. |
||||||||||||
| static PyObject * | ||||||||||||
| add_date_timedelta(PyDateTime_Date *date, PyDateTime_Delta *delta, int negate); | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| static PyObject * | ||||||||||||
| date_fromtimestamp(PyObject *cls, PyObject *obj) | ||||||||||||
| { | ||||||||||||
|
|
@@ -3250,6 +3257,24 @@ date_fromtimestamp(PyObject *cls, PyObject *obj) | |||||||||||
| if (_PyTime_ObjectToTime_t(obj, &t, _PyTime_ROUND_FLOOR) == -1) | ||||||||||||
| return NULL; | ||||||||||||
|
|
||||||||||||
| #ifdef MS_WINDOWS | ||||||||||||
| if (t < 0) { | ||||||||||||
| if (_PyTime_localtime(0, &tm) != 0) | ||||||||||||
| return NULL; | ||||||||||||
|
Comment on lines
+3262
to
+3263
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
|
||||||||||||
| int normalize = 1, negate = 0; | ||||||||||||
| PyObject *date = new_date_subclass_ex(tm.tm_year + 1900, | ||||||||||||
| tm.tm_mon + 1, | ||||||||||||
| tm.tm_mday, | ||||||||||||
| cls); | ||||||||||||
jhohm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| PyObject *delta = new_delta(0, (int)t, 0, normalize); | ||||||||||||
jhohm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| PyObject *result = add_date_timedelta(PyDate_CAST(date), PyDelta_CAST(delta), negate); | ||||||||||||
jhohm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| Py_XDECREF(delta); | ||||||||||||
| Py_XDECREF(date); | ||||||||||||
jhohm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| return result; | ||||||||||||
| } | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| if (_PyTime_localtime(t, &tm) != 0) | ||||||||||||
| return NULL; | ||||||||||||
|
|
||||||||||||
|
|
@@ -4070,9 +4095,6 @@ tzinfo_dst(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(dt)) | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| static PyObject *add_datetime_timedelta(PyDateTime_DateTime *date, | ||||||||||||
| PyDateTime_Delta *delta, | ||||||||||||
| int factor); | ||||||||||||
| static PyObject *datetime_utcoffset(PyObject *self, PyObject *); | ||||||||||||
| static PyObject *datetime_dst(PyObject *self, PyObject *); | ||||||||||||
|
|
||||||||||||
|
|
@@ -5533,6 +5555,18 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, | |||||||||||
| &timet, &us, _PyTime_ROUND_HALF_EVEN) == -1) | ||||||||||||
| return NULL; | ||||||||||||
|
|
||||||||||||
| #ifdef MS_WINDOWS | ||||||||||||
| if (timet < 0) { | ||||||||||||
| int normalize = 1, factor = 1; | ||||||||||||
| PyObject *dt = datetime_from_timet_and_us(cls, f, 0, 0, tzinfo); | ||||||||||||
| PyObject *delta = new_delta(0, (int)timet, us, normalize); | ||||||||||||
jhohm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| PyObject *result = add_datetime_timedelta(PyDateTime_CAST(dt), PyDelta_CAST(delta), factor); | ||||||||||||
jhohm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| Py_XDECREF(delta); | ||||||||||||
| Py_XDECREF(dt); | ||||||||||||
| return result; | ||||||||||||
| } | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.