python - Custom Timer to get the seconds to the next start time/date -
i have project have assigned days of week running, while other days not included should skipped. eg:
days on: mon (0), tues (1), wed (2), thurs (3), fri (4) days off: sat (5), sun (6)
i have settings saved within registry days on, when settings pulled referenced weekday number.
if want put app sleep on fri (4), , have wake on mon (0), how can go doing so? have no way state sure whether days off consistent, might have sun/mon off instead of sat/sun.
ideally need @ current weekday number, see if within list of days on , if not, have calculate number of seconds until next day on begins.
scenarios:
i set script go sleep on friday june 8th, , sat/sun off days, how calculate seconds sleep 2 days starts again on mon 11th?
i set script go sleep on wed june 6th, , need wake on fri june 8th, turn off on friday night have start again on monday morning.
because days on variable, function must able accommodate it; running problems.
any appreciated, big in advance!
check once day - if today not on day, go sleep until tomorrow.
or
use cron (or windows task scheduler) schedule program needed days.
or
import datetime time import sleep seconds_per_day = 3600 * 24 daily_start_time = datetime.time(8,15) # 8:15 def seconds(t): return 3600*t.hour + 60*t.minute + t.second def now(): t = datetime.datetime.now() return t.weekday(), seconds(t) def load_run_days(): # data registry return [1,1,1,1,1,0,0] def days_until_next_run(today, run_days=none): if run_days none: run_days = load_run_days() # rotate run_days = run_days[today+1:] + run_days[:today+1] # find next on day days,_on in enumerate(run_days, 1): if _on: return days # no run day found? raise valueerror("no 'on' days found") def sleep_until_next_run(): today, elapsed = now() days_to_wait = days_until_next_run(today) sleep(-elapsed + days_to_wait*seconds_per_day + seconds(daily_start_time)) def main(): while true: try: sleep_until_next_run() except valueerror: break do_my_stuff() if __name__=="__main__": main()
Comments
Post a Comment