datetime.date
对象,并将其作为一年重新写入。
from datetime import date
def get_season(d):
d = date(year=1900, month=d.month, day=d.day)
if date(1900, 6, 25) <= d <= date(1900, 8, 22):
return 3
elif date(1900, 4, 1) <= d <= date(1900, 6, 24) or \
date(1900, 8, 22) <= d <= date(1900, 10, 31) or \
date(1900, 12, 17) <= d <= date(1900 12, 31) or \
date(1900, 1, 1) == d:
return 2
elif date(1900, 9, 1) <= d <= date(1900, 3, 31) or \
date(1900, 11, 1) <= d <= date(1900, 12, 16) or \
date(1900, 1, 2) <= d <= date(1900, 3, 31):
return 1
这也使得创建边界对变得更加容易。
def get_season(d):
d = date(year=1900, month=d.month, day=d.day)
boundarydict = {1: [(date(1900, 9, 1), date(1900, 3, 31)),
(date(1900, 11, 1), date(1900, 12, 16)),
(date(1900, 1, 2), date(1900, 3, 31))],
2: [(date(1900, 4, 1), date(1900, 6, 24)),
(date(1900, 8, 22), date(1900, 10, 31)),
(date(1900, 12, 17), date(1900, 12, 31)),
(date(1900, 1, 1), date(1900, 1, 1))], # note this one!
3: [(date(1900, 6, 25), date(1900, 8, 22))]}
for retval, boundaries in boundarydict.values():
if any(a <= d <= b for a, b in boundaries):
return retval