Calculating the remaining time to a repeating event in Python
The scenario is as follows: given a totally arbitrary 开发者_运维知识库starting date in UTC there is an event that repeats every 24 hours. I need to calculate, given the current local time, how much time is left before the next event.
Ideally an ideal function would do:
time_since_start = now - start_time
remaining_seconds = time_remaining(time_since_start)
EDIT: Some clarifications. start_time
defines the "epoch", the global start time since the event started repeating. Secondly, now
is an arbitrary time occurring after start_time
, local.
The issue is not calculating the occurrence of the next event (which would be simply adding 24 hours to start_time
) but if I pick a time that falls between one event and the other, how much time is left before the next event.
I'd opt seconds as they can be quickly parsed into days, minutes and hours. Usage of datetime
would be preferred (but not required) over other third-party modules as I am trying to reduce the number of dependencies.
I tried using datetime
and timedelta
, but the difference is always incorrect. What is the best way to proceed there?
What you want is start_time - now + (one day)
精彩评论