Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money.
preface
We all need time and dates in our daily lives. Not only do our Chinese characters have a large number of words to describe time and date, such as the passage of time, the minute, the minute, the blink of an eye, etc.
Similarly, in our program, we can’t do without time and date records.
In The Python high-level language, the built-in libraries provide the Time and datetime modules for time and date processing.
In this issue, we will learn the methods related to the Time module, let’s go~
1. The description of the time
Computer network time began at 0 o ‘clock on January 1, 1970, the first year of UNIX
The Python Time module mainly accesses and converts time
There are three forms of time:
- Timestamp: is a floating point number that can be added or subtracted
- Format time string (string_time) : indicates the common time format 2021-10-29
- Struct_time: a tuple containing years, months, days, hours, minutes and seconds.
🔔 Important Note:
- The Python Time module is currently supported for 2038 only.
- UTC (Universal Coordinated Time), also known as Greenwich Mean Time or Universal Standard Time
- DST DST time. Advance one hour at a time based on local time
- Python time is an immutable type, read-only and cannot be changed
2. Format the time string
format | meaning |
---|---|
%a | Short for the name of the local week (e.g. Thu on Thursday) |
%A | Full English name of the local week (e.g. Thursday) |
%b | Short for names of local months (e.g. August agU) |
%B | Full English name of a local month (e.g. August) |
%c | Local string representation of the corresponding date and time (e.g. 15/08/27 10:20:06) |
%d | What’s the date of the month (01-31) |
%f | Microseconds (range 0.999999) |
%H | Number of hours in a day (00-23 on a 24-hour basis) |
%I | Number of hours (12-hour system, 0-11 |
%j | Days of the year (001-366) |
%m | Month (01-12) |
%M | Minutes (00-59) |
%p | Local AM or PM identifier |
%S | seconds |
%U | Number of weeks in a year. (00-53 Sunday is the beginning of the week.) |
%w | Day of the week (0-6, 0 is Sunday) |
%W | Number of weeks of the year (1-54) |
%x | Local corresponding date string (such as 15/08/01) |
%X | Local corresponding time string (e.g. 08:08:10) |
%y | Remove the year of the century (00-99) represented by two numbers |
%Y | Complete year (4 digits to indicate a year such as 2021) |
%z | Interval from UTC time (empty string if local time) |
%Z | The name of the time zone (empty string if local time) |
% % | ‘%’ character |
3. Time structure
index | attribute | Value range |
---|---|---|
0 | Tm_year (years) | For example, 2021 |
1 | Tm_mon (month) | 1 ~ 12 |
2 | Tm_mday (day) | 1 to 31 |
3 | Tm_hour (when) | 0 ~ 23 |
4 | Tm_min (points) | 0 ~ 59 |
5 | Tm_sec (in seconds) | 0 to 61(60 or 61 is a leap second) |
6 | Tm_wday (weekday) | 0 ~ 6 |
7 | Tm_yday (day of the year) | 1~366 |
8 | Tm_isdst (whether daylight saving time or not) | – 1 |
4. Time-related methods
methods | role |
---|---|
time.sleep(t) | Sleep t seconds. T can be a floating point number or an integer |
time.time() | Gets the current system timestamp |
time.gmtime(sec) | The timestamp is converted to the structured time of UTC, with SEC defaulting to time.time() as the argument |
time.localtime(sec) | The timestamp is converted to the current structured time, with SEC defaulting to time.time() as the argument |
time.ctime(sec) | Timestamp conversion to a formatted string for local time, SEC default time.time() as argument |
time.asctime(t) | Converts structured time to a formatted string with time.localtime() as an argument |
time.mktime(t) | Convert structured time to timestamp |
time.strftime(format,[t]) | The local time that converts structured time to the specified string format is passed in by defaulttime.localtime() |
time.strptime(string,[t]) | Convert formatted time to structured time |
time.clock() | Returns the CPU time for executing the current program |
5. Test the cat
import time
Get the local timestamp
stamp = time.time()
Get the local GMT time
gmt = time.gmtime()
Get local time
local = time.localtime()
# local time formatting
strtime = time.ctime()
Convert structured time to formatting characters
asctime = time.asctime()
# convert structured time to timestamp
constamp = time.mktime(local)
# Convert structured time to specified string format time
strf = time.strftime("%Y-%m-%d %H:%M:%S")
Convert formatted time to structured time
str = time.strptime("The 2021-10-29 21:07:24"."%Y-%m-%d %H:%M:%S")
print("Get local timestamp :",stamp)
print("Get local GMT time :",gmt)
print("Get local time :",local)
print("Local time Formatting :",strtime)
print("Structuring time to format character :",asctime)
print("Convert structured time to timestamp :",constamp)
print("Convert structured time to specified string format time :",strf)
print("Convert formatted time to structured time :".str)
Copy the code
conclusion
In this issue, we understand and learn the three time formats involved in the Time module, and use the commonly used time method.
Time module in our program, can help us quickly find a certain point in the log details.
That’s the content of this episode. Please give us your thumbs up and comments. See you next time