Skip to content

Instantly share code, notes, and snippets.

@ssato
Created May 12, 2016 10:34
Show Gist options
  • Save ssato/cf04a70ba9ccfcb5a14667a479a904ce to your computer and use it in GitHub Desktop.
Save ssato/cf04a70ba9ccfcb5a14667a479a904ce to your computer and use it in GitHub Desktop.
国民の祝日を計算してみるテスト
ssato@x1carbon% ipython
Python 2.7.11 (default, Mar 31 2016, 20:46:51)
Type "copyright", "credits" or "license" for more information.
IPython 3.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import calendar, datetime, csv, itertools, operator
In [2]: to_date = lambda ymd, sep='/': datetime.date(*(int(x) for x in ymd.split(sep)))
In [3]: is_dates = lambda *days: all(d.startswith("20") for d in days)
In [4]: holidays = [to_date(d) for d in itertools.chain(*((t[1], t[3]) for t in csv.reader(open("syukujitsu.csv")) if is_dates(*operator.itemgetter(1,3)(t))))]
In [5]: holidays[:5]
Out[5]:
[datetime.date(2016, 1, 1),
datetime.date(2017, 1, 1),
datetime.date(2016, 1, 11),
datetime.date(2017, 1, 9),
datetime.date(2016, 2, 11)]
In [6]: rhs = sorted(holidays, reverse=True)
In [7]: calendar.weekday?
Signature: calendar.weekday(year, month, day)
Docstring:
Return weekday (0-6 ~ Mon-Sun) for year (1970-...), month (1-12),
day (1-31).
File: /usr/lib64/python2.7/calendar.py
Type: function
In [8]: [h + datetime.timedelta(days=1) for h in holidays if calendar.weekday(h.year, h.month, h.day) == 6] # 国民の祝日に関する法律 第3条第2項による振替休日
Out[8]: [datetime.date(2017, 1, 2), datetime.date(2016, 3, 21)]
In [9]: [t[1] for t in itertools.izip(rhs, rhs[1:]) if operator.sub(*t).days == 1]
Out[9]:
[datetime.date(2017, 5, 4),
datetime.date(2017, 5, 3),
datetime.date(2016, 5, 4),
datetime.date(2016, 5, 3)]
In [10]: [t[1] for t in itertools.izip(rhs, rhs[1:]) if operator.sub(*t).days == 2] # 国民の祝日に関する法律 第3条第3項による休日 ... になるはず (この期間では 0)
Out[10]: []
In [11]:
@ssato
Copy link
Author

ssato commented May 25, 2016

国民の祝日の CSV データ (syukujitsu.csv) はこちらから: http://www8.cao.go.jp/chosei/shukujitsu/gaiyou.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment