计算两日期之间的工作天数, 过滤休息日
程序员文章站
2022-03-03 10:12:18
...
import datetime
# 计算两个日期之间的工作日数,非天数.
class workDays():
def __init__(self, start_date, end_date, days_off=None):
"""days_off:休息日,默认周六日, 以0(星期一)开始,到6(星期天)结束, 传入tupple
没有包含法定节假日,
"""
self.start_date = start_date
self.end_date = end_date
self.days_off = days_off
if self.start_date > self.end_date:
self.start_date,self.end_date = self.end_date, self.start_date
if days_off is None:
self.days_off = 5,6
# 每周工作日列表
self.days_work = [x for x in range(7) if x not in self.days_off]
def workDays(self):
"""实现工作日的 iter, 从start_date 到 end_date , 如果在工作日内,yield 日期
"""
# 还没排除法定节假日
tag_date = self.start_date
while True:
if tag_date > self.end_date:
break
if tag_date.weekday() in self.days_work:
yield tag_date
tag_date += datetime.timedelta(days=1)
def daysCount(self):
"""工作日统计,返回数字"""
return len(list(self.workDays()))
def weeksCount(self, day_start=0):
"""统计所有跨越的周数,返回数字
默认周从星期一开始计算
"""
day_nextweek = self.start_date
while True:
if day_nextweek.weekday() == day_start:
break
day_nextweek += datetime.timedelta(days=1)
# 区间在一周内
if day_nextweek > self.end_date:
return 1
weeks = ((self.end_date - day_nextweek).days + 1)/7
weeks = int(weeks)
if ((self.end_date - day_nextweek).days + 1)%7:
weeks += 1
if self.start_date < day_nextweek:
weeks += 1
return weeks
测试:
a=datetime.date(2014,5,15)
b=datetime.date(2014,5,13)
work = workDays(a,b)
for day in work.workDays():
print day,"星期",day.weekday()+1
print "总天数:", work.daysCount()
print "总周数:", work.weeksCount(day_start=6)
结果:
2014-05-13 星期 2
2014-05-14 星期 3
2014-05-15 星期 4
总天数: 3
总周数: 1
转载于:https://my.oschina.net/u/243080/blog/265549
上一篇: 【LeetCode】226. 翻转二叉树
下一篇: 3位有效数字