I wrote some lines of code (python 3.7) to check for a given in time, which timeslot in a time table it matches. ,它匹配时间表中的哪个时隙。 For this purpose I use pythons datetime
module. My problem is that I don't know how to check this for a in time. 进行检查。 I ran through a lot of related subjects while searching for an answer, but I could not find anything that comes close enough.
I got this working for a (timestamp) (时间戳记)上进行了此操作
pseudo code :
time_point = 11:00
timeslot_1 = 10:00 - 12:00
timeslot_2 = 12:00 - 14:00
def check_interval(time_unit):
if time_unit within timeslot_1:
return timetable_interval_1
if time_unit within timeslot_2:
return timetable_interval_2
else:
return False
check_interval(time_point)
10:00 - 12:00
But I would like to know this for a (interval between two timestamps) (两个时间戳记之间的间隔)
pseudo code :
time_period = 11:00 - 12:30
timeslot_1 = 10:00 - 12:00
timeslot_2 = 12:00 - 14:00
def check_interval(time_unit):
if time_unit within timeslot_1:
return timeslot_1
if time_unit within timeslot_2:
return timeslot_2
if time_unit within timeslot_1 and within timeslot_2:
return timeslot_1 + timeslot_2
else:
return False
check_interval(time_period)
10:00 - 14:00
You could use time
to define time points, and tuples of times to represent intervals. For example:
from datetime import time
time_period = time(11), time(12, 30)
timeslot_1 = time(10), time(12)
timeslot_2 = time(12), time(14)
def check_interval(period):
if timeslot_1[0] <= period[0] <= period[1] <= timeslot_1[1]:
return timeslot_1
if timeslot_2[0] <= period[0] <= period[1] <= timeslot_2[1]:
return timeslot_2
if timeslot_1[0] <= period[0] <= period[1] <= timeslot_2[1]:
return timeslot_1[0], timeslot_2[1]
else:
return False
print(check_interval((time(11), time(12, 30))))
# prints (datetime.time(10, 0), datetime.time(14, 0))
print(check_interval((time(9), time(12, 30))))
# prints False
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.