This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Default to year 1 in strptime if year 0 has been specified
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Matthias.Meyer, belopolsky, benjamin.peterson, hynek, lemburg, r.david.murray
Priority: low Keywords:

Created on 2012-05-11 10:54 by Matthias.Meyer, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (6)
msg160404 - (view) Author: Matthias Meyer (Matthias.Meyer) Date: 2012-05-11 10:54
Hi folks,

What I did:
import time
time.strptime('0000-10-03T15:35:05Z','%Y-%m-%dT%H:%M:%SZ')

What I expected:
time.struct_time(tm_year=0, tm_mon=10, tm_mday=3, tm_hour=15, tm_min=35, tm_sec=5, tm_wday=2, tm_yday=276, tm_isdst=-1)

What I got:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/lib/python2.7/_strptime.py", line 440, in _strptime
    datetime_date(year, 1, 1).toordinal() + 1
ValueError: year is out of range


Environment:
Ubuntu 12.04 x64
python --version: 2.7.3


If you need more information, please let me know...
msg160406 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-05-11 11:20
Hello Matthias,

%Y for strptime is defined as:

"Year with century as a decimal number [0001,9999] (strptime), […]"

so it works as specified. And actually that's correct as there was no year zero (http://en.wikipedia.org/wiki/0_(year)). :)
msg160407 - (view) Author: Matthias Meyer (Matthias.Meyer) Date: 2012-05-11 11:29
but ISO 8601 specifies year 0 to be identical with 1 BC. Shouldn't it default to that, then?
msg160410 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-05-11 11:52
Well, that's what C's strptime does. But that would be a feature request (if it works as documented, it's no bug). :)

I've transformed this issue accordingly. But as a feature request, it won't go into 2.7 unless I'm missing something.
msg160411 - (view) Author: Hynek Schlawack (hynek) * (Python committer) Date: 2012-05-11 12:16
I have to correct myself:

It's _not_ what C's strptime does. C's strptime returns via "struct tm" which saves only "years since 1900" in the "tm_year" field.
msg160631 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2012-05-14 16:07
Datetime doesn't support BC.
History
Date User Action Args
2022-04-11 14:57:30adminsetgithub: 58986
2012-05-14 16:07:16r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg160631

resolution: not a bug
2012-05-11 16:42:00terry.reedysetversions: - Python 3.2
2012-05-11 12:16:03hyneksetmessages: + msg160411
2012-05-11 11:52:24hyneksetpriority: normal -> low

type: crash -> enhancement

title: strptime fails for year 0 -> Default to year 1 in strptime if year 0 has been specified
nosy: + lemburg, belopolsky, benjamin.peterson
versions: + Python 3.2, Python 3.3, - Python 2.7
messages: + msg160410
resolution: not a bug -> (no value)
stage: needs patch
2012-05-11 11:29:15Matthias.Meyersetmessages: + msg160407
2012-05-11 11:20:33hyneksetresolution: not a bug

messages: + msg160406
nosy: + hynek
2012-05-11 10:54:35Matthias.Meyercreate