Today we will take a look at a use of the time library. When it comes to the time library, everyone should be familiar with it. As a built-in library of python, there is no need to install it. You can just import it and use it directly.
1. Introduction to commonly used functions of time library
1.time.time(): Returns the timestamp of the current time
import time
time.time()
####Out: 1693465071.7247112
2.time.sleep(S): The thread delays running for the specified time. The unit is seconds.
import time
time.sleep(5)
###推迟5秒后继续运行
3.time.strftime(format[, t]): Receives a time tuple and returns the local time expressed as a readable string. The format is determined by the parameter format.
import time
print(time.strftime("%Y-%m-%d %H:%M:%S"))
####2023-08-31 15:06:45
After understanding the use of several basic functions, let's learn more about the time library through an example.
2. Use the time library to create a digital clock
1. Let’s look at it step by step, first create our clock class:
class Clock(object):
def __init__(self, hour=0, minute=0, second=0):
self._hour = hour
self._minute = minute
self._second = second
First we created our clock class and defined our initialization method.
The functions of several parameters here are as follows:
hour: how many hours it represents
minute: how many minutes it represents
second: how many seconds it represents
2. Edit the clock running algorithm
def run(self):
self._second += 1
if self._second == 60:
self._second = 0
self._minute += 1
if self._minute == 60:
self._minute = 0
self._hour += 1
if self._hour == 24:
self._hour = 0
Here is a function that our clock runs. It defines _second += 1, that is, 1 second increases upwards every second. When self._second == 60, it returns to 0S, _minute += 1, _minute = = 60, then continue to increase upwards, _hour += 1. Maybe my expression is not very good, but it is actually easy to understand. It is the normal operating mechanism of our clock. Continue to look down.
3. Display time
def show(self):
return '%02d:%02d:%02d' % \
(self._hour, self._minute, self._second)
What does %02d mean in the above code? Or why can't it be written as %2d?
%2d is to output the number with a width of 2 and right alignment. If the number of data digits is less than 2, spaces will be added to the left.
%02d is similar to %2d, except that 0 is added on the left
4. Edit the main program.main
def main():
clock = Clock(23, 59, 59)
while True:
print(clock.show())
sleep(1)
clock.run()
Let’s first look at the output of program execution, as follows:
Let’s take a look at the code of our main program:
Define a variable clock, passing in the parameter Clock(23, 59, 59) like our class.
Then the time.sleep() function of the time library is used here. The function of this function is mentioned above. In this way, the digital clock program is completed.
2. Summary
The use of the time library covers almost all kinds of projects. Here is a small example to gain a deeper understanding of the time library.
@Neng