-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateTime.py
More file actions
218 lines (189 loc) · 6.74 KB
/
dateTime.py
File metadata and controls
218 lines (189 loc) · 6.74 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
Month_name = ['', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December']
DayOfTheWeek = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
Month_num = {'January': 1, 'February': 2, 'March': 3, 'April': 4, 'May': 5, 'June': 6, 'July': 7, 'August': 8,
'September': 9, 'October': 10, 'November': 11, 'December': 12}
class Date:
__slots__ = ['__day', '__month', '__year', '__Month_table', '__pref_month']
def recalc(self):
if self.__year % 4 == 0 and self.__year % 100 != 0 or self.__year % 400 == 0:
self.__Month_table = [0] * 13
self.__Month_table[1] = 31
self.__Month_table[2] = 29
else:
self.__Month_table = [0] * 13
self.__Month_table[1] = 31
self.__Month_table[2] = 28
for i in range(3, 13):
if i < 8:
if i % 2 == 1:
self.__Month_table[i] = 31
else:
self.__Month_table[i] = 30
else:
if i % 2 == 0:
self.__Month_table[i] = 31
else:
self.__Month_table[i] = 30
self.__pref_month = [0] * 13
for i in range(1, 13):
self.__pref_month[i] = self.__pref_month[i - 1] + self.__Month_table[i]
def normalize(self):
if self.__day == 0:
self.__month -= 1
if self.__month == 0:
self.__year -= 1
self.recalc()
self.__month = 12
self.__day = self.__Month_table[self.__month]
elif self.__month == 0:
if self.__month == 0:
self.__year -= 1
self.recalc()
self.__month = 12
def __init__(self, d, m=0, y=0):
if type(d) == int and m == 0:
self.__year = (d // (400 * 365 + 97)) * 400 + 1
if d % (400 * 365 + 97) == 0:
self.__day = 31
self.__month = 12
self.__year -= 1
return
d -= (d // (400 * 365 + 97)) * (400 * 365 + 97)
self.__year += (d // (365 * 100 + 24)) * 100
if d // (100 * 365 + 24) == 4 and d % (365 * 100 + 24) == 0:
self.__day = 30
self.__month = 12
self.__year -= 1
return
d -= (d // (365 * 100 + 24)) * (365 * 100 + 24)
tup = 4 * 365 + 1
self.__year += (d // tup) * 4
if d == 0:
self.__day = 31
self.__month = 12
self.__year -= 1
return
d -= (d // tup) * tup
self.__year += d // 365
if d // 365 == 4 and d % 365 == 0:
self.__day = 30
self.__month = 12
self.__year -= 1
return
d -= (d // 365) * 365
self.recalc()
m = False
for i in range(1, 13):
if self.__pref_month[i] >= d and not m:
self.__month = i
m = True
d -= self.__pref_month[i - 1]
self.__day = d
elif type(d) == Date:
self.__day = d.__day
self.__month = d.__month
self.__year = d.__year
elif m == 0 and '.' in d:
self.__day, self.__month, self.__year = map(int, d.split('.'))
elif m == 0:
t = d.split()
self.__month = Month_num[t[0]]
t[1] = t[1][0:-1]
self.__day = int(t[1])
self.__year = int(t[2])
else:
self.__day = d
self.__month = m
self.__year = y
self.recalc()
self.normalize()
def get_day(self):
return self.__day
def get_month(self):
return self.__month
def get_year(self):
return self.__year
def month_length(self):
return self.__Month_table[self.__month]
def set_day(self, d):
if self.month_length() >= d > 0:
self.__day = d
def __str__(self):
return f"{self.__day:02}.{self.__month:02}.{self.__year:04}"
def text(self):
return f"{Month_name[self.get_month()]} {self.__day}, {self.__year}"
def __lt__(self, other):
if other.__year > self.__year:
return True
elif other.__year == self.__year and other.__month > self.__month:
return True
elif other.__year == self.__year and other.__month == self.__month and other.__day > self.__day:
return True
else:
return False
def __eq__(self, other):
return self.__day == other.__day and self.__year == other.__year and self.__month == other.__month
def __ne__(self, other):
return not self == other
def __ge__(self, other):
return not self < other
def __gt__(self, other):
return self >= other and not self == other
def __le__(self, other):
return self < other or self == other
def next(self):
t = Date(self)
t.__day += 1
if t.__day > t.month_length():
t.__day = 1
t.__month += 1
if t.__month > 12:
t.__year += 1
t.__month = 1
if t.__year % 4 in [0, 1]:
t.recalc()
return t
def prev(self):
t = Date(self)
t.__day -= 1
if t.__day < 1:
t.__month -= 1
if t.__month < 1:
t.__year -= 1
t.__month = 12
if t.__year % 4 in [0, 3]:
t.recalc()
t.__day = t.month_length()
return t
def subint(self, other):
other = Date(other)
t = (self.__year - 1) // 4 - (self.__year - 1) // 100 + (self.__year - 1) // 400
ds = (self.__year - 1) * 365 + t
t = (other.__year - 1) // 4 - (other.__year - 1) // 100 + (other.__year - 1) // 400
dot = (other.__year - 1) * 365 + t
self.recalc()
other.recalc()
sub = self.__pref_month[self.__month - 1] + self.__day
sub += -other.__pref_month[other.__month - 1] - other.__day
return ds - dot + sub
def __sub__(self, other):
if type(other) == Date:
return self.subint(other)
else:
return Date(self.subint(other))
def __int__(self):
return self.subint(Date("01.01.0001")) + 1
def day(self):
return self.subint(Date("01.01.0001")) % 7
def __add__(self, other):
return Date(int(self) + int(other))
a = [0] * 7
s = 0
for i in range(1, 2401):
for j in range(1, 13):
t = Date(13, j, i)
a[t.day()] += 1
s += 1
for i in a:
print(i / s)