classification
Title: mktime - OverflowError: mktime argument out of range - on very specific time
Type: behavior Stage: test needed
Components: Extension Modules Versions: Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: belopolsky Nosy List: JoeKuan, belopolsky, haypo, lemburg
Priority: normal Keywords:

Created on 2011-04-15 11:11 by JoeKuan, last changed 2011-04-15 14:04 by lemburg. This issue is now closed.

Messages (9)
msg133806 - (view) Author: (JoeKuan) Date: 2011-04-15 11:11
>>> a = (1970, 1, 1, 0, 59, 58, 0, 0, 0)
>>> time.mktime(a)
-2.0

>>> a = (1970, 1, 1, 0, 59, 59, 0, 0, 0)
>>> time.mktime(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: mktime argument out of range

>>> a = (1970, 1, 1, 1, 0, 0, 0, 0, 0)
>>> time.mktime(a)
0.0

>>> a = (1970, 1, 1, 0, 59, 60, 0, 0, 0)
>>> time.mktime(a)
0.0
msg133807 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2011-04-15 11:35
JoeKuan wrote:
> 
> New submission from JoeKuan <kuan.joe@gmail.com>:
> 
>>>> a = (1970, 1, 1, 0, 59, 58, 0, 0, 0)
>>>> time.mktime(a)
> -2.0

On Windows, you get an OverflowError for this tuple as well.

>>>> a = (1970, 1, 1, 0, 59, 59, 0, 0, 0)
>>>> time.mktime(a)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> OverflowError: mktime argument out of range
> 
>>>> a = (1970, 1, 1, 1, 0, 0, 0, 0, 0)
>>>> time.mktime(a)
> 0.0
> 
>>>> a = (1970, 1, 1, 0, 59, 60, 0, 0, 0)
>>>> time.mktime(a)
> 0.0

Note that time.mktime() is direct interface to the C lib
funtion of the same name. As a result, the support for
the various values is platform dependent. In general,
dates before the epoch tend not to work or give wrong
results.

Since mktime() works on local time, the time zone
in affect on 1970-01-01 matters and that's why you are
seeing the OverflowError even for values after
1970-01-01 00:00:00.
msg133814 - (view) Author: (JoeKuan) Date: 2011-04-15 12:39
I don't think it is to do with the underlying C mktime. Because it works fine with 00:59:58 and 01:00:00, 1, Jan 1970. It is to do with some specific value -1 in the internal code of time.mktime

Here is the C code.

int main(int argc, char *argv[]) {

        struct tm aTime = { 58, 59, 0, 1, 0, 70, 0, 0, 0, 0 };
        time_t mTime = mktime(&aTime);
        printf("%s\n", ctime(&mTime));

        aTime.tm_sec = 59;
        mTime = mktime(&aTime);
        printf("%s\n", ctime(&mTime));

        aTime.tm_sec = 0;
        aTime.tm_min = 0;
        aTime.tm_hour = 1;
        mTime = mktime(&aTime);
        printf("%s\n", ctime(&mTime));
}

-------------------------------------------------------
Output from the same machine which gives the python error message


Thu Jan  1 00:59:58 1970

Thu Jan  1 00:59:59 1970

Thu Jan  1 01:00:00 1970
msg133815 - (view) Author: Alexander Belopolsky (Alexander.Belopolsky) Date: 2011-04-15 12:44
Isn't this a duplicate of issue1726687?

>
msg133816 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2011-04-15 13:10
JoeKuan wrote:
> 
> JoeKuan <kuan.joe@gmail.com> added the comment:
> 
> I don't think it is to do with the underlying C mktime. Because it works fine with 00:59:58 and 01:00:00, 1, Jan 1970. It is to do with some specific value -1 in the internal code of time.mktime

Here's the module code:

    buf.tm_wday = -1;  /* sentinel; original value ignored */
    tt = mktime(&buf);
    /* 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;
    }

This is correct by the books, since the Linux man-page states (the
POSIX page is similar):

"""
       The  mktime()  function  modifies  the fields of the tm structure as follows:
       tm_wday and tm_yday are set to values determined from  the  contents  of  the
       other  fields;  if  structure  members are outside their valid interval, they
       will be normalized (so that, for example, 40 October is changed into 9 Novem-
       ber);  tm_isdst  is set (regardless of its initial value) to a positive value
       or to 0, respectively, to indicate whether DST is or is not in effect at  the
       specified time.  Calling mktime() also sets the external variable tzname with
       information about the current timezone.

       If the specified broken-down time cannot  be  represented  as  calendar  time
       (seconds  since  the Epoch), mktime() returns a value of (time_t) -1 and does
       not alter the members of the broken-down time structure.
"""

On which platform are you trying this ?

> Here is the C code.
> 
> int main(int argc, char *argv[]) {
> 
>         struct tm aTime = { 58, 59, 0, 1, 0, 70, 0, 0, 0, 0 };
>         time_t mTime = mktime(&aTime);
>         printf("%s\n", ctime(&mTime));
> 
>         aTime.tm_sec = 59;
>         mTime = mktime(&aTime);
>         printf("%s\n", ctime(&mTime));
> 
>         aTime.tm_sec = 0;
>         aTime.tm_min = 0;
>         aTime.tm_hour = 1;
>         mTime = mktime(&aTime);
>         printf("%s\n", ctime(&mTime));
> }
> 
> -------------------------------------------------------
> Output from the same machine which gives the python error message
> 
> 
> Thu Jan  1 00:59:58 1970
> 
> Thu Jan  1 00:59:59 1970
> 
> Thu Jan  1 01:00:00 1970

On Windows, you get errors for the first two.
msg133817 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2011-04-15 13:17
Alexander Belopolsky wrote:
> 
> Alexander Belopolsky <alexander.belopolsky@gmail.com> added the comment:
> 
> Isn't this a duplicate of issue1726687?

Could be, but that patch is not yet in Python 2.7, since Python 2.7.1
was release in Nov 2010.
msg133818 - (view) Author: Alexander Belopolsky (belopolsky) (Python committer) Date: 2011-04-15 13:27
If we can rely on the "versions" field, OP is using python 2.6.  I don't think this can be classified as a security issue, so it won't be appropriate to backport issue1726687 to 2.6.
msg133819 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2011-04-15 13:29
This issue is a duplicate of #1726687 which is already fixed in Python 2.7 by 7a89f4a73d1a (Feb 15 2011): it will be part of 2.7.2.

Only security vulnerabilities are fixed in Python 2.6, so I change the version field to 2.7 only.
msg133826 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2011-04-15 14:04
Marc-Andre Lemburg wrote:
> 
> Marc-Andre Lemburg <mal@egenix.com> added the comment:
> 
> Alexander Belopolsky wrote:
>>
>> Alexander Belopolsky <alexander.belopolsky@gmail.com> added the comment:
>>
>> Isn't this a duplicate of issue1726687?
> 
> Could be, but that patch is not yet in Python 2.7, since Python 2.7.1
> was release in Nov 2010.

FWIW: The fix does work on Linux for the mentioned special case.

Aside: mxDateTime will have a similar fix in the next release and
that will also be available for Python 2.6.
History
Date User Action Args
2011-04-15 14:04:58lemburgsetmessages: + msg133826
2011-04-15 13:29:17hayposetstatus: open -> closed
2011-04-15 13:29:01hayposetversions: + Python 2.7, - Python 2.6
nosy: + haypo

messages: + msg133819

resolution: duplicate
2011-04-15 13:27:41belopolskysetassignee: belopolsky
type: behavior
components: + Extension Modules

nosy: - Alexander.Belopolsky
messages: + msg133818
stage: test needed
2011-04-15 13:17:16lemburgsetmessages: + msg133817
2011-04-15 13:10:48lemburgsetmessages: + msg133816
title: mktime - OverflowError: mktime argument out of range - on very specific time -> mktime - OverflowError: mktime argument out of range - on very specific time
2011-04-15 12:44:10Alexander.Belopolskysetnosy: + Alexander.Belopolsky
title: mktime - OverflowError: mktime argument out of range - on very specific time -> mktime - OverflowError: mktime argument out of range - on very specific time
messages: + msg133815
2011-04-15 12:39:12JoeKuansetmessages: + msg133814
2011-04-15 11:35:52lemburgsetnosy: + lemburg
title: mktime - OverflowError: mktime argument out of range - on very specific time -> mktime - OverflowError: mktime argument out of range - on very specific time
messages: + msg133807
2011-04-15 11:24:49ezio.melottisetnosy: + belopolsky
2011-04-15 11:11:18JoeKuancreate