OP的编辑提到真正的潜在问题是:
“从 X 日到 Y 日,有多少小时的带薪休假?”
我同意,我会以最直接和直接的方式计算,例如:
import datetime
import itertools
accrual_months_days = (1,1), (4,1), (7,1), (10,1)
def accruals(begin_date, end_date, hours_per=8):
"""Vacation accrued between begin_date and end_date included."""
cur_year = begin_date.year - 1
result = 0
for m, d in itertools.cycle(accrual_months_days):
if m == 1: cur_year += 1
d = datetime.date(cur_year, m, d)
if d < begin_date: continue
if d > end_date: return result
result += hours_per
if __name__ == '__main__': # examples
print accruals(datetime.date(2010, 1, 12), datetime.date(2010, 9, 20))
print accruals(datetime.date(2010, 4, 20), datetime.date(2012, 12, 21))
print accruals(datetime.date(2010, 12, 21), datetime.date(2012, 4, 20))
直接公式当然会更快,但如果没有错误,可能会很棘手——如果没有别的,这个“通过检查纠正”示例可以通过检查它们是否同意大量样本来自动校准更快的公式日期对(确保在后者中包括所有极端情况,例如季度的第一天和最后一天)。