国民の祝日(日本)をリストで出してみる
汚いソースでスイマセンが、作ったのでさらしてみる。
むしろ、添削&デバッグ(現在未デバッグ)大歓迎です!他力本願寺!
なお、春分の日/秋分の日は2011〜2099年までしか対応していません。
また、事前にdateutilモジュールを追加しておいてください。
http://niemeyer.net/python-dateutil
#!/usr/bin/python # -*- coding: utf-8 -*- # nationalholidays.py import datetime from dateutil.rrule import * #http://www8.cao.go.jp/chosei/shukujitsu/gaiyou.html def JPNnationalholidays(fromdate,todate): hdays = [] if fromdate > todate: return hdays #日にちが決まっているもの(月,日) holiday1=[ (1,1),(2,11),(4,29),(5,3),(5,4),(5,5),(11,3),(11,23),(12,23) ] for (mo,dy) in holiday1: tmp = list(rrule(YEARLY,bymonth=mo,bymonthday=dy,dtstart=fromdate,until=todate)) hdays = hdays+tmp #春分の日 #秋分の日(注意:秋分の日が9月第三週の水曜日の場合は、月曜日が敬老の日の為、国民の祝日が前の日に追加される) pointer = fromdate while pointer <= todate: shunbun = Shunbun_no_hi(pointer.year) shuubun = Shuubun_no_hi(pointer.year) if(pointer <= shunbun <= todate): hdays.append(shunbun) if(pointer <= shuubun <= todate): hdays.append(shuubun) keirou = rrule(YEARLY,bymonth=9,byweekday=MO(3),dtstart=pointer.replace(day=1),count=1) delta = shuubun - keirou[0] if delta == datetime.timedelta(days=2): hdays.append(shuubun+datetime.timedelta(days=-1)) nextyear = pointer.year+1 pointer = pointer.replace(year=nextyear,month=3,day=1) #振替休日(GW中の処理付き) for d in hdays: if d.weekday() == 6: if d.month == 5 and ( 3 <= d.day <= 5): tmp = d.replace(day=6) else: tmp = d + datetime.timedelta(days=+1) if tmp <= todate: hdays.append(tmp) #ハッピーマンデー(月,第n週の月曜日) happymonday = [ (1,2),(7,3),(9,3),(10,2) ] for (mo,wk) in happymonday: tmp = list(rrule(YEARLY,bymonth=mo,byweekday=MO(wk),dtstart=fromdate,until=todate)) hdays = hdays+tmp hdays.sort() return hdays #c.f. http://ja.wikipedia.org/wiki/%E6%98%A5%E5%88%86%E3%81%AE%E6%97%A5 def Shunbun_no_hi(year): y = year % 4 May19 = datetime.datetime(year,3,19) May20 = datetime.datetime(year,3,20) May21 = datetime.datetime(year,3,21) if y == 0: if 2011 <= year <= 2088: return May20 else: return May19 elif y == 1: return May20 elif y == 2: if 2011 <= year <= 2012: return May21 else: return May20 else: if 2011 <= year <= 2055: return May21 else: return May20 def Shuubun_no_hi(year): y = year % 4 Sep22 = datetime.datetime(year,9,22) Sep23 = datetime.datetime(year,9,23) if y == 0: return Sep22 elif y == 1: if 2011 <= year <= 2041: return Sep23 else: return Sep22 elif y == 2: if 2011 <= year <= 2074: return Sep23 else: return Sep22 else: return Sep23
こんな感じでお使いください。
import datetime as d from nationalholidays import JPNnationalholidays as nh st=d.datetime(2011,1,1) ed=d.datetime(2011,12,31) #2011年の国民の祝日をリストで出す h=nh(st,ed) print h