Mi versión.
Código Python:
Ver originalfrom datetime import date
from datetime import timedelta
def next_day(some_date, day):
"day: 0 - monday ... 6 - sunday"
offset = ((7 + day) - some_date.weekday()) % 7
return some_date + timedelta(days=offset)
def count_days(from_date, to_date, day):
new_from_date = next_day(from_date, day)
if new_from_date > to_date:
return 0
else:
return ((to_date - new_from_date).days / 7 + 1)
def weekdays(from_date, to_date):
"""
Count the days of rest
"""
sats = count_days(from_date, to_date, 5)
suns = count_days(from_date, to_date, 6)
return sats + suns
if __name__ == '__main__':
from time import time
t1 = time()
assert weekdays(date(2013, 9, 18), date(2013, 9, 23)) == 2, "Error en test 1"
assert weekdays(date(2013, 1, 1), date(2013, 2, 1)) == 8, "Error en test 2"
assert weekdays(date(2013, 2, 2), date(2013, 2, 3)) == 2, "Error en test 3"
assert weekdays(date(1999, 1, 1), date(2000, 1, 1)) == 105, "Error en test 4"
assert weekdays(date(2004, 2, 1), date(2004, 2, 29)) == 9, "Error en test 5"
assert weekdays(date(1980, 8, 8), date(2012, 3, 3)) == 3295, "Error en test 6"
assert weekdays(date(2001, 2, 2), date(2007, 12, 31)) == 722, "Error en test 7"
assert weekdays(date(2002, 9, 9), date(2002, 9, 10)) == 0, "Error en test 8"
assert weekdays(date(2011, 1, 1), date(2012, 2, 1)) == 114, "Error en test 9"
assert weekdays(date(1995, 3, 2), date(1999, 2, 3)) == 410, "Error en test 10"
assert weekdays(date(1992, 2, 1), date(2000, 1, 1)) == 827, "Error en test 11"
assert weekdays(date(1998, 5, 1), date(2001, 2, 22)) == 294, "Error en test 12"
assert weekdays(date(2003, 8, 2), date(2005, 3, 3)) == 166, "Error en test 13"
assert weekdays(date(2010, 2, 5), date(2010, 12, 31)) == 94, "Error en test 14"
t2 = time()