Last active
January 29, 2016 19:12
-
-
Save edigiacomo/307e93e83b9f7e8c4121 to your computer and use it in GitHub Desktop.
Calcolo dei giorni lavorati durante l'A.S. 2015-16 per il Comune di Pesaro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date, timedelta | |
import calendar | |
def daterange(start, end, step=timedelta(days=1)): | |
d = start | |
while d <= end: | |
yield d | |
d += step | |
def is_holiday(day): | |
if day.isoweekday() == 7: | |
return True | |
elif day in ( | |
date(2015, 9, 24), | |
date(2015, 11, 1), | |
date(2015, 11, 2), | |
date(2015, 12, 7), | |
date(2015, 12, 8), | |
) + tuple( | |
daterange(date(2015, 12, 24), date(2016, 1, 6)) | |
) + tuple( | |
daterange(date(2016, 3, 24), date(2016, 3, 29)) | |
) + ( | |
date(2016, 3, 30), date(2016, 4, 25), date(2016, 5, 1), | |
date(2016, 6, 2), | |
): | |
return True | |
else: | |
return False | |
def is_workday(day): | |
if day == date(2015, 9, 14): | |
return True | |
elif all([ | |
day >= date(2015, 9, 14), | |
day <= date(2015, 10, 6), | |
]): | |
return calendar.weekday(day.year, day.month, day.day) in ( | |
calendar.WEDNESDAY, calendar.THURSDAY, calendar.FRIDAY | |
) | |
elif all([ | |
day > date(2015, 10, 6), | |
day < date(2016, 1, 11), | |
]): | |
return calendar.weekday(day.year, day.month, day.day) in ( | |
calendar.MONDAY, calendar.TUESDAY, calendar.WEDNESDAY, | |
calendar.THURSDAY, | |
) | |
elif all([ | |
day >= date(2016, 1, 11), | |
day <= date(2016, 6, 4), | |
]): | |
return calendar.weekday(day.year, day.month, day.day) in ( | |
calendar.TUESDAY, calendar.WEDNESDAY, calendar.THURSDAY, | |
calendar.FRIDAY | |
) | |
else: | |
return False | |
def dump_summary(start_day, end_day): | |
days = [ | |
d for d in daterange(start_day, end_day) | |
if not is_holiday(d) and is_workday(d) | |
] | |
print("Giorni lavorativi: {}.".format(len(days))) | |
print("Elenco:") | |
for d in days: | |
print("- {:%a %d %b %y}".format(d)) | |
start_day = date(2015, 9, 1) | |
end_day = date(2016, 6, 4) | |
dump_summary(start_day, end_day) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment