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
  Check time within specific time ranges basvdm 3 587 Jan-20-2025, 05:10 PM
Last Post: Gribouillis
Question [SOLVED] [datetime.strptime] ValueError: time data 'foo' does not match format 'bar' Winfried 1 1,187 Jan-02-2025, 02:09 AM
Last Post: lyly19
  Help about a specific time selection QJZ 0 394 Dec-01-2024, 11:25 AM
Last Post: QJZ
  How to move an object over time rather than instantly? temlotresid6 3 1,607 Oct-23-2024, 11:20 AM
Last Post: temlotresid6
  Is there a difference between Python’s time.sleep and win32api.Sleep? phpjunkie 4 1,063 Sep-21-2024, 05:17 PM
Last Post: aakritiintelligence
  How to insert text time - into video frames? oxidian 0 1,092 Aug-25-2024, 04:51 PM
Last Post: oxidian
  Using RTC time in Rasberry Pi Programs sab201 1 859 Aug-18-2024, 05:50 PM
Last Post: snippsat
  Schedule exit a program at a specific time 4 am every day. chubbychub 3 1,427 May-17-2024, 03:45 PM
Last Post: chubbychub
  Filer and sort files by modification time in a directory tester_V 5 2,307 May-02-2024, 05:39 PM
Last Post: tester_V
Question Convert UTC now() to local time to compare to a strptime() Calab 2 2,057 Apr-29-2024, 07:24 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