How to learn the Python time module

The time module mainly contains various classes and functions that provide date and time functions. This module not only provides the function of formatting date and time into string, but also the function of recovering date and time from string.

Import the time module in the Python interactive interpreter, and then enter the [e for e in dir(time) if not e.startswith('_')] command to see all the attributes and functions contained in the module :

[ e for e indir(time)if not e.startswith('_')]['altzone','asctime','clock','ctime','daylight','get_clock_info','gmtime','localtime','mktime','monotonic','perf_counter','process_time','sleep','strftime','strptime','struct_time','time','timezone','tzname']

A time.struct_time class is provided in the time module. This class represents a time object. It mainly contains 9 attributes. The information of each attribute is shown in the following table:

Table 1 The meaning of each attribute in the time.struct_time class

Field name Field meaning Value
tm_year year such as 2017, 2018 etc.
tm_mon Month Such as 2, 3, etc., the range is 1~12
tm_mday Day Such as 2, 3, etc., the range is 1~31
tm_hour Hour such as 2, 3, etc., the range is 0~23
tm_min minutes such as 2, 3, etc., the range is 0~59
tm_sec seconds such as 2, 3, etc., the range is 0~59
tm_wday week Monday is 0, the range is 0~6
tm_yday Day of the year in a year For example, 65, range 1~366
tm_isdst Daylight saving time 0, 1, or -1

For example, Python can use time.struct_time(tm_year=2018, tm_mon=5, tm_mday=2, tm_hour=8, tm_min=0, tm_sec=30, tm_wday=3, tm_yday=1, tm_isdst=0) to clearly represent time .

In addition, Python can also use a tuple containing 9 elements to represent time. The 9 elements of this tuple have a one-to-one correspondence with the 9 attributes in the struct_time object. For example, the program can use (2018, 5, 2, 8, 0, 30, 3, 1, 0) to represent time.

The commonly used functions in the date and time module are as follows:

time.asctime([t]): Convert a time tuple or struct_time to a time string. If the parameter t is not specified, the current time will be converted by default.

time.ctime([secs]): Convert the time represented by seconds into a time string.

time.gmtime([secs]): Convert the time represented by seconds into a struct_time object. If no parameters are passed in, the current time is used.

time.localtime([secs]): Convert the time represented by seconds into a struct_time object representing the current time. If no parameters are passed in, the current time is used.

time.mktime(t): It is the inverse function of localtime, used to convert the time represented by the struct_time object or tuple into how many seconds have passed since 0:00 on January 1, 1970.

time.perf_counter(): Returns the value of the performance counter. In seconds.

time.process_time(): Returns the time that the current process uses the CPU. In seconds.

time.sleep(secs): Pause for secs and do nothing.

time.strftime(format[, t]): Format a time tuple or struct_time object into a time string in the specified format. If the parameter t is not specified, the current time will be converted by default.

time.strptime(string[, format]): Parse string format time into struct_time object.

time.time(): Returns how many seconds have passed since 0:00 on January 1, 1970.

time.timezone: Returns the time offset of the local time zone in seconds.

time.tzname: Returns the name of the local time zone.

The following program demonstrates the function of the time block:

import time
# Convert current time to time string
print(time.asctime())
# Convert the specified time into a time string, the last 3 elements of the time tuple are not set
print(time.asctime((2018,2,4,11,8,23,0,0,0))) # Mon Feb 411:08:232018
# Convert the time represented by seconds to a time string
print(time.ctime(30)) # Thu Jan 108:00:301970
# Convert time represented by seconds to struct_time object.
print(time.gmtime(30))
# Convert current time to struct_time object.
print(time.gmtime())
# Convert the time represented by the number of seconds into a struct representing the current time_time object
print(time.localtime(30))
# Convert time in tuple format to time represented by seconds
print(time.mktime((2018,2,4,11,8,23,0,0,0))) # 1517713703.0
# Returns the value of the performance counter
print(time.perf_counter())
# Returns the CPU time used by the current process
print(time.process_time())
# time.sleep(10)
# Convert the current time to a string in the specified format
print(time.strftime('%Y-%m-%d %H:%M:%S'))
st ='March 20, 2018'
# Restore the specified time string to struct_time object.
print(time.strptime(st,'%Y year%m month%d day'))
# Returns how many seconds have passed since 0:00 on January 1, 1970, 1970.
print(time.time())
# Returns the time offset of the local time zone in seconds
print(time.timezone) #Exported in Dongba District in China-28800

Run the above program, you can see the following output:

