Created
April 15, 2010 17:58
-
-
Save dizzi/367410 to your computer and use it in GitHub Desktop.
This file contains 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
''' | |
Created on 15.4.2010 | |
@author: meskod | |
''' | |
import string,time,calendar | |
from datetime import date, datetime, timedelta | |
class workingDays: | |
''' | |
classdocs | |
''' | |
holidays = [] | |
def __init__(self, holidaysFile): | |
with open(holidaysFile, "r") as fi: | |
print("Opening holidays file "+holidaysFile+"\n") | |
holidaysLines = fi.readlines() | |
fi.close | |
for line in holidaysLines: | |
if not line.startswith("CZK"): | |
continue | |
parts = line.split(" ") | |
self.holidays.append(parts[1].strip()) | |
def previousWD(self, date): | |
oneDay = timedelta(days=1) | |
while(True): | |
date=date-oneDay | |
if(self.isWD(date)): | |
return date | |
def nextWD(self, date): | |
oneDay = timedelta(days=1) | |
while(True): | |
date=date+oneDay | |
if(self.isWD(date)): | |
return date | |
#month last WD | |
def monthLastWD(self, date): | |
tmp = date | |
if(type(date)==type(5)): | |
date = datetime.today() | |
date = date.replace(month=tmp) | |
date = date.replace(day=calendar.monthrange(date.year,date.month)[1]) | |
return self.givenOrBeforeDate(date) | |
#month first WD | |
def monthFirstWD(self, date): | |
tmp = date | |
if(type(date)==type(5)): | |
date = datetime.today() | |
date = date.replace(month=tmp) | |
date = date.replace(day=1) | |
return self.givenOrAfterDate(date) | |
def isWD(self, date): | |
if(self.isHoliday(date) or self.isWeekend(date)): | |
return False | |
else: | |
return True | |
#returns date in case that date is WD | |
def givenOrBeforeDate(self, date): | |
if(self.isWD(date)): | |
return date | |
else: | |
return self.previousWD(date) | |
#returns date in case that date is WD | |
def givenOrAfterDate(self, date): | |
if(self.isWD(date)): | |
return date | |
else: | |
return self.nextWD(date) | |
#returns date in case that date is WD | |
def givenOrBeforeDay(self, day): | |
date = datetime.today().replace(day=day) | |
return givenOrBeforeDate(date) | |
#returns date in case that date is WD | |
def givenOrAfterDay(self, day): | |
date = datetime.today().replace(day=day) | |
return givenOrAfterDate(date) | |
def isWeekend(self, date): | |
if(date.isoweekday()==6 or date.isoweekday()==7): | |
return True | |
else: | |
return False | |
def isHoliday(self, date): | |
if(date.strftime("%d/%m/%y") in self.holidays): | |
return True | |
else: | |
return False | |
if __name__ == '__main__': | |
wd = workingDays("m:\GPC\Holiday\holiday.txt") | |
print("Today is WD: %s"%wd.isWD(datetime.today())) | |
print("Saturday is WD: %s"%wd.isWD(date(2010,04,10))) | |
print("Xmas is WD: %s"%wd.isWD(date(2010,12,24))) | |
print("Previous WD: %s"%wd.previousWD(date(2010,12,25))) | |
print("Next WD: %s"%wd.nextWD(date(2010,12,24))) | |
print("Nearest WD after: %s"%wd.givenOrAfterDate(date(2010,12,24))) | |
print("Nearest WD after: %s"%wd.givenOrAfterDate(date(2010,12,23))) | |
print("Nearest WD before: %s"%wd.givenOrBeforeDate(date(2010,12,24))) | |
print("Nearest WD before: %s"%wd.givenOrBeforeDate(date(2010,12,23))) | |
print("Nearest WD after: %s"%wd.givenOrAfterDay(11)) | |
print("Nearest WD before: %s"%wd.givenOrBeforeDay(11)) | |
print("First month WD: %s"%wd.monthFirstWD(date(2010,5,1))) | |
print("First month WD: %s"%wd.monthFirstWD(date(2010,3,1))) | |
print("First month WD: %s"%wd.monthFirstWD(1)) | |
print("Last month WD: %s"%wd.monthLastWD(date(2010,5,1))) | |
print("Last month WD: %s"%wd.monthLastWD(date(2010,3,1))) | |
print("Last month WD: %s"%wd.monthLastWD(date(2010,1,10))) | |
print("Last month WD: %s"%wd.monthLastWD(1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment