N
N
Nikita M.2021-08-27 12:44:30
Python
Nikita M., 2021-08-27 12:44:30

How to make the right time intervals?

There is a script. In a certain period of time, the rate should be 10, in another period 12.
The problem is the following: how to make a night time period so that it automatically moves out the next day after 24:00.
When I put the time span like this

today_3am = datetime.today().replace(hours=3, minute=0)
today_8pm = datetime.today().replace(hours=20, minute=0)
if datetime.now() > today_8pm and datetime.now() < today_3am:
    return 0.5

Then when this code is checked at 08/27/2021 at 9 pm, then today_3am will be equal to 08/27/2021 03:00, which is already incorrect, because I need the next day. Throwing a day through timedelta is not suitable.

If there is any good library for this task - I will be glad if you recommend it, thanks.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Krostelev, 2021-08-27
@twistfire92

So you add a day according to the condition when initializing separately today_3am and separately today_8pm
If the user's time is more than 20:00, then add a day today_3am.
If the user's time is less than 3:00, then today_8pm subtract the day.
Well, something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question