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: strptime() with year-weekday pair can produce invalid data
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: belopolsky, iritkatriel, lemburg, serhiy.storchaka, skip.montanaro
Priority: normal Keywords:

Created on 2015-03-20 11:19 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin.

Messages (6)
msg238645 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-20 11:19
strptime() with year-weekday pair can produce invalid data.

>>> time.strptime('2015 0', '%Y %w')
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=1, tm_isdst=-1)
>>> time.strptime('2015 1', '%Y %w')
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
>>> time.strptime('2015 2', '%Y %w')
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=1, tm_isdst=-1)
>>> time.strptime('2015 3', '%Y %w')
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=1, tm_isdst=-1)
>>> time.strptime('2015 4', '%Y %w')
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=-1)
>>> time.strptime('2015 5', '%Y %w')
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=1, tm_isdst=-1)
>>> time.strptime('2015 6', '%Y %w')
time.struct_time(tm_year=2015, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=1, tm_isdst=-1)

You get Jan 1 with any weekday.
msg238679 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2015-03-20 14:26
I don't think this is a bug. From the 2.7.9 docs:

The default values used to fill in any missing data when more accurate values cannot be inferred are (1900, 1, 1, 0, 0, 0, 0, 1, -1).
msg238682 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-20 14:36
Actually you can got invalid data for any date.

>>> time.strptime('2015 3 20', '%Y %m %d')
time.struct_time(tm_year=2015, tm_mon=3, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=4, tm_yday=79, tm_isdst=-1)
>>> time.strptime('2015 3 20 0', '%Y %m %d %w')
time.struct_time(tm_year=2015, tm_mon=3, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=79, tm_isdst=-1)

All date values are known, the problem is that they contradict.
msg238695 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2015-03-20 15:46
>>>> time.strptime('2015 3 20 0', '%Y %m %d %w')
> time.struct_time(tm_year=2015, tm_mon=3, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=79, tm_isdst=-1)
>
> All date values are known, the problem is that they contradict.

Garbage in, garbage out. :-) Clearly today is not Sunday. I not sure
there is a correct answer in the face of contradictory inputs.
msg238702 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-03-20 17:00
Agree. But may be at least emit a warning?
msg407340 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-30 00:12
Reproduced on 3.11.
History
Date User Action Args
2022-04-11 14:58:14adminsetgithub: 67905
2021-11-30 00:12:31iritkatrielsetnosy: + iritkatriel

messages: + msg407340
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.4, Python 3.5
2015-03-20 17:00:08serhiy.storchakasetmessages: + msg238702
2015-03-20 15:46:48skip.montanarosetmessages: + msg238695
2015-03-20 14:36:45serhiy.storchakasetmessages: + msg238682
2015-03-20 14:26:18skip.montanarosetnosy: + skip.montanaro
messages: + msg238679
2015-03-20 11:19:31serhiy.storchakacreate