Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Python time time conversion

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "sample analysis of Python time time conversion", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and study the "sample analysis of Python time time conversion".

Review and review

We already know that there are three time formats in the Python built-in module-time.

Time stamp

Structured time

String time

In this issue, we will continue to learn more about the three times involved in the time module, Let's go~

1. Time module source code description

After we import time through import time, we can view the time.pyi file after long pressing the CTRL key.

Looking at the time.pyi file, we will get the following information

The type and time of the attribute in the time module are all int.

List the method header name and return value information. You can view the details of the method by jumping to the left asterisk.

2. Core object of time module

The time module obtains the number of seconds (integer or floating point) through the timer at the bottom of the system, which is often referred to as POSIX timestamp.

There is only one class in the time module: time.struct_time. Type is tuple

Struct_time converts timestamps into structured time objects, and time-related attributes are stored in the nametuple tuple.

You can also call the gmtime,localtime,strptime method to get the resulting time object.

3. Timestamp and structured time conversion

Time stamp converted to structured time

Use time.localtime ([sec]) to convert to a struct_time object in a local time zone. If sec is empty, the return value obtained by time.time () is used

Local = time.localtime (1635566119) print ("local timestamp converted to structured time:", local)

Use time.gmtime ([sec]) to convert to a struct_time object in the UTC area. If sec is empty, the return value obtained by time.time () is used

Now = time.time () gmt = time.gmtime (now) print ("utc timestamp converted to structured time:", gmt)

Conversion of structured time to timestamp

Use time.mktime (t) to convert a structured object into seconds

Mk = time.mktime (time.localtime ()) print (mk)

4. Conversion between string time and structured time

Convert string time to structured time

Use time.strptime (string [, format]) to parse a string into a structured time according to the specified fromat encoding, with a default value of "a b d H:%M:%S Y"

Str = time.strptime ("2021-10-29 21:07:24", "% Y-%m-%d% H:%M:%S") print (str)

Conversion of structured time to string

Use time.strftime (fromat, [, t]) to format the structured time object into a string according to the specified fromat encoding, and t defaults to the return value of time.localtime ()

Strf = time.strftime ("% Y-%m-%d H:%M:%S") print (strf)

The above is all the contents of the article "sample Analysis of Python time time conversion". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report