[issue14157] time.strptime without a year fails on Feb 29

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor

New submission from Martin Morrison <[hidden email]>:

time.strptime without a year fails on Feb 29 with:

>>> time.strptime("Feb 29", "%b %d")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.6/_strptime.py", line 440, in _strptime
    datetime_date(year, 1, 1).toordinal() + 1
ValueError: day is out of range for month

This is due to the use of "1900" as the default year when parsing. It would be nice to have an optional "defaults" keyword argument to the strptime function that can be used to override the defaults, thus allowing leap year dates to be parsed without specifying the date.

(Note: the code in question attempted to set the year *after* the parse so that ultimately there is a valid struct_time, but since the parse never succeeds, this can't work).

----------
components: Library (Lib)
messages: 154621
nosy: Martin.Morrison
priority: normal
severity: normal
status: open
title: time.strptime without a year fails on Feb 29
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor

Changes by Antoine Pitrou <[hidden email]>:


----------
nosy: +belopolsky, haypo, hynek
versions: +Python 3.2, Python 3.3

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Alexander Belopolsky <[hidden email]> added the comment:

This strikes me as an implementation artifact.  There is no reason for time.strptime() to validate date triplets.  Applications that require valid dates can use datetime.strptime().  I suggest changing time.strptime() specification to match POSIX strptime().  My understanding is that POSIX only requires field by field range checking (%d range 01 to 31, %m range 01 to 12) and not full structure validation.  This would be consistent with the way leap seconds are currently treated:

>>> time.strptime('60', '%S')[5]
60

----------
assignee:  -> belopolsky
stage:  -> needs patch
type: behavior -> enhancement

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Shawn <[hidden email]> added the comment:

I'm seeing this when a year *is* specified with Python 2.6 and 2.7:


import time
time.strptime("20090229T184823Z", "%Y%m%dT%H%M%SZ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.6/_strptime.py", line 440, in _strptime
    datetime_date(year, 1, 1).toordinal() + 1
ValueError: day is out of range for month


import datetime
datetime.datetime.strptime("20090229T184823Z", "%Y%m%dT%H%M%SZ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 440, in _strptime
    datetime_date(year, 1, 1).toordinal() + 1
ValueError: day is out of range for month

----------
nosy: +swalker

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Shawn <[hidden email]> added the comment:

I'm an idiot; nevermind my comment.  The original date was bogus.

----------

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Hynek Schlawack <[hidden email]> added the comment:

The point isn’t that time.strptime validates dates but that it uses datetime internally:

julian = datetime_date(year, month, day).toordinal() - \
                      datetime_date(year, 1, 1).toordinal() + 1

Is it worth to reimplement this functionality?  It strikes easier to me to just use a different year if year is undefined and date == Feb 29.

----------

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Hynek Schlawack <[hidden email]> added the comment:

I gave it a shot, doesn’t look like a hack to me, what do you think?

----------
keywords: +patch
Added file: http://bugs.python.org/file25301/strptime-on-leap-years.diff

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Changes by Hynek Schlawack <[hidden email]>:


----------
keywords: +needs review
stage: needs patch -> patch review

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Changes by Arfrever Frehtes Taifersar Arahesis <[hidden email]>:


----------
nosy: +Arfrever

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Alexander Belopolsky <[hidden email]> added the comment:

This is a bit of a hack, but seems to get the work done.  Does anyone have any objections to committing?

----------

_______________________________________
Python tracker <[hidden email]>
<http://bugs.python.org/issue14157>
_______________________________________
_______________________________________________
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
|

[issue14157] time.strptime without a year fails on Feb 29

STINNER Victor
In reply to this post by STINNER Victor

Antoine Pitrou <[hidden email]> added the comment:

Fine with me.

----------
nosy: +pitrou
stage: patch review -> commit review

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