I have a executor that executes phone calles according to what user specifies.
I want to schedule time only in specific range of hours during the day, for example from 8AM to 6PM.
The executor is working 24 hours per day and is checking records in database whether to execute a phone call or not at the moment.
Is there any tricky way to schedule future calls by using python's datetime
and setting somehow these working hours with given frequency and number of calls.
For example I want to schedule 4 calls with 1 hour frequency. Let's say it's 4PM. Next call should be scheduled on 4PM, 5PM and next one - next day at 8AM, 10AM.
I'd expect datetime
to work in this conditions like below:
def schedule(no_of_calls: int, frequency: timedelta):
dt = get_a_magic_datetime_with_hour_range()
value = dt.now() # this will get 4PM in above's example. Though if it would be 10PM it would return 8AM next day.
for i in range(no_of_calls):
save_to_db(value)
value += frequency
I know I could create my own working_datetime
which would make proper validations and return values from within range of working hours, but maybe there is already something like this.
If you still don't get the idea - think of this like of parking meter - if you pay outside of paying hours - it will count from the start of paying hours next day (or same if it's past 12AM).