[Python-bugs-list] [ python-Bugs-656817 ] Bug in week calculation of time module

noreply@sourceforge.net noreply@sourceforge.net
Sat, 21 Dec 2002 08:45:04 -0800


Bugs item #656817, was opened at 2002-12-20 08:37
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=656817&group_id=5470

Category: Python Library
Group: Python 2.2.1
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Christian Loth (chloth)
>Assigned to: Tim Peters (tim_one)
Summary: Bug in week calculation of time module

Initial Comment:
Noticed while writing an application in connection with
PostgreSQL.
Because of Leapyears, the year 1998 had 53 weeks,
instead of
the normal 52 weeks. PostgreSQL does ist correctly:
SELECT EXTRACT(WEEK FROM TIMESTAMP'1998-31-12 20:38:40');
 date_part
-----------
        53
(1 row)

However the python equivalent fails, neither U nor W gets
it right:
>>> int(strftime("%U", strptime("1998-12-31", "%Y-%m-%d")))
52
>>> int(strftime("%W", strptime("1998-12-31", "%Y-%m-%d")))
52
>>>



----------------------------------------------------------------------

>Comment By: Tim Peters (tim_one)
Date: 2002-12-21 11:45

Message:
Logged In: YES 
user_id=31435

Well, yes, 1999 spanned *parts* of 53 calendar weeks.  
Every year does, since 365 = 7*52 + 1 -- no year fits 
wholly within 52 calendar weeks.

%U considers weeks to begin on Sunday.  1999-01-01 
was a Friday, and it and the next day (Saturday)  were 
in "week 0" according to %U rules.  1999-01-03 was the 
first Sunday, so was in "week 1" according to %U rules.  
1999-01-10 was the start of "week 2", 1999-01-17 the 
start of "week 3", and so on.  1999-12-26 was the start 
of "week 52".

Read the strftime docs.  You don't have to get ANSI's, the 
strftime format codes are also documented in Python's 
Library Ref Man.  I don't know what SQL's rules are, but 
they're clearly different.  That's not a bug in strftime, so 
closing this as invalid.

If what SQL returns is really the ISO 8601 week number, 
then the new datetime module in Python 2.3 supplies that  
via the date isocalendar() method.  Like so:

>>> from datetime import *
>>> date(1999, 12, 31).isocalendar()
(1999, 52, 5)
>>> date(1998, 12, 31).isocalendar()
(1998, 53, 4)
>>>

That appears to match the "week numbers" you're fixated 
<wink> on.

----------------------------------------------------------------------

Comment By: Christian Loth (chloth)
Date: 2002-12-21 04:45

Message:
Logged In: YES 
user_id=553797

If that is so, how do you explain the following (again
PostgreSQL vs.
Python comparison).

SELECT EXTRACT(WEEK FROM TIMESTAMP'1999-12-31 20:38:40');
 date_part
-----------
        52
(1 row)

>>> int(strftime("%U", strptime("1999-12-31", "%Y-%m-%d")))
52

Does that mean, according to your explaination, that 1999
had 53 weeks? (which of course it hadn't).


----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2002-12-20 12:13

Message:
Logged In: YES 
user_id=31435

Have you read the docs for strftime()?  strftime() is defined by 
the ANSI C standard, not by SQL  1998-01-01 was in "week 
0" according to ANSI C, so the 53rd week of the year is week 
52.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=656817&group_id=5470