E
E
Elvis2020-07-23 23:13:51
Python
Elvis, 2020-07-23 23:13:51

Is the calculation of the day correct?

Hey!
On the server, you need to make a selection from the database depending on the time.
The condition is as follows: the sampling occurs from 4:00 of the current day to 3:59 of the next day, the
sampling occurs every 15 minutes.
That is, if the script is launched on 23.07 at 12:15, then the selection should be from 23.07 4:00 to the current moment, that is, just more than 23.07 4:00
and if the script is launched on 24.07 at 2:30, then the selection is the same from 23.07 4:00 to date.
but as soon as it crosses the 4:00 boundary, the current day is already selected as the start date.

sort of wrote a selection of the day to insert into the script:

import datetime
a = datetime.datetime.today()
b = a.day
if a.hour < 4:
    b = (a - datetime.timedelta(1)).day
print(b)

Interested in whether the script is correct and in general the construction is (a - datetime.timedelta(1)).daylegal?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-07-24
@Dr_Elvis

everything is ok,
everything is legal - the usual manipulation of the object
, well, you can do it, but it's all the same

from datetime import datetime, timedelta
N = 0 if datetime.today().hour > 4 else 1
b = (datetime.today() - timedelta(N)).day

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question