[issue14653] Improve mktime_tz to use calendar.timegm instead of time.mktime

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[issue14653] Improve mktime_tz to use calendar.timegm instead of time.mktime

STINNER Victor

New submission from Mitar <[hidden email]>:

I would suggest improvement of mktime_tz to use calendar.timegm internally instead of time.mktime. The problem is that on Windows mktime_tz fails with "mktime argument out of range" for this code:

mktime_tz(parsedate_tz('Thu, 1 Jan 1970 00:00:00 GMT'))

if user is in GMT+X timezone. Obviously, "Thu, 1 Jan 1970 00:00:00 GMT" is not out of range. But because mktime_tz uses internally time.mktime which takes into the account local time (and local timezone) and then compensate for the timeline, out of range condition happens. I would suggest such implementation:

def mktime_tz(data):
    """Turn a 10-tuple as returned by parsedate_tz() into a UTC timestamp."""
    if data[9] is None:
        # No zone info, so localtime is better assumption than GMT
        return time.mktime(data[:8] + (-1,))
    else:
        t = calendar.timegm(data[:8] + (0,))
        return t - data[9]

It does not raise and exception, and it is also much cleaner: directly using GMT function and not localtime with timezone compensation.

----------
components: Library (Lib)
messages: 159074
nosy: mitar
priority: normal
severity: normal
status: open
title: Improve mktime_tz to use calendar.timegm instead of time.mktime
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14653>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%2B1322467933539-512619%40n6.nabble.com

Reply | Threaded
Open this post in threaded view
|

[issue14653] Improve mktime_tz to use calendar.timegm instead of time.mktime

STINNER Victor

R. David Murray <[hidden email]> added the comment:

I think that what is going to happen is that both of these functions are going to be deprecated in favor of functions that use datetimes.

That said, this might be a worthwhile as a bug fix.  I'm adding Alexander as nosy to see what he thinks.  (mktime_tz is located in email.utils, with the source in Lib/email/_parseaddr.py).

----------
assignee:  -> r.david.murray
nosy: +belopolsky, r.david.murray
versions: +Python 3.2, Python 3.3

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14653>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/lists%2B1322467933539-512619%40n6.nabble.com