S
S
Safronov_Alexei2020-07-14 10:55:31
Python
Safronov_Alexei, 2020-07-14 10:55:31

How to understand if one time period overlaps another?

Hi Habr!

The question is the following
. I have two time intervals: 09:20 - 10:20 and 10:21 - 11:21

How to make it so that when entering the next time interval, it checks if it captures these intervals.
That is, what would sweep away options like 08:00 - 12:00, 09:25 - 16:00, etc.

I can’t solve the problem, please help)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2020-07-14
@Safronov_Alexei

It all depends on the library you are using. For example datetime like this:

from datetime import datetime

a = datetime(year=2020, month=1, day=1, hour=9, minute=0, second=0)
b = datetime(year=2020, month=1, day=1, hour=12, minute=0, second=0)

c = datetime(year=2020, month=1, day=1, hour=18, minute=0, second=0)

if a < c < b:
    print('In range')
else:
    print('Out of range')

Datetime objects can be compared to each other.

D
Dr. Bacon, 2020-07-14
@bacon

ok google, "python time interval intersection"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question