これは簡単な実装です。
>>> def timeRange(timestr):
... t1, t2 = timestr.split("-")
... timeFormat = "%I:%M%p"
... t1AM = datetime.datetime.strptime(t1 + "AM", timeFormat)
... t1PM = datetime.datetime.strptime(t1 + "PM", timeFormat)
... t2 = datetime.datetime.strptime(t2, timeFormat)
...
... if (t2 - t1AM).seconds < (t2-t1PM).seconds:
... return t1AM.time(), t2.time()
... else:
... return t1PM.time(), t2.time()
>>> timeRange("11:00-2:00PM")
(datetime.time(11, 0), datetime.time(14, 0))
>>> timeRange("4:00-5:00PM")
(datetime.time(16, 0), datetime.time(17, 0))
>>> timeRange("11:00-1:00AM")
(datetime.time(23, 0), datetime.time(1, 0))
>>> timeRange("11:00-2:00PM")
(datetime.time(11, 0), datetime.time(14, 0))
>>> timeRange("12:00-1:00PM")
(datetime.time(12, 0), datetime.time(13, 0))
これはオブジェクトを返しますが、time
必要に応じてそれを datetime オブジェクトにまとめることができます。