Python Week Numbers where all weeks have 7 days, regardless of year rollover
Question: I have an application where I need to measure the week-number of the year, and
I want all weeks to have 7 days, regardless of whether the days are in
separate years.

For example, I want all the days from Dec 30, 2012 to Jan 5, 2013 to be in the
same week.

But this is not straight forward to do in python, because as the `datetime`
documentation states [here](http://docs.python.org/2/library/datetime.html):

    
    
    %U  Week number of the year (Sunday as the first day of the week) 
    as a decimal number [00,53]. All days in a new year preceding the 
    first Sunday are considered to be in week 0.
    

I do not want 'All days in a new year preceding the first Sunday' to be
considered to be in week 0. Week 0 will have less than 7 days, as will the
last week of 2012.

Therefore Python returns:

    
    
     import datetime 
     datetime.date(2012, 12, 31).strftime('%Y-%U')
     >>> 2012-53
    
     import datetime 
     datetime.date(2013, 01, 01).strftime('%Y-%U')
     >>> 2013-00
    

Even though those two days are a Monday and Tuesday, respectably and should be
in the same week, when a week is considered to start on Sunday and end on
Saturday.

Instead, I want functionality that mirrors what MySQL does with `yearweek` in
mode 2 (doc [here](http://dev.mysql.com/doc/refman/5.1/en/date-and-time-
functions.html#function_yearweek)).

For example,

    
    
     mysql> select yearweek('2013-01-01', 2) as week;
     +--------+                                      
     | week   |                                      
     +--------+                                      
     | 201253 |                                      
     +--------+                                      
     1 row in set (0.64 sec)                         
    

Note that even though the date is in 2013, the week is considered to be
201253, guaranteeing that the last week of 2012 will 7 days.

Is this already implemented in Python?

Calendar included below for reference:

    
    
       December 2012     
    Mo Tu We Th Fr Sa Su 
                    1  2 
     3  4  5  6  7  8  9 
    10 11 12 13 14 15 16 
    17 18 19 20 21 22 23 
    24 25 26 27 28 29 30 
    31                   
    
         January 2013     
    Mo Tu We Th Fr Sa Su 
        1  2  3  4  5  6 
     7  8  9 10 11 12 13 
    14 15 16 17 18 19 20 
    21 22 23 24 25 26 27 
    28 29 30 31          
    


Answer: The **isoweek** module provides everything you need.

From the documentation:

    
    
    from isoweek import Week
    w = Week(2011, 20)
    print "Week %s starts on %s" % (w, w.monday())
    
    print "Current week number is", Week.thisweek().week
    print "Next week is", Week.thisweek() + 1
    

<http://pypi.python.org/pypi/isoweek/1.1.0>

