➜

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
标题: time.strptime does not properly convert out-of-bounds values
类型: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6, Python 3.5, Python 2.7
process
状态: closed Resolution: duplicate
Dependencies: 后续: strptime() can produce invalid date with negative year day
View: 23718
分配给: 抄送列表: abarry, beng94, iaslan, ned.deily
优先级: normal 关键字:

Created on 2016-02-03 16:02 by iaslan, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg259488 - (view) Author: Ioannis Aslanidis (iaslan) 日期: 2016-02-03 16:02
There seems to be a bug in the time library when performing the following conversion:

"""
In [8]: mytime=time.strptime(str([2015, 53, 0]), '[%Y, %U, %w]')

In [9]: mytime
Out[9]: time.struct_time(tm_year=2016, tm_mon=1, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=368, tm_isdst=-1)

In [10]: time.strftime('%Y %U', mytime)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-10-c13538c08ea5> in <module>()
----> 1 time.strftime('%Y %U', mytime)

ValueError: day of year out of range 
"""

As you can observe, tm_yday got a value of 368 instead of 3. It seems that the C function is not properly subtracting 365 or 366 days when converting from week 53 of a particular year which, according to ISO 8601 [1] and per python documentation [2]. Documentation explicitly says that: "The first week of the year is the week with the year's first Thursday in it".

[1] https://en.wikipedia.org/wiki/ISO_8601#Week_dates
[2] https://docs.python.org/2/library/time.html
msg259957 - (view) Author: Tamás Bence Gedai (beng94) * 日期: 2016-02-09 22:19
This is connected to http://bugs.python.org/issue23718

I submitted a patch that hopefully solves this problem.
msg260165 - (view) Author: Ned Deily (ned.deily) * (Python committer) 日期: 2016-02-12 05:19
Thanks for the report.  Closing this issue as a duplicate of Issue23719.
msg260166 - (view) Author: Ned Deily (ned.deily) * (Python committer) 日期: 2016-02-12 05:21
Make that Issue23718
历史
日期 用户 动作 参数
2022-04-11 14:58:27admin修改github: 70467
2016-02-12 05:21:10ned.deily修改后续: PEP 475: port test_eintr to Windows -> strptime() can produce invalid date with negative year day
消息: + msg260166
2016-02-12 05:19:15ned.deily修改状态: open -> closed

后续: PEP 475: port test_eintr to Windows

抄送: + ned.deily
消息: + msg260165
resolution: duplicate
stage: resolved
2016-02-09 22:19:16beng94修改抄送: + beng94
消息: + msg259957
2016-02-03 16:13:43abarry修改抄送: + abarry
标题: Possible bug in time library -> time.strptime does not properly convert out-of-bounds values

versions: - Python 3.2, Python 3.3, Python 3.4
2016-02-03 16:02:51iaslan创建