Skip to content

Commit 891ac09

Browse files
committed
Add checks for edge cases in datetime.timestamp and datetime.astimezone methods
1 parent c9932a9 commit 891ac09

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

Lib/_pydatetime.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1990,7 +1990,10 @@ def _mktime(self):
19901990
t = (self - epoch) // timedelta(0, 1)
19911991
def local(u):
19921992
y, m, d, hh, mm, ss = _time.localtime(u)[:6]
1993-
return (datetime(y, m, d, hh, mm, ss) - epoch) // timedelta(0, 1)
1993+
try:
1994+
return (datetime(y, m, d, hh, mm, ss) - epoch) // timedelta(0, 1)
1995+
except (ValueError, OverflowError):
1996+
return u
19941997

19951998
# Our goal is to solve t = local(u) for u.
19961999
a = local(t) - t

Modules/_datetimemodule.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5415,6 +5415,7 @@ local(long long u)
54155415
{
54165416
struct tm local_time;
54175417
time_t t;
5418+
long long old_u = u;
54185419
u -= epoch;
54195420
t = u;
54205421
if (t != u) {
@@ -5424,7 +5425,13 @@ local(long long u)
54245425
}
54255426
if (_PyTime_localtime(t, &local_time) != 0)
54265427
return -1;
5427-
return utc_to_seconds(local_time.tm_year + 1900,
5428+
5429+
// Check edge cases
5430+
int year = local_time.tm_year + 1900;
5431+
if (year < MINYEAR || year > MAXYEAR) {
5432+
return old_u;
5433+
}
5434+
return utc_to_seconds(year,
54285435
local_time.tm_mon + 1,
54295436
local_time.tm_mday,
54305437
local_time.tm_hour,

0 commit comments

Comments
 (0)