Connecting Tech Pros Worldwide Help | Site Map

Getting start/end dates given week-number

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 9th, 2006, 02:05 PM
Tim Chase
Guest
 
Posts: n/a
Default Getting start/end dates given week-number

I've been trying to come up with a good algorithm for determining
the starting and ending dates given the week number (as defined
by the strftime("%W") function).

My preference would be for a Sunday->Saturday range rather than a
Monday->Sunday range. Thus,
[color=blue][color=green][color=darkred]
>>> startDate, stopDate = weekBoundaries(2006, 23)[/color][/color][/color]

would yield a start-date of June 4, 2006 and an end-date of June
10, 2006 in this hypothetical function (as strftime("%W") for
today, June 9th, 2006 returns 23).

I've posted my first round of code below, but I'm having problems
with dates early in 2005, as the tests show.

Any thoughts/improvements/suggestions would be most welcome.

Thanks,

-tkc


from datetime import date, timedelta
from time import strptime
DEBUG = False
tests = [
#test date start end
(date(2006,1,1), (date(2006,1,1), date(2006,1,7))),
(date(2006,1,2), (date(2006,1,1), date(2006,1,7))),
(date(2006,1,3), (date(2006,1,1), date(2006,1,7))),
(date(2006,1,4), (date(2006,1,1), date(2006,1,7))),
(date(2006,1,5), (date(2006,1,1), date(2006,1,7))),
(date(2006,1,6), (date(2006,1,1), date(2006,1,7))),
(date(2006,1,7), (date(2006,1,1), date(2006,1,7))),
(date(2006,1,8), (date(2006,1,8), date(2006,1,14))),
(date(2005,1,1), (date(2004,12,26), date(2005,1,1))),
(date(2005,1,2), (date(2005,1,2), date(2005,1,8))),
]
def weekBoundaries(year, week):
startOfYear = date(year, 1, 1)
now = startOfYear + timedelta(weeks=week)
# isoweekday() % 7 returns Sun=0 ... Sat=6
sun = now - timedelta(days=now.isoweekday() % 7)
sat = sun + timedelta(days=6)
if DEBUG:
print "DEBUG: now = %s/%s" % (now, now.strftime("%a"))
print "DEBUG: sun = %s/%s" % (sun, sun.strftime("%a"))
print "DEBUG: sat = %s/%s" % (sat, sat.strftime("%a"))
return sun, sat

for test, expectedResult in tests:
print "Testing %s" % test
year = test.year
# jigger it so that %W is Sun->Sat rather than Mon->Sun
weekNum = int((test + timedelta(days=1)).strftime("%W")) - 1
results = weekBoundaries(year, weekNum)
passed = (expectedResult == results)
print "Week#%s: %s" % (weekNum, passed)
print "=" * 50





  #2  
Old June 9th, 2006, 02:25 PM
wittempj@hotmail.com
Guest
 
Posts: n/a
Default Re: Getting start/end dates given week-number

see the calendar faq http://www.faqs.org/faqs/calendars/faq/part3/,
look especially in section 6.7.

Tim Chase wrote:[color=blue]
> I've been trying to come up with a good algorithm for determining
> the starting and ending dates given the week number (as defined
> by the strftime("%W") function).
>
> My preference would be for a Sunday->Saturday range rather than a
> Monday->Sunday range. Thus,
>[color=green][color=darkred]
> >>> startDate, stopDate = weekBoundaries(2006, 23)[/color][/color]
>
> would yield a start-date of June 4, 2006 and an end-date of June
> 10, 2006 in this hypothetical function (as strftime("%W") for
> today, June 9th, 2006 returns 23).
>
> I've posted my first round of code below, but I'm having problems
> with dates early in 2005, as the tests show.
>
> Any thoughts/improvements/suggestions would be most welcome.
>
> Thanks,
>
> -tkc
>
>
> from datetime import date, timedelta
> from time import strptime
> DEBUG = False
> tests = [
> #test date start end
> (date(2006,1,1), (date(2006,1,1), date(2006,1,7))),
> (date(2006,1,2), (date(2006,1,1), date(2006,1,7))),
> (date(2006,1,3), (date(2006,1,1), date(2006,1,7))),
> (date(2006,1,4), (date(2006,1,1), date(2006,1,7))),
> (date(2006,1,5), (date(2006,1,1), date(2006,1,7))),
> (date(2006,1,6), (date(2006,1,1), date(2006,1,7))),
> (date(2006,1,7), (date(2006,1,1), date(2006,1,7))),
> (date(2006,1,8), (date(2006,1,8), date(2006,1,14))),
> (date(2005,1,1), (date(2004,12,26), date(2005,1,1))),
> (date(2005,1,2), (date(2005,1,2), date(2005,1,8))),
> ]
> def weekBoundaries(year, week):
> startOfYear = date(year, 1, 1)
> now = startOfYear + timedelta(weeks=week)
> # isoweekday() % 7 returns Sun=0 ... Sat=6
> sun = now - timedelta(days=now.isoweekday() % 7)
> sat = sun + timedelta(days=6)
> if DEBUG:
> print "DEBUG: now = %s/%s" % (now, now.strftime("%a"))
> print "DEBUG: sun = %s/%s" % (sun, sun.strftime("%a"))
> print "DEBUG: sat = %s/%s" % (sat, sat.strftime("%a"))
> return sun, sat
>
> for test, expectedResult in tests:
> print "Testing %s" % test
> year = test.year
> # jigger it so that %W is Sun->Sat rather than Mon->Sun
> weekNum = int((test + timedelta(days=1)).strftime("%W")) - 1
> results = weekBoundaries(year, weekNum)
> passed = (expectedResult == results)
> print "Week#%s: %s" % (weekNum, passed)
> print "=" * 50[/color]

  #3  
Old June 9th, 2006, 04:55 PM
Serge Orlov
Guest
 
Posts: n/a
Default Re: Getting start/end dates given week-number

Tim Chase wrote:[color=blue]
> I've been trying to come up with a good algorithm for determining
> the starting and ending dates given the week number (as defined
> by the strftime("%W") function).[/color]

I think you missed %U format, since later you write:
[color=blue]
> My preference would be for a Sunday->Saturday range rather than a
> Monday->Sunday range. Thus,[/color]
[color=blue]
> Any thoughts/improvements/suggestions would be most welcome.[/color]

If you want to match %U:

def weekBoundaries(year, week):
startOfYear = date(year, 1, 1)
week0 = startOfYear - timedelta(days=startOfYear.isoweekday())
sun = week0 + timedelta(weeks=week)
sat = sun + timedelta(days=6)
return sun, sat

  #4  
Old June 9th, 2006, 05:35 PM
Tim Chase
Guest
 
Posts: n/a
Default Re: Getting start/end dates given week-number

> I think you missed %U format, since later you write:

correct. I remember seeing something (a long while back) that
had a Sunday-first format, but I must have missed it in my
reading of "man strftime".
[color=blue]
> If you want to match %U:
>
> def weekBoundaries(year, week):
> startOfYear = date(year, 1, 1)
> week0 = startOfYear - timedelta(days=startOfYear.isoweekday())
> sun = week0 + timedelta(weeks=week)
> sat = sun + timedelta(days=6)
> return sun, sat[/color]

Works wonderfully...Thanks!

-tkc




 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.