Getting start/end dates given week-number

T

Tim Chase

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,

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
 
W

wittempj

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

Tim said:
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,

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
 
S

Serge Orlov

Tim said:
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).

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

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
 
T

Tim Chase

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".
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

Works wonderfully...Thanks!

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top