473,503 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,
startDate, stopDate = weekBoundaries(2006, 23)


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


Jun 9 '06 #1
3 16641
see the calendar faq http://www.faqs.org/faqs/calendars/faq/part3/,
look especially in section 6.7.

Tim Chase wrote:
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,
>>> startDate, stopDate = weekBoundaries(2006, 23)


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


Jun 9 '06 #2
Tim Chase wrote:
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

Jun 9 '06 #3
> 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


Jun 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2323
by: Dave Griffiths | last post by:
Hi I am trying to get JS to work out week numbers for given dates, I'm sure this is possible. Any help would be welcomed at this point. Thanks in advance.
5
3286
by: Bullschmidt | last post by:
If I know the week number and the year, how can I calc the beginning and ending dates of the week? For background I'm going to do some grouping by week but don't just want to call the weeks Week...
6
2441
by: Bill R via AccessMonster.com | last post by:
I have a query: SELECT tblCalendar.CalendarDay AS LastSunday FROM tblCalendar WHERE (((tblCalendar.CalendarDay)>=(Now()-7) And (tblCalendar.CalendarDay)...
0
2834
by: Lee Harr | last post by:
I wrote a function to return the first date of a given week (and a few related functions) : -- return the first date in the given week CREATE or REPLACE FUNCTION week_start(integer, integer)...
2
4202
by: MSK | last post by:
Hi, Continued to my earlier post regaring "Breakpoints are not getting hit" , I have comeup with more input this time.. Kindly give me some idea. I am a newbie to .NET, recently I installed...
5
359
by: Elainie | last post by:
I need to get the dates between now and next week but using Now and Next week not any specific dates... Please help, going mad... Elaine
2
1579
by: egrill | last post by:
I need to be able to group date field by week. I can identify the week but I need to translate the ww into a date. For example; if the date falls in the 5th week of the year, I want to group all...
5
2500
by: cla | last post by:
I'm using this code on an application to track football schedules: ---- $season = '2005'; $basedate = strtotime('this friday', strtotime('31 August '.$season)); for($d=0;$d<=31;$d++) {...
3
2345
by: pchaitanya | last post by:
I have selected some list of valid dates to a label. now i need to find first day among the given dates from label contrl i got dates from calender control by clicking for entire week.. ...
8
2510
by: Innocent2104 | last post by:
Hi there, The script below displays the attached output but as shown, it skips certain days and i need to include these to calculate my avg balance for a certain month, i.e.Nov. How do i update the...
0
7207
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7291
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5598
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.