directory
- A preface.
- 2.Python time module common functions introduced
- Python time module
- 1. The Python Time module obtains the time
- 2. The Python Time module defines a custom time format
- 3. The Python Time module checks the execution time of the code
- Four. Guess you like it
Python Basics: Python Basics
The Python Time module consists of functions that provide date and time functions. The time module not only provides the function of formatting the date and time into a string, but also provides the function of restoring the date and time from the string.
A preface.
Struct_time ** class. This class represents a time object. It mainly contains 9 attributes, and the information of each attribute is shown in the figure below:
2.Python time module common functions introduced
-
**time.gmtime([secs]) ** – Converts the time in seconds to a struct_time object. If no parameter is passed, the current time is used.
-
Time.asctime ([t]) – Converts a time tuple or struct_time to a time string. If t is not specified, the current time is converted by default.
-
Time.localtime ([secs]) – Converts a time in seconds to a struct_time object representing the current time. If no parameter is passed, the current time is used.
-
Time.sleep (secs) – Pause secs for a second and do nothing;
-
**time.strftime(format[, t]) ** – Formats a time tuple or struct_time object into a time string in the specified format. If t is not specified, the current time is converted by default.
-
**time.strptime(string[, format]) ** — Parse a string of time into a struct_time object;
-
**time.time() ** – Returns the number of seconds since 0:00 on January 1, 1970;
-
Time. tzname – Returns the name of the local time zone;
! usr/bin/env python
–– coding:utf-8 _–
“”” @author: @blog (personal Blog): Py @time :2021/3/28 07:37 @motto: A thousand miles can’t be achieved without a single step, and a river can’t be achieved without a small stream. The wonderful program life needs to be accumulated unremitting!
“” “
Gets the name of the local time zone
local_time = time.tzname[0]
If the encoding format is not set, the output is garbled characters
print(local_time.encode(‘latin-1’).decode(‘gbk’))
Output: China Standard time
Python time module
1. The Python Time module obtains the time
#! Usr /bin/env python # -* -coding :utf-8 _*- """ @author: Py @time :2021/3/28 07:37 @motto: A thousand miles can't be achieved without a single step, and a river can't be achieved without a small stream. The wonderful program life needs to be accumulated unremitting! """ """ """ """ """ The default format is print(time.asctime()) # returns an object of type time.struct_time print(time.gmtime()) # returns an object of type time.struct_time Print (time.localtime()) # print(time.localtime()) # print(time.localtime()) # Thu Nov 7 21:26:29 2019 time.struct_time(tm_year=2019, tm_mon=11, tm_mday=7, tm_hour=13, tm_min=26, tm_sec=29, tm_wday=3, tm_yday=311, tm_isdst=0) time.struct_time(tm_year=2019, tm_mon=11, tm_mday=7, tm_hour=21, tm_min=26, Tm_wday =3, tM_yday =311, tm_isdst=0)Copy the code
2. The Python Time module defines a custom time format
The time.strftime function can be used to convert a struct_time object to a custom time format or to convert a string to a struct_time object. The following format parameters may be used during the conversion:
- %a day of the week;
- %A full name of day of the week;
- %b – short for months;
- % full name of month B;
- %c Standard date time string;
- %C Last two digits of the year;
- %d Decimal day of the month;
- %D month/day/year;
- %e The decimal day of the month in a two-character field;
- %F year – month – day;
- %g The last two digits of the year, using the week based year;
- %G year points, using week-based years;
- %h abbreviated month name;
- %H Hours in the 24-hour system;
- %I 12 hours;
- **%j ** The day of the year in decimal notation;
- %m Decimal month;
- %M The number of minutes in the ten-hour system;
- %n new line character;
- %p local AM or PM equivalent display;
- %r 12 hours of time;
- %R displays hour and minute: hh:mm;
- %S Number of decimal seconds;
- %t horizontal TAB;
- %T: hh:mm:ss;
- %u Specifies the day of the week. Monday is the first day (value 0 to 6, Monday is 0);
- %U Week of the year, with Sunday as the first day (values 0 to 53);
- %V The week of the year, using a week-based year;
- %w Day of the week in decimal notation (values 0 to 6, with Sunday being 0);
- %W Week of the year, with Monday as the first day (values 0 to 53);
- %x standard date string;
- %X standard time string;
- %y Decimal year without century (values from 0 to 99);
- %Y The tenth year with the century part;
- %z, %z time zone name, null if no time zone name is available;
- %% percent sign;
Convert the struct_time object to a custom time format. The example code is as follows:
#! Usr /bin/env python # -* -coding :utf-8 _*- """ @author: Py @time :2021/3/28 07:37 @motto: A thousand miles can't be achieved without a single step, and a river can't be achieved without a small stream. The wonderful program life needs to be accumulated unremitting! Import time str_time = time.strftime('%Y-%m-%d %H:% m :%S', Print (str_time) str_time = time.strftime('%Y/%m/%d %H:% m :%S', Print (str_time) str_time = time.strftime('%Y/%m/%d %H-% m -%S', String (STR) print(str_time) "" 2019-11-07 14:14:59 2019/11/07 22:14:59 2019/11/07 22-14-59 ""Copy the code
3. The Python Time module checks the execution time of the code
In the course of Python development projects, we tend to optimize the code to find out which code is time-consuming and how much, so the same can be done through the time module
# import time start_time = time.time() # Code block elapse_time = time.time() -start_time # Unit: secondCopy the code
Code analysis: obtain the current time from time.time(), and then subtract the two times to get the time of the code, in seconds. Example code:
#! Usr /bin/env python # -* -coding :utf-8 _*- """ @author: Py @time :2021/3/28 07:37 @motto: A thousand miles can't be achieved without a single step, and a river can't be achieved without a small stream. The wonderful program life needs to be accumulated unremitting! """ "import time def my_model(): Time.sleep (0.5) end_time = time.time() print(" {} second ".format(end_time-start_time)) if __name__ == "__main__":Copy the code
If one day you use such code to deceive your project experience or boss, when your leg is broken, please don’t mention me!
Four.Guess you like
- Python Configuration Environment
- Python variable
- Python operator.
- Python conditions determine if/else
- Python while loop
- Python break
- Python continue
- The Python for loop
- The Python string
- The Python list
- The Python tuple tuple
- Python dictionary dict
- Python conditional derivation
- Python list derivation
- Python dictionary derivation
The Python Time module
This article is posted by the blog – Ape says Programming Ape says Programming!