Skip to main content
Code Review

Return to Revisions

2 of 3
added link to documentation
200_success
  • 145.5k
  • 22
  • 190
  • 479

You have a bug: if dt is a Sunday, then it outputs the date range starting from the previous Sunday. That is because datetime.isocalendar() represents the day of week as a number from 1 (= Monday) to 7 (= Sunday).

>>> print week_string(date(2014, 01, 19))
Jan 12 - 18, 2014

My recommendation:

def week_string(dt):
 # Use underscore to indicate disinterest in the year and week
 _, _, weekday = dt.isocalendar()
 week_0 = dt - timedelta(days=weekday % 7)
 week_1 = week_0 + timedelta(days=6)

Then, I would write the following six lines more compactly:

 day_0, month_0, year_0 = week_0.strftime('%e-%b-%Y').lstrip().split('-')
 day_1, month_1, year_1 = week_1.strftime('%e-%b-%Y').lstrip().split('-')

The rest of it seems fine.

200_success
  • 145.5k
  • 22
  • 190
  • 479
default

AltStyle によって変換されたページ (->オリジナル) /