Python Forum
Find if time is between start and end time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find if time is between start and end time
#1
Hi,

I have a list of time intervals with start and end time. For example the first interval is between 12:30-13:00. How can I quickly check if e.g. 1745 (17:45) is included between any of the time intervals?

period = [[1230, 1300], [1400, 1500],[900, 1000], [1530, 1800],[1930, 2100]]

I appreciate your help.
Reply
#2
Assuming that the set of periods is a fixed set of non overlapping intervals and you have potentially many searches to do, it seems to me that the best solution is to use the bisect module
import bisect

period = [[1230, 1300], [1400, 1500],[900, 1000], [1530, 1800],[1930, 2100]]

# prepare two helper arrays
scale, intervals = zip(
    *sorted((end, inter) for inter in period for end in inter))
scale = list(scale)
scale.append(2401)

class NotFound(RuntimeError):
    pass

def find_interval(time):
    idx = bisect.bisect_left(scale, time)
    if not (idx % 2) and (scale[idx] != time):
        raise NotFound(time)
    return intervals[idx]

if __name__ == '__main__':
    i = find_interval(1735)
    print(i)
Reply
#3
Can you be sure an interval does not cross midnight (such as [2300, 0130])? If so it's trivial.
period = [[1230, 1300], [1400, 1500],[900, 1000], [1530, 1800],[1930, 2100]]
def in_interval(test,periods_list):
  if test >= periods_list[0] and test <= periods_list[1]:
    return True
  else:
    return False

val_to_test = int(input("Enter value"))
for intervals in period:
  if in_interval(val_to_test,intervals):
    print(f'In interval {intervals}')
Reply
#4
Great code. Thank you both!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Schedule exit a program at a specific time 4 am every day. chubbychub 3 332 May-17-2024, 03:45 PM
Last Post: chubbychub
  Filer and sort files by modification time in a directory tester_V 5 499 May-02-2024, 05:39 PM
Last Post: tester_V
Question Convert UTC now() to local time to compare to a strptime() Calab 2 353 Apr-29-2024, 07:24 PM
Last Post: deanhystad
  Date Time Series Help...Please spra8560 2 468 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Python date format changes to date & time 1418 4 784 Jan-20-2024, 04:45 AM
Last Post: 1418
  time difference bettwenn logs enkliy 14 1,204 Nov-21-2023, 04:51 PM
Last Post: rob101
Question Need Help with Vehicle Routing Problem with Time Windows (VRPTW) in Python kasper321421312 1 699 Nov-10-2023, 08:19 PM
Last Post: snippsat
  How do I stream and record at the same time with arducam? traderjoe 0 531 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 627 Oct-03-2023, 11:15 AM
Last Post: snippsat
  [Python 2.7] Why can't I press [ESC] a fourth time? Ashwood 3 745 Aug-27-2023, 02:01 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020