4

I am trying to get a date based on a number of the week, but there are some annoyances.

The date.weekday() returns the day of the week where 0 in Monday and 6 is Sunday.

The %w directive of date.strftime() and date.strptime() uses the 0 for Sunday and 6 for Saturday.

This causes some really annoying issues when trying to figure out a date given a week number from date.weekday().

Is there a better way of getting a date from a week number?

EDIT:

Added the example.

import datetime

original_date = datetime.date(2014, 8, 24)
week_of_the_date = original_date.isocalendar()[1] # 34
day_of_the_date = original_date.isocalendar()[2] # 7
temp = '{0} {1} {2}'.format(*(2014, week_of_the_date, day_of_the_date-1))
date_from_week = datetime.datetime.strptime(temp, '%Y %W %w')
week_from_new_date = date_from_week.isocalendar()[1] # 35!!

EDIT 2:
I ultimately put the date stuff in the view (using jQuery UI), it has more consistent notions of weeks.

kasperhj
  • 10,052
  • 21
  • 63
  • 106
  • What exactly is a week number in this context, and what does `date.weekday` have to do with it? Sample input and output would be helpful. – Peter DeGlopper Aug 24 '14 at 07:48
  • Ah, didn't know about that problem, very ugly inconsistency! Different, but related question [here](http://stackoverflow.com/q/7550726/2647279) – Bas Swinckels Aug 24 '14 at 07:48
  • Week number (usually?) refers to [ISO week date](http://en.wikipedia.org/wiki/ISO_week_date). Regarding the difference in weekday between time and datetime modules, you're right, it's hideous. –  Aug 24 '14 at 08:02
  • @PeterDeGlopper Sure. I've added an example. – kasperhj Aug 24 '14 at 08:02
  • 1
    Possibly relevant: http://stackoverflow.com/questions/304256/whats-the-best-way-to-find-the-inverse-of-datetime-isocalendar – Peter DeGlopper Aug 24 '14 at 08:07
  • 1
    I think the Sunday vs. Monday distinction between `weekday` and `strftime` using `%W` is moot - you could use `isoweekday` to get those to line up. The real problem is that `strftime`, based on the underlying C function, determines the first week of the year differently than the ISO definition. With `%W` the docs say: " All days in a new year preceding the first Monday are considered to be in week 0". ISO calendars count the week containing the first Thursday as the first week, for reasons I do not understand, and it doesn't look like there's a nicer way to reverse that than the answer linked. – Peter DeGlopper Aug 24 '14 at 08:22
  • There's also this module someone linked off one of the answers linked on that older answer: https://pypi.python.org/pypi/isoweek/ – Peter DeGlopper Aug 24 '14 at 08:25
  • @PeterDeGlopper You should probably post your last two comments as an answer. – Sylvain Leroux Aug 24 '14 at 08:33
  • 1
    @SylvainLeroux - I suppose there's enough actual information there that I'm not trying to take credit for pointing out a dupe. – Peter DeGlopper Aug 24 '14 at 08:42

1 Answers1

2

I think the Sunday vs. Monday distinction between weekday and strftime using %W is moot - you could use isoweekday to get those to line up, or %U in strftime if you wanted Sunday as the first day of the week. The real problem is that strftime, based on the underlying C function, determines the first week of the year differently than the ISO definition. With %W the docs say: " All days in a new year preceding the first Monday are considered to be in week 0". ISO calendars count the week containing the first Thursday as the first week, for reasons I do not understand.

Two ways I found to work with ISO weeks, either just getting datetime.date instances back or supporting a variety of operations, are:

  1. this answer with a simple timedelta approach: What's the best way to find the inverse of datetime.isocalendar()?
  2. this third-party library: https://pypi.python.org/pypi/isoweek/
Community
  • 1
  • 1
Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83