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: datetime.strptime at the turn of the year
Type: behavior Stage: resolved
Components: Versions: Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 2.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: BUG in how _strptime() handles week 0
View: 23136
Assigned To: Nosy List: belopolsky, martin.panter, serhiy.storchaka, torm
Priority: normal Keywords:

Created on 2014-12-30 18:04 by torm, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
strptimetest.c serhiy.storchaka, 2014-12-30 18:45
Messages (4)
msg233213 - (view) Author: Tomasz Ryczkowski (torm) Date: 2014-12-30 18:04
I've found wrong behaviour datetime.strptim function at the turn of the year.

I've created datetime object base on the week number (%W), year (%Y) and day of week (%w). The date for Tuesday in the first week in 2015 is wrong:

>>> from datetime import datetime
>>> datetime.strptime('%s %s %s' % (0, 2015, 1), '%W %Y %w').date()
datetime.date(2014, 12, 29) # OK

>>> datetime.strptime('%s %s %s' % (0, 2015, 2), '%W %Y %w').date()
datetime.date(2015, 1, 1) # WRONG !!!

>>> datetime.strptime('%s %s %s' % (0, 2015, 3), '%W %Y %w').date()
datetime.date(2014, 12, 31) # OK

>>> datetime.strptime('%s %s %s' % (0, 2015, 4), '%W %Y %w').date()
datetime.date(2015, 1, 1) # OK

>>> datetime.strptime('%s %s %s' % (0, 2015, 5), '%W %Y %w').date()
datetime.date(2015, 1, 2) # OK

>>> datetime.strptime('%s %s %s' % (0, 2015, 6), '%W %Y %w').date()
datetime.date(2015, 1, 3) # OK

>>> datetime.strptime('%s %s %s' % (0, 2015, 0), '%W %Y %w').date()
datetime.date(2015, 1, 4) # OK

The same error exists in another years.

Link to my post about this on stackoverflow:
http://stackoverflow.com/questions/27708833/why-does-datetime-strptime-get-an-incorrect-date-for-tuesday-in-the-week-0-of
msg233215 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-30 18:45
In C the strptime function doesn't return valid data if an input is invalid.

$ ./strptimetest "0 2015 1" "%W %Y %w"
2015-00--2 00:00:00
$ ./strptimetest "0 2015 2" "%W %Y %w"
2015-00--1 00:00:00
$ ./strptimetest "0 2015 3" "%W %Y %w"
2015-00-00 00:00:00
$ ./strptimetest "0 2015 4" "%W %Y %w"
2015-01-01 00:00:00
$ ./strptimetest "0 2015 5" "%W %Y %w"
2015-01-02 00:00:00
$ ./strptimetest "0 2015 6" "%W %Y %w"
2015-01-03 00:00:00
$ ./strptimetest "0 2015 7" "%W %Y %w"
2015-01-00 00:00:00

So this behavior likely is not a bug and doesn't need to be fixed in maintained releases. But it would be good to make strptime() more consistant and either extend it to support week and weekday numbers out of current valid range, or raise an exception.
msg233239 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-12-31 10:11
Issue 23136 looks like a duplicate, but has a potential patch
msg233249 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2014-12-31 16:30
Closing as a duplicate of #23136.
History
Date User Action Args
2022-04-11 14:58:11adminsetgithub: 67323
2014-12-31 16:30:33belopolskysetstatus: open -> closed
versions: + Python 2.7, Python 3.2, Python 3.3, Python 3.4
superseder: BUG in how _strptime() handles week 0
messages: + msg233249

resolution: duplicate
stage: resolved
2014-12-31 10:11:06martin.pantersetnosy: + martin.panter
messages: + msg233239
2014-12-30 18:45:16serhiy.storchakasetfiles: + strptimetest.c
versions: + Python 3.5
nosy: + serhiy.storchaka, belopolsky

messages: + msg233215
2014-12-30 18:04:02tormcreate