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 %U broken
Type: Stage:
Components: Library (Lib) Versions: Python 2.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: bnahas, brett.cannon
Priority: normal Keywords:

Created on 2007-01-24 23:00 by bnahas, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg31090 - (view) Author: Brian Nahas (bnahas) Date: 2007-01-24 23:00
Python 2.4.1 (#1, May 16 2005, 15:19:29)
[GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from time import strptime
>>> strptime('2006-53-0', '%Y-%U-%w')
(2006, 12, 31, 0, 0, 0, 6, 365, -1)
>>> strptime('2007-00-0', '%Y-%U-%w')
(2006, 12, 24, 0, 0, 0, 6, -7, -1)
>>> strptime('2007-01-0', '%Y-%U-%w')
(2007, 1, 7, 0, 0, 0, 6, 7, -1)
>>> strptime('2007-02-0', '%Y-%U-%w')
(2007, 1, 7, 0, 0, 0, 6, 7, -1)
>>> strptime('2007-03-0', '%Y-%U-%w')
(2007, 1, 14, 0, 0, 0, 6, 14, -1)
>>>


Note that in the above test, Sunday of week 1 and week 2 for 2007 reported the date as 2007-01-07 and Sunday of week 0 was reported as 2006-12-24, not 2006-12-31.  I'm not sure exactly what is correct, but the inconsistencies are bothersome.

Same results on:
Python 2.4.4c1 (#70, Oct 11 2006, 10:59:14) [MSC v.1310 32 bit (Intel)] on win32
msg31091 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2007-01-25 18:50
I will try to fix this when I can.  Just to warn you, Brian, I really doubt I will put the effort into backporting this to 2.4.
msg31092 - (view) Author: Brian Nahas (bnahas) Date: 2007-01-25 19:39
No worries.

Here's what I'm doing as a work-around.  I needed to convert the results of a mysql YEARWEEK field to the sunday at the start of that week:

import datetime
def mysqlWeekToSundayDate(yearweek):
    year = int(yearweek[0:4])
    week = int(yearweek[4:6])
    day = datetime.date(year, 1, 1)
    dayDelta = datetime.timedelta(1)
    weekDelta = datetime.timedelta(7)
    while day.strftime("%w") != "0":
        day = day + dayDelta
    
    day = day + ((week - 1) * weekDelta)
        
    return day

I'm relatively new to Python so it is probably not the most efficient method but it does the job.
msg31093 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2007-01-25 21:33
Rev. 53564 (trunk) has the fix and 2.5 will as soon as a commit problem I am having is fixed.  I basically rewrote the algorithm to have a generic calculation for the Julian day and just shifted the length of week 0 and the day of the week based on whether %U or %W was specified.  Cut out all the other edge cases which were messy and confusing.
History
Date User Action Args
2022-04-11 14:56:22adminsetgithub: 44505
2007-01-24 23:00:30bnahascreate