Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Week Numbers where all weeks have 7 days, regardless of year rollover
    text
    copied!<p>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.</p> <p>For example, I want all the days from Dec 30, 2012 to Jan 5, 2013 to be in the same week.</p> <p>But this is not straight forward to do in python, because as the <code>datetime</code> documentation states <a href="http://docs.python.org/2/library/datetime.html" rel="noreferrer">here</a>:</p> <pre><code>%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. </code></pre> <p>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.</p> <p>Therefore Python returns:</p> <pre><code> import datetime datetime.date(2012, 12, 31).strftime('%Y-%U') &gt;&gt;&gt; 2012-53 import datetime datetime.date(2013, 01, 01).strftime('%Y-%U') &gt;&gt;&gt; 2013-00 </code></pre> <p>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.</p> <p>Instead, I want functionality that mirrors what MySQL does with <code>yearweek</code> in mode 2 (doc <a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_yearweek" rel="noreferrer">here</a>).</p> <p>For example, </p> <pre><code> mysql&gt; select yearweek('2013-01-01', 2) as week; +--------+ | week | +--------+ | 201253 | +--------+ 1 row in set (0.64 sec) </code></pre> <p>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. </p> <p>Is this already implemented in Python?</p> <p>Calendar included below for reference:</p> <pre><code> 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload