Pythonのライブラリ「jpholiday」を使うと祝日を判定できるけど、土日と、それからカスタムで追加したい日付には対応してないわよね。
対処方法は色々あるけど、まずは無難のところから。
import datetime import jpholiday import os class Hol():
def __init__(self,today): self.today = today self.yos = ["月","火","水","木","金","土","日"] #self.handan(self.today) self.holiday = None self.answer = 0 self.days() def handan(self,today): self.name = jpholiday.is_holiday_name(today) if(self.name != None): self.holiday = self.name elif(self.holiday9(self.today) != None): self.holiday = "XHOLIDAY" else: self.yobi = self.yos[self.today.weekday()] self.name = self.yobi + "曜日" if(self.today.weekday() > 4): self.holiday = self.name def days(self): self.answer = 0 if(jpholiday.is_holiday_name(self.today) == None): if(self.holiday9(self.today) == None): while 1: self.answer = self.answer + 1 self.today = self.today + datetime.timedelta(days=1) self.handan(self.today) if(self.holiday != None): break def holiday9(self,today): re1 = None csv_holiday = os.environ["HOME"] + "/bin/holiday_all.csv" s = "" now = today.strftime("%Y%m%d\n") # rstrip()が本道だが処理時間が長いので with open(csv_holiday) as f: for s_line in f: if(now == s_line): re1 = "HOLIDAY" break return(re1)
print(Hol(datetime.datetime.now()).answer)
これを実行すると、お休みまであと何日かと数字で返す……と思うわ。まだデバッグ中だけどね☆
オブジェクトを作ると、.answerに休日までの日数が入るの。 判定材料はjphlidayの出力、土日かどうか、そして ユーザホーム/bin/holiday_all.csv ってファイルに YYYY/mm/dd 形式で日付を並べておくと追加の判定材料にするわ。
あとは好きにしてね。