Fri Feb 2211:28:392019
Mon Feb 411:08:232018
Thu Jan 108:00:301970
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=30, tm_wday=3, tm_yday=1, 
tm_isdst=0)
time.struct_time(tm_year=2019, tm_mon=2, tm_mday=22, tm_hour=3, tm_min=28, tm_sec=39, tm_wday=4, tm_yday=53, 
tm_isdst=0)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=30, tm_wday=3, tm_yday=1, 
tm_isdst=0)1517713703.00.00.1406252019-02-2211:28:39
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=79, 
tm_isdst=-1)1550806119.4960592-28800

The two functions strftime() and strptime() in the time module are inverse functions of each other, where strftime() is used to convert struct_time objects or time tuples into time strings; and the strptime() function is used to convert time strings It is a struct_time object. Both functions involve writing format templates. For example, in the above program, %Y represents the year, %m represents the month, %d represents the day, %H represents the hour, %M represents the minute, and %S represents the second. The commands supported by the time format string required by these two functions are shown in the following table:

Instruction meaning
%a Localized abbreviated name of the day of the week, such as Sun for Sunday
%A Full name of localized day of the week
%b Localized abbreviated name of the month, such as Jan for January
%B Full name of the localized month
%c Localized date and time representation
%d represents the value of the day of the month, Fan Gu: 01~31
%H represents the hour in the 24-hour system, range: 00~23
%I represents the hour in 12-hour clock, range: 01~12
%j The day of the year, range: 001~366
%m represents the value of the month, range: 01~12
%M represents the value of minutes, range: 00~59
%p Localization of morning or afternoon. When using the strptime() function and using the %I instruction to parse the hour, %p only affects the hour field
%S represents the value of minutes, range: 00~61. The range is indeed 00~61, 60 is valid when representing the time stamp of a leap second, and 61 is caused by some historical reasons
%U represents the first few weeks of the year, with Sunday as the first day of the week, ranging from 00 to 53. In this way, the first Sunday of the year is considered the first week. When using the strptime() function to parse a time string, the command will only be effective if both the day of the week and the year are specified at the same time
%w represents the numeric value of the day of the week, range: 0~6, where 0 represents Sunday
%W represents the first few weeks of the year, with Monday being the first day of the week, and the range is from 00 to 53. In this way, the first Monday of the year is considered the first week. When using the strptime() function to parse a time string, the command will only be effective if both the day of the week and the year are specified at the same time
%x Localized date representation
%X Localized time representation
%y Abbreviation of year, range: 00~99, for example, 2018 is abbreviated as 18
%Y The complete form of the year. Such as 2018
%z Display time zone offset
%Z Time zone name (If the time zone is not available, it will be empty)
%% Used to represent the% symbol

Knowledge point expansion:

time module

This module provides various time-related functions. For related functions, please refer to the datetime and calendarat modules.

This module does not provide all functions on all platforms, and varies by platform

The following is an explanation of some terms and conventions

The above is how to learn the details of the Python time module. For more detailed information about the Python time module, please follow other related articles on ZaLou.Cn!

Recommended Posts

How to learn the Python time module
How to view the python module
How to learn python quickly
How to install the downloaded module in python
How to save the python program
How does Python handle the json module
How to change the CentOS server time to Beijing time
How to use the zip function in Python
Python uses the email module to send mail
How to use the format function in python
How to enter python through the command line
How to switch the hosts file using python
How to modify the CentOS server time to Beijing time
Python how to move the code collectively right
How to comment python code
How to add the gzip module to Nginx on Ubuntu 14.04
How to practice after the python file is written
How to understand python objects
How to use python tuples
How does python judge the module installation is complete
Python uses the re module to verify dangerous characters
How long does it take to learn python by myself?
How to find the area of a circle in python
How to set or modify the time zone on Ubuntu 20.04
How to set or modify the time zone on Ubuntu 20.04
How to set or modify the time zone in CentOS 8
python how to view webpage code
How Python implements the mail function
How does python change the environment
How to write python configuration file
How to wrap in python code
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to write classes in python
How to filter numbers in python
How to install Python on CentOS 8
How to solve python dict garbled
How to view errors in python
How to write return in python
How Python implements the timer function
How to understand variables in Python
How to clear variables in python
How to understand python object-oriented programming
Is python crawler easy to learn
How to use SQLite in Python
Python3 module
Learn the basics of python interactive mode
How to verify successful installation of python
How to make a globe with Python
How to use and and or in Python
python Douban replaces pip to install python module
How to delete cache files in python
How to introduce third-party modules in Python
How does python improve the calculation speed
How to represent null values in python
How to save text files in python
Python string to judge the password strength
How to use PYTHON to crawl news articles
Python novices learn standard library module naming
How to write win programs in python