From b7d40d170248fb2419d068c6b0647ed262e3427f Mon Sep 17 00:00:00 2001 From: Alexander Belopolsky Date: Tue, 11 Jan 2011 01:21:25 +0000 Subject: [PATCH] Issue #1726687: time.mktime() will now correctly compute value one second before epoch. Original patch by Peter Wang, reported by Martin Blais. --- Lib/test/test_time.py | 13 +++++++++++++ Modules/timemodule.c | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index baabfbbb0aa..d6492320107 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -339,6 +339,19 @@ def test_negative(self): self.assertEqual(self.yearstr(-1234), '-1234') self.assertEqual(self.yearstr(-123456), '-123456') + + def test_mktime(self): + # Issue #1726687 + for t in (-2, -1, 0, 1): + try: + tt = time.localtime(t) + except (OverflowError, ValueError): + pass + self.assertEqual(time.mktime(tt), t) + # Hopefully year = -1 is enough to make OS mktime fail + self.assertRaises(OverflowError, time.mktime, + (-1, 1, 1, 0, 0, 0, -1, -1, -1)) + class TestAsctimeAccept2dYear(_TestAsctimeYear, _Test2dYear): pass diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 2286006113d..3a12522050f 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -694,8 +694,11 @@ time_mktime(PyObject *self, PyObject *tup) time_t tt; if (!gettmarg(tup, &buf)) return NULL; + buf.tm_wday = -1; /* sentinel; original value ignored */ tt = mktime(&buf); - if (tt == (time_t)(-1)) { + /* Return value of -1 does not necessarily mean an error, but tm_wday + * cannot remain set to -1 if mktime succedded. */ + if (tt == (time_t)(-1) && buf.tm_wday == -1) { PyErr_SetString(PyExc_OverflowError, "mktime argument out of range"); return NULL;