Given a date range how to calculate the number of weekends partially or wholly within that range?

(A few definitions as requested:
take 'weekend' to mean Saturday and Sunday.
The date range is inclusive i.e. the end date is part of the range
'wholly or partially' means that any part of the weekend falling within the date range means the whole weekend is counted.)

To simplify I imagine you only actually need to know the duration and what day of the week the initial day is...

I darn well now it's going to involve doing integer division by 7 and some logic to add 1 depending on the remainder but I can't quite work out what...

extra points for answers in Python ;-)

Edit

Here's my final code.

Weekends are Friday and Saturday (as we are counting nights stayed) and days are 0-indexed starting from Monday. I used onebyone's algorithm and Tom's code layout. Thanks a lot folks.

First define "week". Seriously. What day does it start? Do you round up to the next whole week or truncate to the previous whole week? Do you go from matching days (Wed to Wed) or to you ignore fractional weeks and go Sun to Sun (or Mon to Mon)?
–
S.LottMay 19 '09 at 16:03

Does number of weekends mean number of saturdays and/or sundays in date range?
–
jcoonMay 19 '09 at 16:11

1

S.Lott - weeks don't come into it. You have a range of days. if a weekend (defined as Sat and Sun if you like) is partially or wholly in that period then it counts. Apart from defining weekend then everything was clearly stated in the original question if you read it carefully.
–
andybakMay 19 '09 at 16:44

1

Sorry, it wasn't clear. If the range starts on Sunday and ends on the following Saturday is that 0, 1 or 2 weekends?
–
S.LottMay 19 '09 at 17:20

1

@andybak: here's my point. The question had some small logical holes. "usually imply" and "most people interpret" don't plug the holes. In the case of calendrical calculations the details matter a great deal. Please update the question with the details so other people with similar questions can benefit from yours.
–
S.LottMay 19 '09 at 18:04

4 Answers
4

For each day of the week, figure out how many days are required before a period starting on that day "contains a weekend". For instance, if "contains a weekend" means "contains both the Saturday and the Sunday", then we have the following table:

Obviously this doesn't have to be coded as a table, now that it's obvious what it looks like.

Then, given the day-of-week of the start of your period, subtract[*] the magic value from the length of the period in days (probably start-end+1, to include both fenceposts). If the result is less than 0, it contains 0 weekends. If it is equal to or greater than 0, then it contains (at least) 1 weekend.

Then you have to deal with the remaining days. In the first case this is easy, one extra weekend per full 7 days. This is also true in the second case for every starting day except Sunday, which only requires 6 more days to include another weekend. So in the second case for periods starting on Sunday you could count 1 weekend at the start of the period, then subtract 1 from the length and recalculate from Monday.

More generally, what's happening here for "whole or part" weekends is that we're checking to see whether we start midway through the interesting bit (the "weekend"). If so, we can either:

1) Count one, move the start date to the end of the interesting bit, and recalculate.

2) Move the start date back to the beginning of the interesting bit, and recalculate.

In the case of weekends, there's only one special case which starts midway, so (1) looks good. But if you were getting the date as a date+time in seconds rather than day, or if you were interested in 5-day working weeks rather than 2-day weekends, then (2) might be simpler to understand.

My general approach for this sort of thing: don't start messing around trying to reimplement your own date logic - it's hard, ie. you'll screw it up for the edge cases and look bad. Hint: if you have mod 7 arithmetic anywhere in your program, or are treating dates as integers anywhere in your program: you fail. If I saw the "accepted solution" anywhere in (or even near) my codebase, someone would need to start over. It beggars the imagination that anyone who considers themselves a programmer would vote that answer up.

Next, filter down to just the days which are weekends. In your case you're interested in Friday and Saturday nights, which are 5 and 6. (Notice how I'm not trying to roll this part into the previous list comprehension, since that'd be hard to verify as correct).

weekend_days = [d for d in days if d.weekday() in (FRI,SAT)]
for day in weekend_days: # debug
print day, day.weekday() # debug

Finally, you want to figure out how many weekends are in your list. This is the tricky part, but there are really only four cases to consider, one for each end for either Friday or Saturday. Concrete examples help make it clearer, plus this is really the sort of thing you want documented in your code:

To count whole weekends, just adjust the number of days so that you start on a Monday, then divide by seven. (Note that if the start day is a weekday, add days to move to the previous Monday, and if it is on a weekend, subtract days to move to the next Monday since you already missed this weekend.)

You would need external logic beside raw math. You need to have a calendar library (or if you have a decent amount of time implement it yourself) to define what a weekend, what day of the week you start on, end on, etc.

Without a logical definition of days in your code, a pure mathematical methods would fail on corner case, like a interval of 1 day or, I believe, anything lower then a full week (or lower then 6 days if you allowed partials).

I don't think you do. We could recast the problem with integers. Given an integer range - how many 'strips' of 2 integers at intervals of 7 are wholly or partially in that range...
–
andybakMay 19 '09 at 16:20

A calendar library is not necessary, but it would probably be very helpful. If the goal is to solve this problem quickly, one would probably use the 'calendar' or 'datetime' class. If this is just an intellectual exercise, then by all means, knock yourself out with integer arithmetic. ;-)
–
las3rjockMay 19 '09 at 17:26

@andybak, well as a counter point, consider the case where you have 2 days, Saturday and Sunday, as opposed to say, Monday, Tuesday. Or to go even further, say you had one day, Saturday. I think any raw integer method is going to fail there. Your code is going to need some clear logical idea of what days of the week there are and what constitues a weekend.
–
James McMahonMay 19 '09 at 17:37

@andybak, The downvote should be undoable. Unless they decreased the amount of time you have to undo it. Let me edit my question and then you should be able to undo the vote, if you so desire.
–
James McMahonMay 19 '09 at 17:44

1

The chosen answer does define days. Wither you use a built in library or roll your own you still can't just take a number divide it to intervals of 7 to get your answer. Your code has to have some concept of the days it is representing.
–
James McMahonMay 20 '09 at 2:12