上述程序修改成 如果可用工作时间段包含了非工作时间,需要将其算作连续可用时间
时间: 2023-12-22 07:05:29 浏览: 59
好的,针对您的需求,您可以将程序修改如下:
1. 在判断连续可用时间的时候,先判断当前时间是否在工作时间段内,如果不在,则需要将其算作连续可用时间。
2. 在计算连续可用时间的时候,需要将工作时间段和非工作时间段合并成一个时间段列表,然后按照时间顺序进行排序,最后遍历时间段列表,计算连续可用时间。
以下是修改后的程序代码示例:
```python
from datetime import datetime, timedelta
def get_continuous_available_time(time_slots):
# 合并工作时间段和非工作时间段
time_slots.append(('00:00', '00:00'))
merged_time_slots = []
start_time, end_time = None, None
for time_slot in sorted(time_slots):
if not start_time:
start_time, end_time = time_slot
elif time_slot[0] <= end_time:
end_time = max(end_time, time_slot[1])
else:
merged_time_slots.append((start_time, end_time))
start_time, end_time = time_slot
merged_time_slots.append((start_time, end_time))
# 计算连续可用时间
continuous_time = timedelta()
last_end_time = datetime.strptime('00:00', '%H:%M')
for time_slot in merged_time_slots:
start_time = datetime.strptime(time_slot[0], '%H:%M')
end_time = datetime.strptime(time_slot[1], '%H:%M')
if start_time > last_end_time:
continuous_time += start_time - last_end_time
continuous_time += end_time - start_time
last_end_time = end_time
return continuous_time
```
希望这个修改后的程序能够满足您的需求。
阅读全文