Skip to content

Instantly share code, notes, and snippets.

@a-yasui
Created August 9, 2012 09:55
Show Gist options
  • Save a-yasui/3302806 to your computer and use it in GitHub Desktop.
Save a-yasui/3302806 to your computer and use it in GitHub Desktop.
明治元年から平成二十四年までの毎年の日数を出力するプログラム
#!/usr/bin/env python
# -*- coding: utf_8 -*-
def yearToWareki (year):
r"""
result: tuple [('元号', 年, 日数), ('元号', 年, 日数)]
2つ目の要素はほぼないが、変わり目の時にある
"""
# 明治元年: 1868
# 明治45年: 1912 (~7/29)
# 大正元年: 1912 (7/30~)
# 大正15年: 1926 (~12/24)
# 昭和元年: 1926 (12/25~)
# 昭和64年: 1989 (~1/7)
# 平成元年: 1989 (1/8~)
y = 365
if is_uru(year):
y += 1
def gan (_y, _start, _gou):
toshi = u"元" if _start == _y else str(_y - _start + 1)
return u"%s%s年" % (_gou, toshi)
if 1868 == year:
return [(u"明治元年", year, y)]
elif 1869 <= year <= 1911:
return [(gan(year, 1868, u"明治"), year, y)]
elif year == 1912:
return [(u'明治45年', year, 205), (u'大正元年', year, 365-205)]
elif 1913 <= year <= 1925:
return [(gan(year, 1912, u"大正"), year, y)]
elif year == 1926:
return [(u"大正15年", year, 365 - 7), (u"昭和元年", year, 7)]
elif 1927 <= year <= 1988:
return [(gan(year, 1926, u"昭和"), year, y)]
elif year == 1989:
return [(u"昭和64年", year, 8), (u"平成元年", year, 365 - 8)]
elif 1990 <= year:
return [(gan(year, 1989, u"平成"), year, y)]
def is_uru (year):
if year % 4 == 0:
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
return True
return False
def out_year (year):
y = 365
if is_uru(year):
y += 1
print "%d is `%d'" % (year, y)
if __name__ == "__main__":
buff = []
for y in range(1868, 2015):
buff = yearToWareki(y)
for b in buff:
print "%s[%d] => %d" % b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment