2019-03-13 20:33:20 +01:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
min_sleep_minutes = 0
|
|
|
|
|
|
|
|
class LoopTimer (object):
|
2019-03-13 20:33:20 +01:00
|
|
|
"""Manages loop times and sleeps until
|
|
|
|
next loop."""
|
2019-03-24 21:35:05 +01:00
|
|
|
def __init__ (self, loop_interval, run_on_hour = False):
|
2019-03-19 16:34:14 +01:00
|
|
|
self.interval = int(str(loop_interval))
|
2019-03-13 20:33:20 +01:00
|
|
|
self.on_hour = run_on_hour
|
|
|
|
self.loop_history = []
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def begin_loop (self):
|
2019-03-13 20:33:20 +01:00
|
|
|
begin_time = datetime.now()
|
|
|
|
print('\n_________Starting new loop___________')
|
|
|
|
print('Date: ' + begin_time.strftime('%a %d %b %y') + ', time: ' + begin_time.strftime('%H:%M') + '\n')
|
|
|
|
self.__add_beginning__(begin_time)
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def __add_beginning__ (self, time):
|
2019-03-13 20:33:20 +01:00
|
|
|
self.loop_history.append((time,))
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def __add_ending__ (self, time):
|
2019-03-13 20:33:20 +01:00
|
|
|
current = self.get_current()
|
|
|
|
self.loop_history[-1] = (current[0], time)
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def end_loop (self):
|
2019-03-13 20:33:20 +01:00
|
|
|
end_time = datetime.now()
|
|
|
|
self.__add_ending__(end_time)
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def get_current (self):
|
2019-03-13 20:33:20 +01:00
|
|
|
return self.loop_history[-1]
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def time_until_next (self):
|
2019-03-13 20:33:20 +01:00
|
|
|
interval_duration = timedelta(minutes=self.interval)
|
|
|
|
loop_duration = self.get_last_duration()
|
|
|
|
sleep_time = interval_duration - loop_duration
|
|
|
|
if self.on_hour:
|
|
|
|
time_until_hour = self.get_time_to_next_hour()
|
|
|
|
if time_until_hour < sleep_time:
|
|
|
|
return time_until_hour
|
2019-03-24 21:35:05 +01:00
|
|
|
|
|
|
|
if sleep_time < timedelta(0):
|
|
|
|
sleep_time = timedelta(0, 0, 0, 0, min_sleep_minutes)
|
2019-03-13 20:33:20 +01:00
|
|
|
return sleep_time
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def get_last_duration (self):
|
2019-03-13 20:33:20 +01:00
|
|
|
if len(self.loop_history) == 0:
|
|
|
|
return
|
|
|
|
begin, end = self.loop_history[-1]
|
|
|
|
return end - begin
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def get_time_to_next_hour (self):
|
2019-03-13 20:33:20 +01:00
|
|
|
cur = datetime.now()
|
|
|
|
rounded = datetime(cur.year, cur.month, cur.day, cur.hour)
|
|
|
|
next_hour_time = rounded + timedelta(hours=1)
|
|
|
|
return next_hour_time - datetime.now()
|
|
|
|
|
2019-03-24 21:35:05 +01:00
|
|
|
def is_new_hour_loop (self):
|
2019-03-13 20:33:20 +01:00
|
|
|
if len(self.loop_history) < 2:
|
|
|
|
return False
|
|
|
|
previous_loop = self.loop_history[-2]
|
|
|
|
current_loop = self.get_current()
|
|
|
|
|
|
|
|
if previous_loop[0].hour != current_loop[0].hour:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|