Skip to content

Instantly share code, notes, and snippets.

@pims
Forked from simonw/countdown_to_newyear.py
Created January 1, 2010 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pims/267151 to your computer and use it in GitHub Desktop.
Save pims/267151 to your computer and use it in GitHub Desktop.
# Depends on the OS X "say" command
import time, datetime, subprocess, math, sys
def say(s):
subprocess.call(['say', str(s)])
def seconds_until(dt):
return time.mktime(dt.timetuple()) - time.time()
def countdown_to(target_time, only_if_below=10, end_statement=None):
said = set()
while True:
i = int(math.ceil(seconds_until(target_time)))
if i < 0:
if end_statement:
say(end_statement)
break
if i <= only_if_below and i not in said:
said.add(i)
say(i)
sys.stdout.write('%s. ' % i)
sys.stdout.flush()
time.sleep(0.1)
if __name__ == '__main__':
countdown_to(
datetime.datetime(2010, 1, 1, 0, 0, 0),
10,
'Happy new year!',
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment