In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use the time module python, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
Time processing is a common operation in work. Let's introduce the common operations of the time module. Before we begin, let's look at the frequently asked questions:
1: how do I get the current time?
2: how do I convert time into a string?
3: how to convert a string into time, and you can get the corresponding year, month, day, hour, minute and second?
4: how to change the time to the format you want?
Mastering the time module, the above problems can be solved easily.
1. Time type division 1.1 Import time module import time1.2 time module
There are so many methods of time module, how to distinguish them?
According to the time format, it is divided into:
A) timestamp
B) struct_time format (time format, you can easily get the values of year, month, day, hour, minute and second)
C) string time (good readability)
The following is explained in detail one by one (the following operations are performed in the jupyter,python3.x version)
2 timestamp processing
Follow your feelings:
2.1 get timestamp
What is a timestamp? It's a large floating-point number that computers use to record time, probably starting sometime in 1970.
Get the timestamp in the correct posture:
Time.time (): gets the timestamp and returns a floating-point number in import timet=time.time () t.
Results:
1582968461.3394606
It doesn't mean much to us to get the timestamp. How can we get the corresponding year and hour?
2.2 timestamp to struct_time:
Struct_time is an object, similar to a tuple, which mainly contains time information
Attribute description tm_ yearbook tm_mon month [01jue 31] tm_ mdayday tm _ hour hours [00mem12] tm_min minutes [00cm59] tm_sec seconds [00je 59] tm_wday week [0je 6] days of tm_isdst Daylight time beginning on January 1st of each year (ignored for the time being)
With this structure, the acquisition time is too easy, how to get it?
Method: time.localtime ([sec]) # get current time st = time.localtime () # get annual print (st.tm_year) # get hourly print (st.tm_hour) # timestamp to struct_timet=1582968461print (time.localtime (t))
Output result:
202017time.struct_time (tm_year=2020, tm_mon=2, tm_mday=29, tm_hour=17, tm_min=27, tm_sec=41, tm_wday=5, tm_yday=60, tm_isdst=0) 2.3timestamp turns time string
Time.ctime (seconds): converts a timestamp to a readable string
# get current time print (time.ctime ()) # timestamp convert string t=1582968461print (time.ctime (t))
Output result:
Sat Feb 29 17:46:18 2020Sat Feb 29 17:27:41 20203 struct_time processing
The main operations are as follows:
Method description time.mktime (tuple) struct_time to timestamp time.asctime ([tuple]) struct_time to time date string time.strftime (format [, tuple]) struct_time to specified time format string
Let's take a look at the first two operations:
# get the current time st = time.localtime () # struct_time to timestamp print (time.mktime (st)) # struct_time to string print (time.asctime (st))
Output result:
1582974399.0Sat Feb 29 19:06:39 2020
How do I convert struct_time to a custom format string?
Time.strftime (format [, tuple]): format is the specified format, and tuple is the struct_time object
The format format can be viewed through help (time.strftime). The main format is as follows:
Format description% Y year: [xxxx]% y year: [xx], without century% m-month: [01jue 12]% d-day: [01recorder 3]% H-hour: [00jue 23]% M minute: [00jue 59]% s second: [00je 59]% x date: [month / day / year]% X time: [hours: minutes: seconds]
Example:
St = time.localtime () #-month-day print (time.strftime ('% Ymuri% MMI% dwells, st)) # hour-minute-second print (time.strftime ('% Hmure% MMI% Songjinst)) #-month-day: minute print (time.strftime ('% Y-%m-%d% Hrig% MRV% st)
Results:
2020-02-2919-19-312020-02-2919: 19:31 characters change time to time
For example, how to convert "2020-02-29 19:19:31" to struct_time or timestamp?
Strptime (string, format), contrary to strftime, specific operations:
S = '2020-02-29 19:19:31'st = time.strptime (st)
Output result:
Time.struct_time (tm_year=2020, tm_mon=2, tm_mday=29, tm_hour=19, tm_min=19, tm_sec=31, tm_wday=5, tm_yday=60, tm_isdst=-1)
These are the main methods of time conversion.
Summary: timestamp to string process # get current time error t = time.time () print (t) # to struct_timest = time.localtime (t) print (st) # st to custom format string s = time.strftime ('% Y-%m-%d% Hpurs% MRV% slots, st) print (s)
Output result:
1582975571.0656374time.struct_time (tm_year=2020, tm_mon=2, tm_mday=29, tm_hour=19, tm_min=26, tm_sec=11, tm_wday=5, tm_yday=60, tm_isdst=0) 2020-02-29 19:26:11 string converted to timestamp s = '2020-02-29 19Frei 26Swiss # string converted to struct_timest = time.strptime (s '% Y-%m-%d% HVA% MVA% S') print (st) # struct_time to time stamp t = time.mktime (st) print (t)
Results:
Time.struct_time (tm_year=2020, tm_mon=2, tm_mday=29, tm_hour=19, tm_min=26, tm_sec=11, tm_wday=5, tm_yday=60, tm_isdst=-1) 1582975571.0
Keep these operations in mind to meet most of the requirements.
The above is all the content of the article "how to use the time module in python". 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.