-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
119 lines (100 loc) · 3.57 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import math
def main():
# TODO: check for valid inputs
date_1 = input('Enter the first date (MM/DD/YY): \n')
date_1 = get_valid_date(date_1)
date_2 = input('Enter the second date (MM/DD/YY): \n')
date_2 = get_valid_date(date_2)
print('Choose an output option:')
option = input('1 = Years + Months + Weeks + Days, 2 = Months + Weeks + Days, 3 = Weeks + Days, 4 = Days\n')
valid_options = {'1', '2', '3', '4'}
while option not in valid_options:
option = input('Please enter a valid output option. 1 = Years + Months + Weeks + Days, 2 = Months + Weeks + Days, 3 = Weeks + Days, 4 = Days\n')
option = int(option) # convert string to int
time_diff = convert_time(option, date_1, date_2)
formatted_time_diff = format_time(time_diff, option)
print(*formatted_time_diff, sep=', ')
def get_valid_date(date):
while True:
date = date.split('/')
if len(date) != 3 or try_int(date):
date = input('Please enter a valid date (MM/DD/YY): \n')
else:
return [int(x) for x in date] # convert string to int array
def try_int(arr):
for i in range(len(arr)):
try:
arr[i] = int(arr[i])
if arr[i] < 0: # negative numbers
raise ValueError
except ValueError:
return True
return False
def check_leap_year(year):
if year < 0:
return False
if year % 4 == 0: # first check
if (year % 100 == 0) and (year % 400 != 0): # second check
return False
return True
return False
def find_day_in_year(date):
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
day = 0
for month in range(date[0] - 1):
if month == 3 and check_leap_year(date[2]): # april case
day += days[month] + 1
else:
day += days[month]
day += date[1]
return day
def find_diff_between_months(month_1, day_1, day_2):
# fix this
days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if day_1 > day_2:
return (days[month_1 - 1] - day_1) + day_2
else:
return day_2 - day_1
def convert_time(option, date_1, date_2):
day_1 = find_day_in_year(date_1)
day_2 = find_day_in_year(date_2)
if date_1[2] > date_2[2] or day_1 > day_2: # find larger of the two dates
temp_date = date_1
date_1 = date_2
date_2 = temp_date
temp_day = day_1
day_1 = day_2
day_2 = temp_day
days_diff = day_2 - day_1
years_diff = date_2[2] - date_1[2]
if date_2[0] - date_1[0] < 0:
years_diff -= 1
months_diff = date_2[0] - date_1[0]
if date_2[1] - date_1[1] < 0:
months_diff -= 1
time_diff = [0] * 4
if option == 1:
time_diff[0] = years_diff
if option <= 2:
time_diff[1] = months_diff
days_diff = find_diff_between_months(date_1[0], date_1[1], date_2[1])
if option <= 3:
time_diff[2] = math.floor(days_diff / 7)
days_diff = days_diff % 7
time_diff[3] = days_diff
return time_diff
def format_time(time_diff, option):
formatted_time = []
if option == 1:
formatted_time.append(str(time_diff[0]) + ' Years')
if option <= 2:
formatted_time.append(str(time_diff[1]) + ' Months')
if option <= 3:
formatted_time.append(str(time_diff[2]) + ' Weeks')
formatted_time.append(str(time_diff[3]) + ' Days')
for i in range(len(time_diff)):
if time_diff[i] == 1: # remove plural from string
formatted_time[i] = formatted_time[i][:-1]
return formatted_time
if __name__ == "__main__":
main()