Queer European MD passionate about IT

seasons.py 941 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import datetime
  2. import re
  3. import sys
  4. import inflect
  5. date_regex = re.compile(r"\d{4}-\d{2}-\d{2}")
  6. p = inflect.engine()
  7. def main():
  8. today = datetime.date.today()
  9. users_date = input("Date of Birth: ")
  10. try:
  11. users_date = parse_date(users_date)
  12. except ValueError:
  13. sys.exit(1)
  14. minutes = get_minutes(users_date, today)
  15. print(format_minutes(minutes))
  16. def get_minutes(start_date, end_date):
  17. minutes = (end_date - start_date).total_seconds() / 60
  18. return int(round(minutes, 0))
  19. def format_minutes(minutes: int) -> str:
  20. stringed_number = p.number_to_words(minutes, andword="").capitalize()
  21. return f"{stringed_number} minute{'s' if minutes > 1 else ''}"
  22. def parse_date(date_string):
  23. if not date_regex.match(date_string): # Acutally not necessary: strptime would raise ValueError anyway
  24. raise ValueError
  25. return datetime.datetime.strptime(date_string, '%Y-%m-%d').date()
  26. if __name__ == '__main__':
  27. main()