11

I've found a error in python datetime.strptime function.

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

What should I do with this information?

6
  • 1
    docs.python.org/2/bugs.html
    – Aran-Fey
    Dec 30, 2014 at 16:43
  • 1
    I would argue that all these dates are incorrect. There is no Monday in the first week of 2015. It's a nonsensical date. It'd be like asking for the 30th of February 2015 - there is no such date.
    – Dunes
    Dec 30, 2014 at 17:05
  • @Dunes However, the Python documents seem to define the first week of the year as the week (Sun-Sat) containing the first Monday of the year. Thus, there is, by definition, a Monday in the first week of 2015. The couple of days before that week that are in 2015, but not part of the first week are deemed to be part of week 0... So, your philosophical preferences aside, the first week of 2015 (or any year) is in fact well-defined according to the documentation... It just doesn't contain the first day of the year in this case...
    – twalberg
    Dec 30, 2014 at 19:33
  • @twalberg I seem to have made a terrible attempt at expressing myself. What I meant is, there is no Monday in the 0th week of 2015. The 0th week of 2015 contains days Thurs-Sat. As such, if you specify an invalid date (such as "0 2015 0" in this case), then strptime is not guaranteed to produce a valid output. I see it as no different than asking what 'a' is in base 10. 'a' is not a defined integer in base 10, and nor is "0 2015 0" a defined date for the format "%W %Y %w".
    – Dunes
    Dec 30, 2014 at 21:16
  • @Dunes I would argue that every week should contain 7 days no matter when it starts or ends. The problem is unavoidable since the length of a year is not evenly divisible by 7. Either week 0 should not exist (it would be week 53 of the previous year), or it should be valid for all 7 days. Since week 0 is documented as being valid, it should behave consistently. Dec 30, 2014 at 22:19

2 Answers 2

6

I've looked over more years and I get the same puzzling behaviour, but I found some logic.

After reading the docs, I understand it a bit better:

%W - Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.

So, %W only fills the correct values in week 0 for the days in the new year! This is perfectly consistent with the following results:

2015:

>>> for i in range(7):
...     datetime.strptime('%s %s %s' % (0, 2015, i), '%W %Y %w').date()
... 
datetime.date(2015, 1, 4)
datetime.date(2014, 12, 29)
datetime.date(2015, 1, 1)
datetime.date(2014, 12, 31)
datetime.date(2015, 1, 1) # start of year
datetime.date(2015, 1, 2)
datetime.date(2015, 1, 3)

2016:

>>> for i in range(7):
...     datetime.strptime('%s %s %s' % (0, 2016, i), '%W %Y %w').date()
... 
datetime.date(2016, 1, 3)
datetime.date(2015, 12, 28)
datetime.date(2015, 12, 29)
datetime.date(2016, 1, 1)
datetime.date(2015, 12, 31)
datetime.date(2016, 1, 1) # start of year
datetime.date(2016, 1, 2)

2017:

>>> for i in range(7):
...     datetime.strptime('%s %s %s' % (0, 2017, i), '%W %Y %w').date()
... 
datetime.date(2017, 1, 1)
datetime.date(2016, 12, 26)
datetime.date(2016, 12, 27)
datetime.date(2016, 12, 28)
datetime.date(2016, 12, 29)
datetime.date(2017, 1, 1)
datetime.date(2016, 12, 31)
# ... start of year

2018:

>>> for i in range(7):
...     datetime.strptime('%s %s %s' % (0, 2018, i), '%W %Y %w').date()
... 
datetime.date(2018, 1, 7)
datetime.date(2018, 1, 1) # start of year
datetime.date(2018, 1, 2)
datetime.date(2018, 1, 3)
datetime.date(2018, 1, 4)
datetime.date(2018, 1, 5)
datetime.date(2018, 1, 6)

So after the year actually begins, the behaviour seems predictable and consistent with the docs.

3
  • 2
    I think this is likely the case. Giving a week number of 0 and a day number that is prior to the start of the given year is probably considered outside the expected domain of the function. Although, I would think it should probably toss an error of some sort for such cases instead of returning bad data...
    – twalberg
    Dec 30, 2014 at 16:53
  • @twalberg - I agree, that's why I said it's puzzling without further experimentation. This is not following python's "least surprising behaviour" thing... Dec 30, 2014 at 16:54
  • Even though the behavior may be consistent with the documentation, I still wonder what implementation of the function would return these results. "Puzzling" is right! Dec 30, 2014 at 17:25
4

I was able to confirm this is a bug. I studied the _strptime.py module and can confirm it's an edge condition with how it handle's julian date calculations.

The issue stems from the fact that calls to _calc_julian_from_U_or_W() can return a -1, which under normal circumstances is invalid. The strptime() function tests and corrects when julian values are -1...but it should NOT do this when the week_of_year is zero.

BTW: The fact that it tests for ONLY -1 is why you are seeing the problem in 2015. This condition only exists when the first day of the year is exactly two days ahead of the date your are testing for.

The following patch corrects the edge condition

--- _strptime.py.orig   2014-12-30 15:47:05.069835336 -0500
+++ _strptime.py        2014-12-30 15:47:21.509139500 -0500
@@ -441,7 +441,7 @@
     # Cannot pre-calculate datetime_date() since can change in Julian
     # calculation and thus could have different value for the day of the week
     # calculation.
-    if julian == -1:
+    if julian == -1 and week_of_year != 0:
         # Need to add 1 to result since first day of the year is 1, not 0.
         julian = datetime_date(year, month, day).toordinal() - \
                   datetime_date(year, 1, 1).toordinal() + 1

I have applied this patch to my local machine, and I now see what I believe the OP wanted:

>>> datetime.strptime('%s %s %s' % (0, 2015, 2), '%W %Y %w').date()
datetime.date(2014, 12, 30)

Filed bug report http://bugs.python.org/issue23136

3
  • The Python documentation for strptime differs from what you quote here. But it's likely that the implementation simply calls the underlying C function so its behavior would be identical. Do you have information about the "bizarre rules" for week 0? Dec 30, 2014 at 17:13
  • @MarkRansom strptime is implemented in pure Python. See _strptime.py, probably in your Python install's /lib/ folder.
    – senshin
    Dec 30, 2014 at 17:22
  • @MarkRansom, I added a quote from the docs to my answer, which (to me) seems to be consistent with the behaviour. Dec 30, 2014 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.