-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcountDaysInMonthInPython.py
More file actions
137 lines (93 loc) · 4.18 KB
/
countDaysInMonthInPython.py
File metadata and controls
137 lines (93 loc) · 4.18 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
#!/bin/python
# count days in month
# you can run this script with: python3 countDaysInMonthInPython.py < year > < month >
import calendar, colorama, os, sys, time, traceback
from colorama import Fore, Style
from datetime import datetime
colorama.init()
def checkOS():
print("Started checking operating system at", datetime.now().strftime("%m-%d-%Y %I:%M %p"))
if sys.platform == "win32":
print(Fore.GREEN + "Operating System:", end=""); sys.stdout.flush()
os.system('ver')
print(Style.RESET_ALL, end="")
operatingSystem = "Windows"
elif sys.platform == "darwin":
print(Fore.GREEN + "Operating System: ")
os.system('sw_vers')
print(Style.RESET_ALL, end="")
operatingSystem = "macOS"
elif sys.platform == "linux":
print(Fore.GREEN + "Operating System: ")
os.system('uname -r')
print(Style.RESET_ALL, end="")
operatingSystem = "Linux"
print("Finished checking operating system at", datetime.now().strftime("%m-%d-%Y %I:%M %p"))
print("")
return operatingSystem
def getYear(operatingSystem):
if operatingSystem == "Windows":
year = int(input("Please type the year and press the \"Enter\" key (Example: 2020): "))
else:
year = int(input("Please type the year and press the \"return\" key (Example: 2020): "))
print("")
return year
def getMonth(operatingSystem):
if operatingSystem == "Windows":
month = int(input("Please type the number of the month and press the \"Enter\" key (Example for March: 3): "))
else:
month = int(input("Please type the number of the month and press the \"Enter\" key (Example for March: 3): "))
print("")
return month
def checkParameters(year, month):
print("Started checking parameter(s) at", datetime.now().strftime("%m-%d-%Y %I:%M %p"))
valid = True
print("Parameter(s):")
print("------------------------")
print("year : {0}".format(year))
print("month: {0}".format(month))
print("------------------------")
if year == None or year == "":
print(Fore.RED + "year is not set." + Style.RESET_ALL)
valid = False
if month == None or month == "":
print(Fore.RED + "month is not set." + Style.RESET_ALL)
valid = False
if valid == True:
print(Fore.GREEN + "All parameter check(s) passed." + Style.RESET_ALL)
print("Finished checking parameter(s) at", datetime.now().strftime("%m-%d-%Y %I:%M %p"))
print("")
else:
raise Exception("One ore more parameters are incorrect.")
def countDaysOfMonth():
print("\nLet's count the days in a month in Python!\n")
try:
operatingSystem = checkOS()
if len(sys.argv) >= 2:
year = int(sys.argv[1])
month = int(sys.argv[2])
else:
year = getYear(operatingSystem)
month = getMonth(operatingSystem)
checkParameters(year, month)
startDateTime = datetime.now()
print("Started counting days in a month at", startDateTime.strftime("%m-%d-%Y %I:%M %p"))
cal = calendar.TextCalendar(calendar.SUNDAY)
print(Fore.BLUE + "Counting the number of days in {0} {1} now: ".format(calendar.month_name[month], year))
time.sleep(1)
for days in cal.itermonthdays(year, month):
if days != 0:
time.sleep(0.25)
print(days)
print("days in {0} {1}.".format(calendar.month_name[month], year))
print(Fore.GREEN + "Successfully counted days in a month," + Style.RESET_ALL)
finishedDateTime = datetime.now()
print("Finished counting day in a month at", finishedDateTime.strftime("%m-%d-%Y %I:%M %p"))
duration = finishedDateTime - startDateTime
print("Total execution time: {0} second(s)".format(duration.seconds))
print("")
except Exception:
print(Fore.RED + "Failed to count days in a month.")
traceback.print_exc()
exit("" + Style.RESET_ALL)
countDaysOfMonth()