-
Notifications
You must be signed in to change notification settings - Fork 0
/
timebetween.py
executable file
·149 lines (124 loc) · 4.5 KB
/
timebetween.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/python
import sys
import datetime
ussage = "ussage: ./timebetween date1 date2 \n(date format: dd-mm-yyyy | today) " \
"\n examples:\n./timebetween 05-07-2010 09-04-2015\n./timebetween 05-07-2010 today"
def DatesToSubstract(date1, date2):
'''
DatesToSubstract returns a json with the result of substracting one date from another
'''
if date1 == 'today':
datetype1 = datetime.datetime.now().date()
else:
datetype1 = datetime.datetime.strptime(date1, '%d-%m-%Y').date()
if date2 == 'today':
datetype2 = datetime.datetime.now().date()
else:
datetype2 = datetime.datetime.strptime(date2, '%d-%m-%Y').date()
if datetype1 < datetype2:
tmp = datetype1
datetype1 = datetype2
datetype2 = tmp
numbisiestos = bisiestos(datetype2.year, datetype1.year)
dif = (datetype1 - datetype2)
years = (dif.days - numbisiestos) / 365
if datetype1.month > datetype2.month:
if datetype1.day >= datetype2.day:
months = datetype1.month - datetype2.month
days = datetype1.day - datetype2.day
elif datetype1.day < datetype2.day:
months = datetype1.month - datetype2.month - 1
days = MonthNumDays(datetype2.month, datetype2.year) - datetype2.day + datetype1.day
if datetype1.month == datetype2.month:
if datetype1.day >= datetype2.day:
months = datetype1.month - datetype2.month
days = datetype1.day - datetype2.day
elif datetype1.day < datetype2.day:
months = 12 * (datetype1.year - datetype2.year) -1
days = MonthNumDays(datetype2.month, datetype2.year) - datetype2.day + datetype1.day
elif datetype1.month < datetype2.month:
if datetype1.day >= datetype2.day:
months = 12 + datetype1.month - datetype2.month
days = datetype1.day - datetype2.day
elif datetype1.day < datetype2.day:
months = 12 + datetype1.month - datetype2.month - 1
days = MonthNumDays(datetype2.month, datetype2.year) - datetype2.day + datetype1.day
result = {}
result['years'] = years
result['months'] = months
result['days'] = days
result['from_day'] = datetype2.day
result['from_month'] = datetype2.month
result['from_year'] = datetype2.year
result['to_day'] = datetype1.day
result['to_month'] = datetype1.month
result['to_year'] = datetype1.year
return result
def bisiestos(start, end):
'''
Returns the number of years with 366 days between the start year and the end year.
:param start:
:param end:
:return:
'''
years = range(start, end, 1)
counter = 0
for year in years:
if bisiesto(year):
counter += 1
return counter
def bisiesto(year):
'''
Returns True if the year had 366 days and false if it had 365
:param year: (yyyy)
:return:
'''
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
return True
else:
return False
def MonthNumDays(month, year):
'''
Returns the number of days in a given month and year.
example: feb2thousand = MonthNumDays(2, 2000)
:param month: (1-12)
:param year: (yyyy)
:return:
'''
if month == 2:
if bisiesto(year):
return 29
else:
return 28
elif month <= 7:
if month == 0:
return 31
elif month % 2 == 0:
return 30
else:
return 31
elif month > 7 and month <= 12:
if month % 2 == 0:
return 31
else:
return 30
else:
return None
def main():
'''
timebetween.py calculates the time elapsed between two dates. see ussage.
:return:
'''
if len(sys.argv) < 3:
print ussage
elif len(sys.argv) == 3:
timeElapsed = DatesToSubstract(sys.argv[1], sys.argv[2])
print "Time elapsed from: {0}-{1}-{2} to: {3}-{4}-{5}".format(timeElapsed['from_day'],
timeElapsed['from_month'], timeElapsed['from_year'],
timeElapsed['to_day'], timeElapsed['to_month'],
timeElapsed['to_year'])
print "{0} years, {1} months, {2} days.".format(timeElapsed['years'], timeElapsed['months'], timeElapsed['days'])
else:
print "Options incorrect: ", ussage, sys.argv
if __name__ == '__main__':
main()