Last active
June 24, 2020 04:05
-
-
Save vsbabu/3345fab9208baa9b1d90095f2c69e9c2 to your computer and use it in GitHub Desktop.
Python3 - calculate working days considering a holiday calendar
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
#!/usr/bin/env python3 | |
import numpy as np | |
import datetime as dt | |
import sys | |
HOLIDAYS = [ | |
"2020-01-01", | |
"2020-02-21", | |
"2020-04-10", | |
"2020-05-01", | |
"2020-05-25", | |
"2020-07-31", | |
"2020-10-02", | |
"2020-10-26", | |
"2020-11-16", | |
"2020-12-25", | |
] | |
DEFAULT_START = dt.date(2020, 5, 1) | |
def get_year_month_week_day(days): | |
year = int(days / 365) | |
days = days % 365 | |
month = int(days / 30) | |
days = days % 30 | |
week = int(days / 7) | |
day = days % 7 | |
return year, month, week, day | |
def gets_year_month_week_day(days): | |
y, m, w, d = get_year_month_week_day(days) | |
return f"""{y}yr {m}mo {w}wk {d}dys""" | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
start = dt.datetime.strptime(sys.argv[1], "%Y-%m-%d").date() | |
else: | |
start = DEFAULT_START | |
end = dt.date.today() # - dt.timedelta(days=1) | |
HOLIDAYS = np.array(HOLIDAYS, dtype="datetime64") | |
start = np.datetime64(start) | |
end = np.datetime64(end) | |
# weekmask for working_days is set as saturday and sunday as not working; | |
# add our holiday list too to the mix | |
working_days = int( | |
np.busday_count(start, end, weekmask="1111100", holidays=HOLIDAYS) | |
) | |
days = int(np.busday_count(start, end, weekmask="1111111")) | |
sdays = gets_year_month_week_day(days) | |
if days < 0 or working_days < 0: | |
print(f"""{working_days} workdays; {days} days till {start} \n ({sdays})""") | |
else: | |
print(f"""{working_days} workdays; {days} days since {start} \n ({sdays})""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment