Repository navigation
gh-159739: Preserve large years in time conversions #160175
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
jelle-openai
wants to merge
2
commits into
python:main
Choose a base branch
from
jelle-openai:jelle/codex/gh-159739-time-large-year
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.
+66
−17
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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/Library/2026-10-22-03-55-33.gh-issue-159739.ce26e1.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 @@ | ||
| Fix :mod:`time` functions returning a negative year or failing to accept | ||
| their own output for large timestamps on platforms that support them. Avoid | ||
| reporting an incorrect year in errors raised by :mod:`datetime` for those | ||
| timestamps. |
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
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -487,7 +487,7 @@ tmtotuple(time_module_state *state, struct tm *p | |||||||
| #define SET(INDEX, VAL) \ | ||||||||
| SET_ITEM((INDEX), PyLong_FromLong((long) (VAL))) | ||||||||
|
|
||||||||
| SET(0, p->tm_year + 1900); | ||||||||
| SET_ITEM(0, PyLong_FromLongLong((long long)p->tm_year + 1900)); | ||||||||
|
Member
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. This fixes the forward direction, but Lines 908 to 910 in e60a9da
We should preserve the round trip. |
||||||||
| SET(1, p->tm_mon + 1); /* Want January == 1 */ | ||||||||
| SET(2, p->tm_mday); | ||||||||
| SET(3, p->tm_hour); | ||||||||
|
|
@@ -629,7 +629,7 @@ static int | |||||||
| gettmarg(time_module_state *state, PyObject *args, | ||||||||
| struct tm *p, const char *format) | ||||||||
| { | ||||||||
| int y; | ||||||||
| long long y; | ||||||||
|
|
||||||||
| memset((void *) p, '\0', sizeof(struct tm)); | ||||||||
|
|
||||||||
|
|
@@ -645,12 +645,12 @@ gettmarg(time_module_state *state, PyObject *args, | |||||||
| &p->tm_wday, &p->tm_yday, &p->tm_isdst)) | ||||||||
| return 0; | ||||||||
|
|
||||||||
| if (y < INT_MIN + 1900) { | ||||||||
| if (y < (long long)INT_MIN + 1900 || y > (long long)INT_MAX + 1900) { | ||||||||
| PyErr_SetString(PyExc_OverflowError, "year out of range"); | ||||||||
| return 0; | ||||||||
| } | ||||||||
|
|
||||||||
| p->tm_year = y - 1900; | ||||||||
| p->tm_year = (int)(y - 1900); | ||||||||
| p->tm_mon--; | ||||||||
| p->tm_wday = (p->tm_wday + 1) % 7; | ||||||||
| p->tm_yday--; | ||||||||
|
|
@@ -906,7 +906,7 @@ time_strftime(PyObject *module, PyObject *args) | |||||||
| return NULL; | ||||||||
| } | ||||||||
| else if (!gettmarg(state, tup, &buf, | ||||||||
| "iiiiiiiii;strftime(): illegal time tuple argument") || | ||||||||
| "Liiiiiiii;strftime(): illegal time tuple argument") || | ||||||||
| !checktm(&buf)) | ||||||||
| { | ||||||||
| return NULL; | ||||||||
|
|
@@ -918,7 +918,7 @@ time_strftime(PyObject *module, PyObject *args) | |||||||
| // physical devices (#123017). | ||||||||
| #if defined(_MSC_VER) || (defined(__sun) && defined(__SVR4)) || defined(_AIX) \ | ||||||||
| || defined(__VXWORKS__) || defined(__ANDROID__) | ||||||||
| if (buf.tm_year + 1900 < 1 || 9999 < buf.tm_year + 1900) { | ||||||||
| if (buf.tm_year < 1 - 1900 || buf.tm_year > 9999 - 1900) { | ||||||||
| PyErr_SetString(PyExc_ValueError, | ||||||||
| "strftime() requires year in [1; 9999]"); | ||||||||
| return NULL; | ||||||||
|
|
@@ -1041,12 +1041,12 @@ _asctime(struct tm *timeptr) | |||||||
| "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" | ||||||||
| }; | ||||||||
| return PyUnicode_FromFormat( | ||||||||
| "%s %s%3d %.2d:%.2d:%.2d %d", | ||||||||
| "%s %s%3d %.2d:%.2d:%.2d %lld", | ||||||||
| wday_name[timeptr->tm_wday], | ||||||||
| mon_name[timeptr->tm_mon], | ||||||||
| timeptr->tm_mday, timeptr->tm_hour, | ||||||||
| timeptr->tm_min, timeptr->tm_sec, | ||||||||
| 1900 + timeptr->tm_year); | ||||||||
| (long long)timeptr->tm_year + 1900); | ||||||||
| } | ||||||||
|
|
||||||||
| /*[clinic input] | ||||||||
|
|
@@ -1074,7 +1074,7 @@ time_asctime_impl(PyObject *module, PyObject *tup) | |||||||
| return NULL; | ||||||||
| } | ||||||||
| else if (!gettmarg(state, tup, &buf, | ||||||||
| "iiiiiiiii;asctime(): illegal time tuple argument") || | ||||||||
| "Liiiiiiii;asctime(): illegal time tuple argument") || | ||||||||
| !checktm(&buf)) | ||||||||
| { | ||||||||
| return NULL; | ||||||||
|
|
@@ -1130,7 +1130,7 @@ time_mktime(PyObject *module, PyObject *tm_tuple) | |||||||
|
|
||||||||
| time_module_state *state = get_time_state(module); | ||||||||
| if (!gettmarg(state, tm_tuple, &tm, | ||||||||
| "iiiiiiiii;mktime(): illegal time tuple argument")) | ||||||||
| "Liiiiiiii;mktime(): illegal time tuple argument")) | ||||||||
| { | ||||||||
| return NULL; | ||||||||
| } | ||||||||
|
|
||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't
datetimehave the same issue?