如果这有帮助,我有个想法...
from datetime import datetime, timedelta
# This does not check for crossing from Sunday to Monday
def convert_dt(start_dt, journey):
begin_weekday, begin_hour, begin_minute = journey[0]
end_weekday, end_hour, end_minute = journey[1]
begin_dt = start_dt + timedelta(days=begin_weekday)
begin_dt += timedelta(hours=begin_hour, minutes=begin_minute)
end_dt = start_dt + timedelta(days=end_weekday)
end_dt += timedelta(hours=end_hour,minutes=end_minute)
return (begin_dt, end_dt)
def get_slot_journeys(start_dt, journeys):
next_dt = start_dt
slot_count = 60/5 * 24 * 7
slot_dict = {}
journey_dts = []
#convert journey begin and end to datetimes
for index in range(len(journeys[ begin_weekday ])):
next_journey = [(journeys[ begin_weekday ][index],
journeys[ begin_hour ][index],
journeys[ begin_minute ][index],),
(journeys[ end_weekday ][index],
journeys[ end_hour ][index],
journeys[ end_minute ][index],)
]
journey_dts.append(convert_dt(start_dt, next_journey))
for slot in range(slot_count):
slot_dict[next_dt] = 0
for journey_start, journey_end in journey_dts:
if next_dt >= journey_start and next_dt <= journey_end:
slot_dict[next_dt] = slot_dict[next_dt] + 1
next_dt += timedelta(minutes=(5))
return slot_dict
if __name__ == "__main__":
start_dt = datetime(2012, 1, 2, 0, 0)
journeys = { begin_weekday : [0, 0],
begin_hour : [14, 18],
begin_minute : [20, 30],
end_weekday : [0, 1],
end_hour : [19, 12],
end_minute : [15, 55],
}
slot_dict = get_slot_journeys(start_dt, journeys)
slot_keys = slot_dict.keys()
slot_keys.sort()
for key in slot_keys:
if slot_dict[key]:
print key, slot_dict[key]