Skip to content

Instantly share code, notes, and snippets.

@mike-pete
Created December 17, 2019 21:51
Show Gist options
  • Save mike-pete/2f680a3d1ca61948636daba0c6556c22 to your computer and use it in GitHub Desktop.
Save mike-pete/2f680a3d1ca61948636daba0c6556c22 to your computer and use it in GitHub Desktop.
# These functions are used to calculate how many work days have been in the previous N number of days
days = ['mon','tue','wed','thu','fri','sat','sun']
# this function counts the workdays within the last n days
def countWorkDays(daysAgo, dayOfWeek):
# print(daysAgo, days[dayOfWeek])
workDay = 1 if dayOfWeek < 5 else 0
dayOfWeek = dayOfWeek - 1 if dayOfWeek > 0 else 6
# recursion
if daysAgo == 1:
return workDay
else:
return workDay + countWorkDays(daysAgo-1, dayOfWeek)
# this function calculates the workdays within the last n days
def calculateWorkDays(daysAgo, dayOfWeek):
rDays = daysAgo%7
weeks = (daysAgo-rDays)/7
# this +1 is an offset to count current day
x = dayOfWeek+1 - rDays
if x < -1: rDays -= 2
elif x < 0: rDays -= 1
return int(weeks*5 + rDays)
#today = days.index('mon')
daysAgo = 14
dayOfWeek = 0
a = countWorkDays(daysAgo, dayOfWeek)
b = calculateWorkDays(daysAgo, dayOfWeek)
print(a,b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment