-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Clock Functions.py
More file actions
135 lines (109 loc) · 4.23 KB
/
Python Clock Functions.py
File metadata and controls
135 lines (109 loc) · 4.23 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
'Python Clock Functions:'
'''
Python clock functions allow you to program the actual time in real time. \
Python clock functions work internally, in sync with the Windows clock. \
With Python clock functions; you can set the hour, minute, second, month, \
week, day and date. See Python clock function prefix descriptions below.
'%I' 12-hour prefix
'%H' 24-hour prefix
'%M' Minutes prefix
'%S' Seconds prefix
'%p' AM/PM prefix
'%A' Day of week prefix
'%B' Month prefix
'%d' Date prefix
'%Y' Year prefix
'%U' Weeks per year prefix
'%j' Days per year prefix
'''
'''----------------------------------------------------------------'''
# Let's create a simple Python clock by invoking the Python clock function prefixes.
# First, however, we also need to import two modules; 'time' and 'datetime'. Type and
# execute/run the program example below and see what happens.
import time
import datetime
print(datetime.datetime.now().strftime('%I:%M:%S:%p'))
print(datetime.datetime.now().strftime('%H:%M:%S'))
print(datetime.datetime.now().strftime('%A %B %d,%Y'))
print(datetime.datetime.now().strftime('Week %U'))
print(datetime.datetime.now().strftime('Day %j'))
# Remember you can reduce balky code via, using string variables. Let's use 'timer' as
# the variable and use 'datetime.datetime.now()' as the value. Type and execute/run
# the program example below and see what happens.
import time
import datetime
timer=datetime.datetime.now()
print(timer.strftime('%I:%M:%S:%p'))
print(timer.strftime('%H:%M:%S'))
print(timer.strftime('%A %B %d,%Y'))
print(timer.strftime('Week %U'))
print(timer.strftime('Day %j'))
'''----------------------------------------------------------------'''
# Now, let's create a tuple variable called 'show_time' so we can reduce even more
# balky code, and also gain greater manipulative programming skills at the same time.
# Type and execute/run the program example below and see what happens.
import time
import datetime
show_time=(
'%I:%M:%S:%p',
'%H:%M:%S',
'%A %B %d,%Y',
'Week %U',
'Day %j'
)
timer=datetime.datetime.now()
print(timer.strftime(show_time[0]))
print(timer.strftime(show_time[1]))
print(timer.strftime(show_time[2]))
print(timer.strftime(show_time[3]))
print(timer.strftime(show_time[4]))
'''----------------------------------------------------------------'''
# Now change and rearrange the tuple number values [0] through [4] in the program
# example above and re-execute/run the program and see what happens.
# Now, let's make our Python clock come to life. Let's also keep the code less balky
# and much more program manipulative at the same time. To make the Python clock
# come to life, we are simply going to use a while-loop to constantly refresh the screen
# output. A 'time.sleep()' function will also be implemented to create a one-second
# sleep delay in the screen output. Let's also implement the 'os.system()' function to
# clear the screen output right after every one-second 'time.sleep' delay. Type and
# execute/run the program example below and see what happens.
import os
import time
import datetime
show_time=(
'%I:%M:%S:%p',
'%H:%M:%S',
'%A %B %d,%Y',
'Week %U',
'Day %j'
)
while True:
timer=datetime.datetime.now()
print(timer.strftime(show_time[0]))
print(timer.strftime(show_time[1]))
print(timer.strftime(show_time[2]))
print(timer.strftime(show_time[3]))
print(timer.strftime(show_time[4]))
time.sleep(1)
os.system('cls')
'''----------------------------------------------------------------'''
# Now, let's use a while-loop and a for-loop to completely reduce our Python clock\
# program; remember to keep it (DRY): 'Don't Repeat Yourself' as shown in the examples \
# above with five repeated 'print' statements. If you notice the example below shows
# only one, single 'print' statement, not five repeated ones.
import os
import time
import datetime
show_time=(
'%I:%M:%S:%p',
'%H:%M:%S',
'%A %B %d,%Y',
'Week %U',
'Day %j'
)
while True:
for i in show_time:
timer=datetime.datetime.now()
print(timer.strftime(i))
time.sleep(1)
os.system('cls')