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.
Review and
We already know that there are currently three time formats in Python’s built-in time-module.
- The time stamp
- Structured time
- String time
In this issue, we will continue to learn more about the three kinds of time in the time module, Let’s go~
1. Time module source code description
After importing time, you can view the time.pyi file by holding down the CTRL key
Looking at the time.pyi file, we see the following
- The type of the attribute in the time module. The time is int
- Lists the method header name and return value information. You can jump to the left asterisk to see the method details
2. Core object of time module
The time module gets the number of seconds (of type integer or floating point) from the underlying system timer, commonly known as POSIX timestamps.
The time module has only one class: time.struct_time. The type is tuple
Struct_time converts the timestamp into a structured time object, and time-related attributes are stored in the nametuple.
At the same time also can call gmtime localtime, strptime method to get a result time object.
3. Timestamp and structured time conversion
-
The timestamp is converted to structured time
- Use time.localtime([SEC]) to convert to a struct_time object for the localtime zone. If SEC is empty, the return value from time.time() is used
local = time.localtime(1635566119) print("Local timestamp converted to structured time :",local) Copy the code
- Gmtime ([SEC]) is converted to a struct_time object in the UTC area. If SEC is empty, the return value from time.time() is used
now = time.time() gmt = time.gmtime(now) print("Utc timestamp converted to structured time :",gmt) Copy the code
-
Structured time is converted to timestamps
Use the time. Mktime (t) converts a structured object to a number of seconds
mk = time.mktime(time.localtime()) print(mk) Copy the code
4. Conversion of string time to structured time
-
String time is converted to structured time
Use the time. Trptime (string[,format]) parses a string to a structured time using the specified Fromat encoding. The format default value is “% A % B %d %H:%M:%S %Y”.
str = time.strptime("The 2021-10-29 21:07:24"."%Y-%m-%d %H:%M:%S") print(str) Copy the code
-
Structured time is converted to a string
Strftime (fromat,[,t]) formats the structured time object as a string according to the specified fromat encoding, with t defaulting to the return value of time.localtime()
strf = time.strftime("%Y-%m-%d %H:%M:%S") print(strf) Copy the code
conclusion
This issue mainly combs and summarizes the three forms of Time format in Time module. The core object of time module is struct_time, through which time can be converted in different forms.
That’s the content of this episode. Please give us your thumbs up and comments. See you next time