Skip to content

Instantly share code, notes, and snippets.

@packmad
Created December 4, 2020 22:54
Show Gist options
  • Save packmad/b5aad27636c15a800563a653670cd500 to your computer and use it in GitHub Desktop.
Save packmad/b5aad27636c15a800563a653670cd500 to your computer and use it in GitHub Desktop.
Pretty prints how much time is left to a deadline
#!/usr/bin/python3
import sys
from datetime import datetime, timedelta, timezone
def get_remaining_time(total_seconds: int) -> str:
months, remainder = divmod(total_seconds, 2592000)
days, remainder = divmod(remainder, 86400)
hours, remainder = divmod(remainder, 3600)
minutes, seconds = divmod(remainder, 60)
ret = ''
if months > 0.0:
ret += f'{int(months)} months, '
if days > 0.0:
ret += f'{int(days)} days, '
if hours > 0.0:
ret += f'{int(hours)} hours, '
if minutes > 0.0:
ret += f'{int(minutes)} minutes, '
ret += f'{int(seconds)} seconds.'
return ret
if __name__ == "__main__":
if len(sys.argv) != 2:
sys.exit('Missing date in ISO 8601. E.g., "2015-10-21T23:59-1200"')
deadline = datetime.strptime(sys.argv[1], '%Y-%m-%dT%H:%M%z')
utc_now = datetime.now(timezone.utc)
now = utc_now.astimezone()
if deadline < now:
print('OUT OF TIME!!!')
else:
remaining_time = deadline - now
print(get_remaining_time(remaining_time.total_seconds()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment