-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmas_countdown_testing.py
242 lines (186 loc) · 6.95 KB
/
xmas_countdown_testing.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# import things
import calendar
import datetime
from time import sleep
global date
# simulate date and time for testing
date = datetime.datetime(2020, 1, 1, 0, 0, 56)
# obsolete code
"""
def formatCountdown():
#date = datetime.datetime.now()
#date = datetime.datetime(2023, 1, 27, 0, 59, 59)
# Here we can set the date and time to count down to. This is nice and modular if i ever wanna reuse this code.
monthToCountDownTo = 12
dayToCountDownTo = 24
hourToCountDownTo = 23
minToCountDownTo = 59
secToCountDownTo = 59
# Logic.
months = str(monthToCountDownTo - int(date.strftime("%m")))
days = str(dayToCountDownTo - int(date.strftime("%d")))
hours = str(hourToCountDownTo - int(date.strftime("%H")))
mins = str(minToCountDownTo - int(date.strftime("%M")))
secs = str(secToCountDownTo - int(date.strftime("%S")))
# TODO fix this jank-fest of code, ideally before Christmas
if date.month == monthToCountDownTo and date.day > dayToCountDownTo : # and date.hour > hourToCountDownTo:
formatted_countdown = "Wait till next year!"
elif int(months) <= 0 and int(days) <= 0 and int(hours) <= 0 and int(mins) <= 0 and int(secs) <= 0:
formatted_countdown = "It is Christmas day!"
else:
formatted_countdown = "It is " + months + " months, " + days + " days, " + hours + " hours, " + mins + " mins and " + secs + " seconds" " till Christmas."
formatted_countdown = "It is " + months + " months, " + days + " days, " + hours + " hours, " + mins + " mins and " + secs + " seconds" + " till Christmas"
# print("[SUBTASK] " + str(datetime.datetime.now().strftime("%X")) + ": " +
# "Formatting date as " + str(formatted_countdown))
# spit it back out.
return formatted_countdown
"""
"""
def CountdownRewrite():
Countdown = ""
dayOfYear = 0
months = 0
days = 0
todaysdate = datetime.datetime(2020, 12, 27)
xmasdate = datetime.datetime(todaysdate.year, 12, 25)
fulldate = xmasdate - todaysdate
print(xmasdate)
print(todaysdate)
print(fulldate.seconds)
#Countdown = str(59 - int(datetime.datetime.now().strftime('%S')))
months = xmasdate.month - todaysdate.month
days = xmasdate.day - todaysdate.day
hrs = xmasdate.hour - todaysdate.hour
mins = xmasdate.minute - todaysdate.minute
secs = xmasdate.second - todaysdate.second
print(fulldate)
return "It is " + str(days) + " days, " + str(hrs) + " hours, " + str(mins) + " mins and " + str(secs) + " seconds" + " till Christmas."
#return Countdown
"""
def getDays():
# variables
dayThatXmasIs = 0
dayOfYear = 0
# if leap year
if calendar.isleap(date.year):
# these are the amount of days each month have
months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# if NOT leap year
else:
# these are the amount of days each month have
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# loop through the array and add them up
for month in months:
# add the days to variable
dayThatXmasIs += month
# take away 6, as 31th Dec - 25th Dec = 6 days. Now we know what day of the year xmas is. Reckon this is a bit over-engineered? Perhaps.
dayThatXmasIs -= 6
# add up all the days from the months that have passed
for i in range(date.month - 1):
dayOfYear += months[i]
# then add the current day of the month
dayOfYear += date.day
# return
return dayThatXmasIs - dayOfYear
def getHrsMinsSecs():
# get hours mins secs to midnight TODAY
hours = str(23 - int(date.strftime('%H')))
mins = str(59 - int(date.strftime('%M')))
secs = str(59 - int(date.strftime('%S')))
# return
return hours, mins, secs
# validate
def validateCountdown():
# if days is negative we missed it.
if getDays() < 0:
validate = 0 # Missed christmas
# if days is 0 it is xmas
elif getDays() == 0:
validate = 1 # It is christmas
# otherwise we are still waiting on xmas
else:
validate = 2 # It is not christmas yet
return validate
# format nicely
def formatCountdown():
# variables
formatted_countdown = ""
# check with function
outcome = validateCountdown()
# if only python had switch statements :(
if outcome == 0:
formatted_countdown = "Wait till next year!"
elif outcome == 1:
formatted_countdown = "Merry Christmas!"
elif outcome == 2:
# format
formatted_countdown = "It is " + str(getDays()-1) + " days, " + str(getHrsMinsSecs()[0]) + " hours, " + str(getHrsMinsSecs()[1]) + " minutes and " + str(getHrsMinsSecs()[2]) + " seconds till Christmas."
return formatted_countdown
def calculatePercentage():
# date = datetime.datetime.now()
#date = datetime.datetime(2019, 12, 24, 23, 0, 0)
dayOfYear = 0
# if leap year
if calendar.isleap(date.year):
# these are the amount of days each month have
months = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
secThatXmasIs = 31104000
# if NOT leap year
else:
# these are the amount of days each month have
months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
secThatXmasIs = 31017600
# add up all the days from the months that have passed
for i in range(date.month - 1):
dayOfYear += months[i]
# then add the current day of the month
dayOfYear += date.day
secOfYear = (dayOfYear * 24 * 60 * 60) + (date.hour * 60 * 60) + (date.minute * 60) + date.second
percentageToXmas = secOfYear / secThatXmasIs
return percentageToXmas
def validatePercentage():
outcome = calculatePercentage()
if outcome > 100.00:
validate = True
elif outcome <= 100.00:
validate = False
# boolean to say if we missed chistmas or not
return validate
def formatPercentage():
formattedPercentage = ""
outcome = validatePercentage()
if outcome:
formattedPercentage = "Wait till next year!"
else:
formattedPercentage = "It is " + str(round(calculatePercentage() * 100, 5)) + "% of the way to Christmas."
return formattedPercentage
# loop for testing
def loop():
while True:
sleep(1)
print("[INFO] " + str(datetime.datetime.now().strftime("%x %X")) + ": "
+ formatCountdown())
# for testing
def main():
xyz = input(">>>")
if xyz == "*countdown" or xyz == "*cd":
print("[INFO] " + str(datetime.datetime.now().strftime("%x %X")) + ": "
+ str(formatCountdown()))
elif xyz == "*percent" or xyz == "*pc":
print("[INFO] " + str(datetime.datetime.now().strftime("%x %X")) + ": "
+ str(formatPercentage()))
elif xyz == "go":
loop()
# yeajh
def readKey():
f = open("assets/.gitignore xmas_countdown.txt", "r")
key = f.read()
f.close()
return key
#percent()
#print(readKey())
#print(CountdownRewrite())
#print(formatCountdown())
#print(calculatePercentage())
#print(formatPercentage())
main()