Skip to content

Instantly share code, notes, and snippets.

@boyxuper
Last active December 18, 2015 06:08
Show Gist options
  • Save boyxuper/5737431 to your computer and use it in GitHub Desktop.
Save boyxuper/5737431 to your computer and use it in GitHub Desktop.
solar terms algorithm which didn't pass the test
# -*- coding: utf-8 -*-
__author__ = 'johnx'
__date__ = '6/8/13 2:47 PM'
#see [Y*D+C]-L
#@see http://bbs.csdn.net/topics/340009030
D = .2422
C_21th = {
u'小寒': (1, 5.4055),
u'大寒': (1, 20.12),
u'立春': (2, 3.87),
u'雨水': (2, 18.73),
u'惊蛰': (3, 5.63),
u'春分': (3, 20.646),
u'清明': (4, 4.81),
u'谷雨': (4, 20.1),
u'立夏': (5, 5.52),
u'小满': (5, 21.04),
u'芒种': (6, 5.678),
u'夏至': (6, 21.37),
u'小暑': (7, 7.108),
u'大暑': (7, 22.83),
u'立秋': (8, 7.5),
u'处暑': (8, 23.13),
u'白露': (9, 7.646),
u'秋分': (9, 23.042),
u'寒露': (10, 8.318),
u'霜降': (10, 23.438),
u'立冬': (11, 7.438),
u'小雪': (11, 22.36),
u'大雪': (12, 7.18),
u'冬至': (12, 21.94),
}
SOLAR_EXCEPTIONS = {
2026: {u'雨水': (2, 18), },
2084: {u'春分': (3, 20), },
2008: {u'小满': (5, 21), },
2016: {u'小暑': (7, 7), },
2002: {u'立秋': (8, 8), },
2089: {u'霜降': (10, 23), u'立冬': (11, 7), },
2021: {u'冬至': (12, 21), },
2019: {u'小寒': (1, 5), },
2082: {u'大寒': (1, 20), },
}
def calc_solar_term(year):
terms = {}
#Y is xx in 20xx
Y = year % 100
for name, (month, C) in C_21th.items():
fix = -1 if month in (1, 2) else 0
date = int(Y * D + C) - int((Y + fix) / 4.0)
terms[name] = month, date
if year in SOLAR_EXCEPTIONS:
terms.update(SOLAR_EXCEPTIONS[year])
return terms
def print_solar(year):
items = calc_solar_term(year).items()
items = sorted(items, key=lambda x: x[1][0])
for name, (month, day) in items:
print u'%s: %s年%s月%s日' % (name, year, month, day)
def test():
assert calc_solar_term(2058)[u'立春'] == (2, 3)
assert calc_solar_term(2008)[u'雨水'] == (2, 19)
assert calc_solar_term(2026)[u'雨水'] == (2, 18)
assert calc_solar_term(2088)[u'惊蛰'] == (3, 4)
assert calc_solar_term(2092)[u'春分'] == (3, 19)
assert calc_solar_term(2084)[u'春分'] == (3, 20)
assert calc_solar_term(2088)[u'清明'] == (4, 4)
assert calc_solar_term(2088)[u'谷雨'] == (4, 19)
assert calc_solar_term(2088)[u'立夏'] == (5, 4)
assert calc_solar_term(2088)[u'小满'] == (5, 20)
assert calc_solar_term(2088)[u'芒种'] == (6, 4)
assert calc_solar_term(2088)[u'夏至'] == (6, 20)
assert calc_solar_term(2088)[u'小暑'] == (7, 6)
assert calc_solar_term(2088)[u'大暑'] == (7, 22)
assert calc_solar_term(2088)[u'立秋'] == (8, 6)
assert calc_solar_term(2088)[u'处暑'] == (8, 22)
assert calc_solar_term(2088)[u'白露'] == (9, 6)
assert calc_solar_term(2088)[u'秋分'] == (9, 22)
assert calc_solar_term(2088)[u'寒露'] == (10, 7)
assert calc_solar_term(2088)[u'霜降'] == (10, 22)
assert calc_solar_term(2088)[u'立冬'] == (11, 6)
assert calc_solar_term(2088)[u'小雪'] == (11, 21)
assert calc_solar_term(2088)[u'大雪'] == (12, 6)
assert calc_solar_term(2088)[u'冬至'] == (12, 21)
assert calc_solar_term(2088)[u'大雪'] == (12, 6)
assert calc_solar_term(2089)[u'大寒'] == (1, 19)
if __name__ == '__main__':
print_solar(2008)
test()
# -*- coding: utf-8 -*-
__author__ = 'johnx'
__date__ = '6/8/13 2:47 PM'
from datetime import datetime
solarTerms = (
u'小寒', u'大寒', u'立春', u'雨水',
u'惊蛰', u'春分', u'清明', u'谷雨',
u'立夏', u'小满', u'芒种', u'夏至',
u'小暑', u'大暑', u'立秋', u'处暑',
u'白露', u'秋分', u'寒露', u'霜降',
u'立冬', u'小雪', u'大雪', u'冬至',
)
sTermInfo = (
0, 21208, 42467, 63836,
85337, 107014, 128867, 150921,
173149, 195551, 218072, 240693,
263343, 285989, 308563, 331033,
353350, 375494, 397447, 419210,
440795, 462224, 483532, 504758,
)
def calc_solar_term(year):
terms = {}
for index, (name, const) in enumerate(zip(solarTerms, sTermInfo)):
month = (index // 2) + 1
time_stamp = (31556925974.7 * (year - 1900) + sTermInfo[index] * 60000) - 2208549300000
date = datetime.utcfromtimestamp(time_stamp // 1000).day
terms[name] = month, date
return terms
def print_solar(year):
items = calc_solar_term(year).items()
items = sorted(items, key=lambda x: x[1][0])
for name, (month, day) in items:
print u'%s: %s年%s月%s日' % (name, year, month, day)
def test():
assert calc_solar_term(2058)[u'立春'] == (2, 3)
assert calc_solar_term(2008)[u'雨水'] == (2, 19)
assert calc_solar_term(2026)[u'雨水'] == (2, 18)
assert calc_solar_term(2088)[u'惊蛰'] == (3, 4)
assert calc_solar_term(2092)[u'春分'] == (3, 19)
assert calc_solar_term(2084)[u'春分'] == (3, 20)
assert calc_solar_term(2088)[u'清明'] == (4, 4)
assert calc_solar_term(2088)[u'谷雨'] == (4, 19)
assert calc_solar_term(2088)[u'立夏'] == (5, 4)
assert calc_solar_term(2088)[u'小满'] == (5, 20)
assert calc_solar_term(2088)[u'芒种'] == (6, 4)
assert calc_solar_term(2088)[u'夏至'] == (6, 20)
assert calc_solar_term(2088)[u'小暑'] == (7, 6)
assert calc_solar_term(2088)[u'大暑'] == (7, 22)
assert calc_solar_term(2088)[u'立秋'] == (8, 6)
assert calc_solar_term(2088)[u'处暑'] == (8, 22)
assert calc_solar_term(2088)[u'白露'] == (9, 6)
assert calc_solar_term(2088)[u'秋分'] == (9, 22)
assert calc_solar_term(2088)[u'寒露'] == (10, 7)
assert calc_solar_term(2088)[u'霜降'] == (10, 22)
assert calc_solar_term(2088)[u'立冬'] == (11, 6)
assert calc_solar_term(2088)[u'小雪'] == (11, 21)
assert calc_solar_term(2088)[u'大雪'] == (12, 6)
assert calc_solar_term(2088)[u'冬至'] == (12, 21)
assert calc_solar_term(2088)[u'大雪'] == (12, 6)
assert calc_solar_term(2089)[u'大寒'] == (1, 19)
if __name__ == '__main__':
print print_solar(2013)
print test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment