473,394 Members | 1,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 16628
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
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...

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.