|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from datetime import * |
| 4 | +from dateutil.relativedelta import * |
| 5 | +from dateutil.tz import * |
| 6 | +import calendar |
| 7 | + |
| 8 | +# experimenting again with dateutil.relativedelta(): |
| 9 | +# https://dateutil.readthedocs.io/en/stable/index.html |
| 10 | + |
| 11 | +# This function started with code from: |
| 12 | +# https://levlaz.org/pretty-print-relative-dates-in-python/ |
| 13 | +# I later received a question about "how old was <indiviual-in-question> |
| 14 | +# on a specific date?" and added the ability to specify a given date. |
| 15 | +# There is nothing special about "birthdate," it can be any starting date. |
| 16 | +def yourageattarget(birthyear: int, birthmonth: int, birthday: int, targetyear: int, targetmonth: int, targetday: int): |
| 17 | + """ |
| 18 | + Description: Calculate the difference between input |
| 19 | + birthdate and a given date |
| 20 | + Input: int birthyear, int birthmonth, int birthday, int targetyear, int targetmonth, int targetday. |
| 21 | + Output: a string with diff in years months days. |
| 22 | + """ |
| 23 | + try: |
| 24 | + rd = relativedelta(datetime(int(targetyear), int(targetmonth), int(targetday)), datetime(int(birthyear), int(birthmonth), int(birthday)) ) |
| 25 | + years = f'{rd.years} years, ' if rd.years > 0 else '' |
| 26 | + months = f'{rd.months} months, ' if rd.months > 0 else '' |
| 27 | + days = f'{rd.days} days' if rd.days > 0 else '' |
| 28 | + except Exception as e: |
| 29 | + print(f"Exception doing relativedelta(): {e}") |
| 30 | + sys.exit() |
| 31 | + if f'{years}{months}{days}' == '': |
| 32 | + print("Your input for birthdate cannot be today or in the future.") |
| 33 | + print(f"Try again with <birthmonth> <birthday> <birthyear> <targetmonth> <targetday> <targetyear> on the command line.") |
| 34 | + sys.exit() |
| 35 | + else: |
| 36 | + return f'{years}{months}{days}' |
| 37 | + |
| 38 | + |
| 39 | +def check_input(inputs: list) -> tuple: |
| 40 | + if len(inputs) != 7: |
| 41 | + print(f"Not enough arguments.\n Need <birthmonth> <birthday> <birthyear> <targetmonth> <targetday> <targetyear> on the command line.") |
| 42 | + sys.exit(0) |
| 43 | + |
| 44 | + if all(elements.isnumeric() for elements in (inputs[1:])) != True: |
| 45 | + print(f"Some input was not numeric.\n Need <birthmonth> <birthday> <birthyear> <targetmonth> <targetday> <targetyear> on the command line.") |
| 46 | + sys.exit(0) |
| 47 | + |
| 48 | + if (int(inputs[1]) <= 12 and int(inputs[4]) <= 12): |
| 49 | + # Do nothing. Month is valid. |
| 50 | + birthmonth = int(inputs[1]) |
| 51 | + targetmonth = int(inputs[4]) |
| 52 | + else: |
| 53 | + print("Invalid month. Your input for birthmonth was not between 1 and 12.") |
| 54 | + print(f"Need <birthmonth> <birthday> <birthyear> <targetmonth> <targetday> <targetyear> on the command line.") |
| 55 | + sys.exit(0) |
| 56 | + |
| 57 | + if (int(inputs[2]) <= 31 and int(inputs[5]) <= 31): |
| 58 | + # Do nothing. Gross day validation is OK. |
| 59 | + birthday = int(inputs[2]) |
| 60 | + targetday = int(inputs[5]) |
| 61 | + else: |
| 62 | + print("Invalid day. Your input for birthday was not between 1 and 31.") |
| 63 | + print(f"Need <birthmonth> <birthday> <birthyear> <targetmonth> <targetday> <targetyear> on the command line.") |
| 64 | + sys.exit(0) |
| 65 | + |
| 66 | + if (len(inputs[3]) == 4 and len(inputs[6]) == 4): |
| 67 | + birthyear = inputs[3] |
| 68 | + targetyear = inputs[6] |
| 69 | + else: |
| 70 | + print("Invalid year length. \nYour input for birthyear was not 4 digits long.") |
| 71 | + print(f"Need <birthmonth> <birthday> <birthyear> <targetmonth> <targetday> <targetyear> on the command line.") |
| 72 | + sys.exit(0) |
| 73 | + |
| 74 | + return birthyear, birthmonth, birthday, targetyear, targetmonth, targetday |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + inputs: list = sys.argv |
| 79 | + # Validate the input |
| 80 | + birthyear, birthmonth, birthday, targetyear, targetmonth, targetday = check_input(inputs) |
| 81 | + |
| 82 | + # birthhour = sys.argv[4] |
| 83 | + print(f" ---------------------------") |
| 84 | + print(f"birthmonth = {birthmonth}, birthday = {birthday}, birthyear = {birthyear}\r\ntargetmonth = {targetmonth}, targetday = {targetday}, targetyear = {targetyear}") |
| 85 | + print(f" ---------------------------") |
| 86 | + print(f"Given that input -> {yourageattarget(int(birthyear), int(birthmonth), int(birthday), int(targetyear), int(targetmonth), int(targetday))} old.") |
| 87 | + print(f" ---------------------------") |
| 88 | + |
0 commit comments