Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32k
gh-80620: Support negative values infromtimestamp
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
6704876
a3e5749
7453c3c
15c5520
329db66
34c999f
0dd521f
44bcfd9
e675d63
02082f7
cb2f911
057dd79
5d9db9d
77a8b11
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Support negative values (dates before the UNIX epoch of 1970-01-01) in | ||
:meth:`datetime.date.fromtimestamp` and | ||
:meth:`datetime.datetime.fromtimestamp` on Windows, by substituting 0 | ||
and using :class:`datetime.timedelta` to go back in time. Patch by | ||
John Keith Hohm. |
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,29 @@ 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 negate = 0; | ||||||||||||
PyObject *date = date_fromtimestamp(cls, _PyLong_GetZero()); | ||||||||||||
if (date == NULL) { | ||||||||||||
return NULL; | ||||||||||||
} | ||||||||||||
PyObject *result = NULL; | ||||||||||||
PyObject *delta = PyObject_CallFunction((PyObject*)&DELTA_TYPE(NO_STATE), "iO", 0, obj); | ||||||||||||
if (delta == NULL) { | ||||||||||||
Py_DECREF(date); | ||||||||||||
return NULL; | ||||||||||||
} | ||||||||||||
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. You can move negate and result definitions here. There is no need to initialize result to NULL. | ||||||||||||
result = add_date_timedelta(PyDate_CAST(date), PyDelta_CAST(delta), negate); | ||||||||||||
Py_DECREF(delta); | ||||||||||||
Py_DECREF(date); | ||||||||||||
return result; | ||||||||||||
} | ||||||||||||
#endif | ||||||||||||
if (_PyTime_localtime(t, &tm) != 0) | ||||||||||||
return NULL; | ||||||||||||
@@ -4070,9 +4100,6 @@ tzinfo_dst(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(dt)) | ||||||||||||
} | ||||||||||||
static PyObject *datetime_utcoffset(PyObject *self, PyObject *); | ||||||||||||
static PyObject *datetime_dst(PyObject *self, PyObject *); | ||||||||||||
@@ -5533,6 +5560,26 @@ 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 factor = 1; | ||||||||||||
PyObject *dt = datetime_from_timet_and_us(cls, f, 0, 0, tzinfo); | ||||||||||||
if (dt == NULL) { | ||||||||||||
return NULL; | ||||||||||||
} | ||||||||||||
PyObject *result = NULL; | ||||||||||||
PyObject *delta = PyObject_CallFunction((PyObject*)&DELTA_TYPE(NO_STATE), "iO", 0, timestamp); | ||||||||||||
if (delta == NULL) { | ||||||||||||
Py_DECREF(dt); | ||||||||||||
return NULL; | ||||||||||||
} | ||||||||||||
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. You can move result definition here. There is no need to initialize it to NULL. | ||||||||||||
result = add_datetime_timedelta(PyDateTime_CAST(dt), PyDelta_CAST(delta), factor); | ||||||||||||
Py_DECREF(delta); | ||||||||||||
Py_DECREF(dt); | ||||||||||||
return result; | ||||||||||||
} | ||||||||||||
#endif | ||||||||||||
return datetime_from_timet_and_us(cls, f, timet, (int)us, tzinfo); | ||||||||||||
} | ||||||||||||
Uh oh!
There was an error while loading.Please reload this page.