ano*_*ard 2 python datetime calendar date python-dateutil
规则:
员工在每个季度后的第二天产生8小时的带薪休假时间.宿舍,特别是:
问题
使用python,我需要定义以下函数的内容:
def acrued_hours_between(start_date, end_date):
# stuff
return integer
Run Code Online (Sandbox Code Playgroud)
我目前正在使用Python,并想知道这样的事情的正确方法是什么.
我假设使用DateTime对象,可能还有dateutil模块,在这里会有所帮助,但我的大脑并没有因为某些原因而解决这个问题.
更新
我想象计算有点简单,因为问题是:
"从start_date到end_date会产生多少小时的付费休假?" 鉴于上述"规则".
OP的编辑提到真正的潜在问题是:
"从X-date到Y-date会产生多少小时的带薪休假?"
我同意,并且我会以最直接和最直接的方式计算,例如:
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))
Run Code Online (Sandbox Code Playgroud)
直接公式当然会更快,但如果没有错误可能会很棘手 - 如果没有别的,这个"通过检查正确"的例子可以用来自动校准更快的一个,通过检查它们是否同意大样本日期对(一定要包括后者的所有角落情况,例如当然是季度的第一天和最后一天).
| 归档时间: |
|
| 查看次数: |
538 次 |
| 最近记录: |