weekdays in range
Dave Angel
davea at ieee.org
Sun Oct 18 06:51:07 EDT 2009
Jive Dadson wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">Wow.
> It's a danged tutorial. Thanks again. Take a break.
>
> </div>
>
Ben Finney's method is a very good approach, and an experienced Python
programmer would consider it straightforward.
But I have to ask whether the range of dates you might be considering
could be large. For example, if you want to know how many week-days
there are between a date in 1904 and 2207, it'd build a list of some 110
thousand items. There are other approaches which would be faster,
consume much less memory, and be much harder to read.
I'm not offering to debug it, but here's such an approach, subject to
the old plus/minus one bugs.
def epochweekdays (datetimeobj)
"""Given an arbitrary Monday long ago, figure out how many
weekdays have occurred between that day and the argument"""
subtract to get total_days
return int(totaldays/7) * 5 + max(totaldays%7, 5)
Now, your answer is just
epochweekdays(b) - epochweekdays(a)
DaveA
More information about the Python-list
mailing list