Last active
December 19, 2015 22:39
-
-
Save famanson/6028960 to your computer and use it in GitHub Desktop.
Basic countdown app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from optparse import OptionParser | |
import datetime | |
def countdown(deadline, event): | |
today = datetime.datetime.today() | |
remaining = deadline - today | |
days = remaining.days | |
if days < 2: | |
print '%s day left until %s!' % (str(days), event) | |
else: | |
print '%s days left until %s!' % (str(days), event) | |
def main(): | |
parser = OptionParser() | |
parser.add_option("-d", "--deadline", dest="deadline", | |
help="Specify the deadline in ISO format: yyyy-mm-dd") | |
parser.add_option("-e", "--event", dest="event", | |
help="Name of the deadline event") | |
(options, args) = parser.parse_args() | |
if not options.deadline: | |
# Calculate default deadline, set it to nearest Xmas | |
today = datetime.datetime.today() | |
if today.month > 12 or (today.month == 12 and today.day > 25): | |
deadline = datetime.datetime(today.year+1, 12, 25) | |
else: | |
deadline = datetime.datetime(today.year, 12, 25) | |
event = 'Christmas' | |
else: | |
# Parse it! | |
deadline = datetime.datetime.strptime(options.deadline, "%Y-%m-%d") | |
if not options.event: | |
event = options.deadline | |
else: | |
event = options.event | |
countdown(deadline, event) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment