Skip to content

Instantly share code, notes, and snippets.

@phizaz
Last active June 4, 2020 14:44
Show Gist options
  • Save phizaz/30c0a7974c7cfb2367a4928a7d29e4f4 to your computer and use it in GitHub Desktop.
Save phizaz/30c0a7974c7cfb2367a4928a7d29e4f4 to your computer and use it in GitHub Desktop.
Blocks until a given time (Python)
"""
blocks until a specific time,
useful for planning to run a script at a specific time
usage:
python til.py 13:00 tomorrow && python script.py
"""
import dateparser
from datetime import datetime, timedelta
import time
import argparse
parser = argparse.ArgumentParser(description='Block until run time')
parser.add_argument('time', type=str, nargs='+')
args = parser.parse_args()
run_time = ' '.join(args.time)
tgt = dateparser.parse(run_time)
print('will run at:', tgt)
while True:
now = dateparser.parse('now')
if tgt < now: break
print('wait for:', tgt - now)
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment