上班时间为8:45,下班时间为17:30,中午休息时间为11:45-13:30,假如请假开始时间为2023-05-31 7:30:00结束时间为2023-06-02 15:30:00,计算请假天数的代码
时间: 2024-02-24 17:57:39 浏览: 130
好的,以下是计算请假天数的 Python 代码:
```python
import datetime
start_time = datetime.datetime(2023, 5, 31, 7, 30, 0)
end_time = datetime.datetime(2023, 6, 2, 15, 30, 0)
work_start_time = datetime.datetime(1900, 1, 1, 8, 45, 0)
work_end_time = datetime.datetime(1900, 1, 1, 17, 30, 0)
lunch_start_time = datetime.datetime(1900, 1, 1, 11, 45, 0)
lunch_end_time = datetime.datetime(1900, 1, 1, 13, 30, 0)
total_days = (end_time.date() - start_time.date()).days + 1
leave_days = 0
for i in range(total_days):
current_day = start_time + datetime.timedelta(days=i)
if work_start_time <= current_day.time() <= lunch_start_time:
leave_days += 0.5
elif lunch_end_time <= current_day.time() <= work_end_time:
leave_days += 0.5
elif current_day.date().weekday() >= 5:
leave_days += 0
else:
leave_days += 1
print(f"请假天数为{leave_days}天")
```
代码中,我们先定义了请假开始时间和结束时间,以及上班时间、下班时间和中午休息时间。然后计算出请假总天数,并通过循环每一天的时间来判断该天是否需要计算请假天数。最后输出请假天数。
需要注意的是,这段代码假设周六周日是双休日,不需要计算请假天数。如果你的公司的双休日不是周六周日,需要对代码进行相应的修改。
阅读全文