-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.py
181 lines (136 loc) · 5.26 KB
/
index.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
startYear = 18
dayToIndexRatio = 1 / (4 * 24)
indexToDayRatio = 4 * 24
# FIXME!!! THIS CODE IS SUUUUPER inefficient somehow. It makes the entire program almost unusable
def getDaysInYear(year):
if year % 4 == 0:
daysInYear = 366
else:
daysInYear = 365
return daysInYear
def dateToIndex(year, month, day, startYear):
# calculates the number of days since October 1, start year (2018)
# will not work properly for any dates prior to that
# year, month, day = date.split("-")
year = int(year)
month = int(month)
day = int(day)
index = -1
if year % 4 == 0: # if it is a leap year
monthToDays = {1: 31, 2: 29, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
else:
monthToDays = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
# print(index)
monthVal = int(month)
if startYear == year:
# print(index)
# then just calculate the day it is in the year and subract 304
for preMonth in range(10, monthVal): # add the previous months
index += monthToDays[preMonth]
index += day # add the days
return index
elif year > startYear:
# add the residual from the first year
for preMonth in range(10, 13):
index += monthToDays[preMonth]
startYear += 1
# now treat as if we were calculating days since Jan 1, (startYear + 1)
# add the years
numYears = year - startYear
currentYear = startYear
for yearSinceStart in range(numYears):
if currentYear % 4 == 0:
daysInYear = 366
else:
daysInYear = 365
currentYear += 1
index = index + daysInYear
# add the months
if year % 4 == 0: # if it is a leap year
monthToDays = {1: 31, 2: 29, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
else:
monthToDays = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
monthVal = int(month)
if monthVal > 1:
for month in range(1, monthVal): # don't include the current month because it isn't over yet!
index = index + monthToDays[month]
index = index + day
return index
def indexToDatetime(index, startYear):
# index must represent days since Oct 1, startYear
# put everything back into the framework of normal years (Jan 1, startYear)
if (startYear % 4) == 0: # if it is a leap year
monthToDays = {1: 31, 2: 29, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
else:
monthToDays = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
for month in range(1, 10):
index += monthToDays[month]
index += 1
currentIndex = 0
timeIndex = index - int(index)
index = int(index)
startYear = int(startYear)
year = startYear
while currentIndex < index:
daysInYear = getDaysInYear(year)
currentIndex += daysInYear
year += 1
year = year - 1
currentIndex = currentIndex - daysInYear
if getDaysInYear(year) == 366: # if it is a leap year
monthToDays = {1: 31, 2: 29, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
else:
monthToDays = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31}
month = 1
while currentIndex < index:
currentIndex += monthToDays[month]
month += 1
month = month - 1
currentIndex -= monthToDays[month]
day = 1
while currentIndex < index:
currentIndex += 1
day += 1
day = day - 1
hour = int(timeIndex * 24)
timeIndex = timeIndex - (hour / 24)
minute = int(timeIndex * 24 * 60)
timeIndex = timeIndex - ((minute / 24) / 60)
second = int(timeIndex * 24 * 60 * 60)
return year, month, day, hour, minute, second
def timeToIndex(hour, minute, second, index):
# add hours
index += int(hour) / 24
# add minutes
index += int(minute) / (24 * 60)
# add seconds
index += float(second) / (24 * 60 * 60)
return index
def datetimeToIndex(year, month, day, hour, minute, second):
index1 = dateToIndex(year, month, day, startYear)
index = timeToIndex(hour, minute, second, index1)
return index
startIndex = datetimeToIndex(str(startYear), "10", "01", "00", "00", "00")
startIndex = round(startIndex / dayToIndexRatio) * dayToIndexRatio
def getDaysInYear(year):
#sees if it's a leap year or not
if year % 4 == 0:
daysInYear = 366
else:
daysInYear = 365
return daysInYear
oldIndices = []
newIndices = []
numOff = 0
for i in range(0,600):
index = i
for j in range(int(1 / dayToIndexRatio)):
year, month, day, hour, minute, second = indexToDatetime(index, startYear)
date = str(year) + "-" + str(month) + "-" + str(day) + " " + str(hour) + ":" + str(minute) + ":" + str(second)
if month == "13":
print(month)
newIndex = datetimeToIndex(year, month, day, hour, minute, second)
newIndices.append(newIndex)
oldIndices.append(index)
if newIndex - index != 0:
numOff += 1