Skip to content

Commit 5d9db9d

Browse files
committed
Reuse fromtimestamp for 0 values, and timedelta() instead of casting time_t to int.
1 parent 057dd79 commit 5d9db9d

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

Lib/_pydatetime.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,9 +1039,7 @@ def fromtimestamp(cls, t):
10391039
raise TypeError("'NoneType' object cannot be interpreted as an integer")
10401040
if t < 0 and os.name == 'nt':
10411041
# Windows converters throw an OSError for negative values.
1042-
y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(0)
1043-
result = cls(y, m, d)
1044-
return result + timedelta(seconds=t)
1042+
return cls.fromtimestamp(0) + timedelta(seconds=t)
10451043
y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
10461044
return cls(y, m, d)
10471045

@@ -1859,6 +1857,10 @@ def _fromtimestamp(cls, t, utc, tz):
18591857
18601858
A timezone info object may be passed in as well.
18611859
"""
1860+
if t < 0 and os.name == 'nt':
1861+
# Windows converters throw an OSError for negative values.
1862+
return cls._fromtimestamp(0, utc, tz) + timedelta(seconds=t)
1863+
18621864
frac, t = _math.modf(t)
18631865
us = round(frac * 1e6)
18641866
if us >= 1000000:
@@ -1869,11 +1871,6 @@ def _fromtimestamp(cls, t, utc, tz):
18691871
us += 1000000
18701872

18711873
converter = _time.gmtime if utc else _time.localtime
1872-
if t < 0 and os.name == 'nt':
1873-
# Windows converters throw an OSError for negative values.
1874-
y, m, d, hh, mm, ss, weekday, jday, dst = converter(0)
1875-
result = cls(y, m, d, hh, mm, ss, 0, tz)
1876-
return result + timedelta(seconds=t, microseconds=us)
18771874
y, m, d, hh, mm, ss, weekday, jday, dst = converter(t)
18781875
ss = min(ss, 59) # clamp out leap seconds if the platform has them
18791876
result = cls(y, m, d, hh, mm, ss, us, tz)

Modules/_datetimemodule.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,16 +3262,13 @@ date_fromtimestamp(PyObject *cls, PyObject *obj)
32623262
if (_PyTime_localtime(0, &tm) != 0)
32633263
return NULL;
32643264

3265-
int normalize = 1, negate = 0;
3266-
PyObject *date = new_date_subclass_ex(tm.tm_year + 1900,
3267-
tm.tm_mon + 1,
3268-
tm.tm_mday,
3269-
cls);
3265+
int negate = 0;
3266+
PyObject *date = date_fromtimestamp(cls, _PyLong_GetZero());
32703267
if (date == NULL) {
32713268
return NULL;
32723269
}
32733270
PyObject *result = NULL;
3274-
PyObject *delta = new_delta(0, (int)t, 0, normalize);
3271+
PyObject *delta = PyObject_CallFunction((PyObject*)&DELTA_TYPE(NO_STATE), "iO", 0, obj);
32753272
if (delta == NULL) {
32763273
Py_DECREF(date);
32773274
return NULL;
@@ -5565,13 +5562,13 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp,
55655562

55665563
#ifdef MS_WINDOWS
55675564
if (timet < 0) {
5568-
int normalize = 1, factor = 1;
5565+
int factor = 1;
55695566
PyObject *dt = datetime_from_timet_and_us(cls, f, 0, 0, tzinfo);
55705567
if (dt == NULL) {
55715568
return NULL;
55725569
}
55735570
PyObject *result = NULL;
5574-
PyObject *delta = new_delta(0, (int)timet, us, normalize);
5571+
PyObject *delta = PyObject_CallFunction((PyObject*)&DELTA_TYPE(NO_STATE), "iO", 0, timestamp);
55755572
if (delta == NULL) {
55765573
Py_DECREF(dt);
55775574
return NULL;

0 commit comments

Comments
 (0)