Last active
October 13, 2015 09:28
-
-
Save pyrtsa/4175467 to your computer and use it in GitHub Desktop.
Workdays in 2013
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
# Install dateutil with "pip dateutil install" | |
from datetime import date, timedelta as td | |
from itertools import count, takewhile | |
def holiday(d): # See https://gist.github.com/pyrtsa/3525697 | |
"""Check whether the given dateutil.date is a Finnish national holiday""" | |
import dateutil.easter | |
from datetime import date, timedelta as td | |
assert isinstance(d, date) | |
md = '{}.{}'.format(d.day, d.month) | |
y = d.year | |
# Fixed-date holidays | |
if md in '1.1 6.1 1.5 6.12 24.12 25.12 26.12'.split(): return True | |
# Finnish midsummer (eve on Fri the 19..25th, day on Sat the 20..26th) | |
if date(y,6,19) <= d <= date(y,6,25) and d.weekday() == 4: return True | |
if date(y,6,20) <= d <= date(y,6,26) and d.weekday() == 5: return True | |
# All Saints (Saturday) | |
if date(y,10,31) <= d <= date(y,11,6) and d.weekday() == 5: return True | |
# Easter etc. | |
e = dateutil.easter.easter(d.year) | |
if d in [e - td(2), e, e + td(1), e + td(39), e + td(49)]: return True | |
return False | |
def month_days(y, m): | |
return list(takewhile(lambda d: d.month == m, | |
(date(y, m, 1) + td(x) for x in range(31)))) | |
def month_workdays(y, m): | |
return [d for d in month_days(y, m) | |
if 1 <= d.isoweekday() <= 5 and not holiday(d)] | |
def year_workdays(y): | |
return [len(month_workdays(y, m)) for m in range(1, 13)] | |
if __name__ == '__main__': | |
print(year_workdays(2013)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment