any lang: week number
I don't do much programming and I've never tried working out a calendar problem. I'm trying to
do this in python and if I had the background, then maybe I could find the answer using datetime() or something. Anyhow, here's my issue... I'd like to get a week number. That is, starting with Sunday, ending with Saturday. Looking at UNIX cal I see today, 3/29 would be week 5. I purloined this code from stack overflow and it generally works fine. However when I ran this through a number of tests I found some issues with some weeks where the 21st and 22nd are in the 4th week. March 2011 is an example. Here's what I'm using. Any suggestions are appreciated. Code:
print "which date?", |
Hi,
in a shell the current week can be obtained with: Code:
date +%W Code:
date +%W --date 2011-10-05 Code:
import datetime Evo2. |
Thanks evo2. I should have been more clear. What I'm after is a week number 1-5. The first week being 1, starting on Sundays. I can see this with UNIX cal but to get a proper calculation has been difficult.
|
You still need a little more clarity ;)
Quote:
When I execute cal on my machine, I get: Code:
March 2012 Perhaps you would be interested in isocalendar(). From Python 2.7.2 documentation: Code:
date.isocalendar() |
Hi,
"week number" normally means the week of the year 0-53 (man 1 date). Perhaps what you want is the week of the month? Could post the invocation, output and version of cal that provides this information. Cheers, Evo2. |
Like evo2 and Dark_Helmet above stated, the problem is that we do not have any definition for your "week number".
For example, consider February 2012. The first and last days of February 2012 fell on Wednesdays. Which dates are the first week of February 2012? There are many, all equally valid, answers:
Asking for week number 1-5 is like asking for the NominalAnimalNumber of February 2012. Nobody can answer it correctly before you define exactly what it is. I'm trying hard to not be a dickweed here. If you want the calendar definitions used in your region, just say so; they're very well defined -- but there are large differences between various regions in the world. See ISO week date and Seven-day week articles at Wikipedia, to see how complex (or simple, depending on how familiar you are with human cultural differences) issue week numbering really is. |
I'm just throwing this out there as an example of how I might use the isocalendar() function I referenced. Perhaps it will spur you in the right direction.
This is from an interpreter session: Code:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) And of course, you can always substitute the user-supplied value from your prompt for the literal '29' I used in the above. But again, that's using the Monday-based ISO calendar format. |
All the theory aside; assuming he uses a linux distro which' locale has the definitions
he desires, these should do the job: Code:
$ cal |
Quote:
First, for a Finnish calendar (with LC_ALL=fi_FI.UTF-8 LANG=fi_FI.UTF-8) I need to run ncal -bM to get the standard calendar, Code:
Maaliskuu 2012 Second, '%w' yields the day of week number, 0 = Sun, 1 = Mon, ..., 6 = Sat, not a week number. |
Aight. It's settled. I need a holiday
|
Thanks for the comments. Sorry for the lack of clarity. I've never worked with this type of problem before. There's a couple of posts on Stack Overflow that discuss this (I'm appending URL below).
I think what Dark_Helmet points out above, using isocalendar(), might do the trick. I'm going to play with this and come back to this post. Visually, this is what I have in mind: Code:
March 2012 The week_number I posted earlier comes from these posts. http://stackoverflow.com/questions/7...eek-in-a-month http://stackoverflow.com/questions/3...r-of-the-month |
Quote:
Use the datetime Python module for this. Consider something like the following: Code:
import datetime Code:
print day(2012, 5, 2, 4) Does this not solve your larger problem rather simply? |
Quote:
Prior to reading these additional posts I found I was reasoning with the issue incorrectly. That is, this isn't a *horizontal* calendar issue (like I posted earlier) but rather a *vertical* one. To find the 2nd and 4th Mon and Tue from March 2011: Code:
Su Mo Tu We Th Fr Sa FWIW, here is the old code: Code:
import time, datetime |
Quote:
First, the function assumes you specify the desired date as year, month, weeknum (from 1 to 5, for first to fifth, respectively), and weekday (0 for Monday, 1 for Tuesday, and so on, up to 6 for Sunday). It will then return the datetime.date object that matches that date. First, we create a datetime.date object that matches the first day of the desired month: Code:
theday = datetime.date(year, month, 1) Code:
if theday.weekday() < weekday: Code:
elif theday.weekday() > weekday: Code:
while (theday.weekday() != weekday): At this point, theday is the first desired weekday weekday of the desired month and year. All we need to is adjust theday forwards by weeknum-1 weeks -- as the desired date asked is the weeknum'th weekday: Code:
return theday + datetime.timedelta(7 * (weeknum - 1)) Your example code indicates you actually use the inverse; that is, you want to know whether this is the first, second, third, fourth, or fifth occurrence of this weekday this month. This is even simpler, and depends only on the day of month, and is, as you posted, 1 + ((day - 1) // 7) : Code:
import datetime _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ Why does 1 + ((day - 1) // 7) then work? In Python, // is integer division operator. It means the result will be an integer, with all the decimals removed. The key point here is to understand this operation, i.e. that Code:
0 // 7 == 0 10 // 7 == 1 20 // 7 == 2 The next seven days, 8 to 14, are the second occurrences of their respective weekdays. The next seven days, 15 to 21, are the third occurrences. The next, 22 to 28, the fourth. Obviously the 29 to 31 are the fifth occurrences. Now, if you read the two paragraphs above, and look at the integer division table above, the answer should be obvious. Substract one from the day-of-month, integer divide by seven, and add one, and you have your desired result, 1 to 5. Or, you could even use a fixed list (remembering that Python starts list indices from zero): Code:
dayname = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ] |
All times are GMT -5. The time now is 03:29 AM. |