In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Python how to deal with time, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
# #
# time.time
The representation of time in time module is in the form of timestamp, which is a method of counting time without entanglement. Then let's take a look at the methods that need to be learned in the time module.
Import time
Now = time.time ()
Print now
# 1496065442.08
First of all, you can use the time.time method to check the current time. As a result, he returns 1496065,444. Zero eight. What does this mean? I want to see what time it is. You show me this?! Don't worry, the counting method for time modules starts at 12:00 on January 1, 1970, which is 0. 5%. Then add 1. 5% every second. So which one was more than 1.4 billion just now, and that's all in seconds for the past 40 years or so.
So what's the use of this? I don't like it. How many seconds are there in the past? In fact, this method is more commonly used, generally used to calculate the running time of the program, such as:
Start_time = time.time ()
One_plus = range
For x in one_plus:
Z = x + 100
Print time.time ()-start_time
# 0.000999927520752
And what are the benefits of computing program time? Time is life, time is money! When we find that our developed algorithm has become unusually slow, we should consider whether there is something wrong with the program. And in the training algorithm, consciously calculate the running time of each epoch, you can analyze how long it takes for the whole program to run.
# time.ctime (), time.localtime ()
C_time = time.ctime ()
Print c_time
# Mon May 29 21:58:26 2017
Local_time = time.localtime ()
Print local_time
# time.struct_time (tm_year=2017, tm_mon=5
Tm_mday=29, tm_hour=21, tm_min=58, tm_sec=26
Tm_wday=0, tm_yday=149, tm_isdst=0)
Localtime () returns the local time, including year, month, date, hour, minute, second, day of the week, from 0 to 6, from Monday to Sunday. Tm_yday indicates the number of days of the year, the last one indicates whether it is daylight saving time, and 0 means no.
# time.strftime and time.strptime
Strftime is a common method for formatting output time, which means to output time in a given format. Of course, this format is not your own random format, it should be in line with the Python format. Strftime can convert the time of time type to str character type. Striptime, on the other hand, converts character time into time type. Look at a chestnut.
Import time
Print time.strftime ("% Y_%M_%d_%H_%M_%S")
# 2017_16_29_22_16_13
Print time.strptime ('2017-5-299,'% YMY% MMI% d')
# time.struct_time (tm_year=2017, tm_mon=1, tm_mday=29
Tm_hour=0, tm_min=5, tm_sec=0, tm_wday=6, tm_yday=29
Tm_isdst=-1)
Print time.strptime ("2017 May 5, May 29, 22, 14, 22, 21, 15, 45"
"% Y_%m_%d.%H:%M:%S")
# time.struct_time (tm_year=2017, tm_mon=5
Tm_mday=29, tm_hour=22, tm_min=21, tm_sec=45
Tm_wday=0, tm_yday=149, tm_isdst=-1)
# # datetime # #
# datetime.datetime, datetime.now, datetime. Strftime
Datetime returns more output forms that we have seen, now can get the current date, and strfttime is the function to format the output, which is suitable for both the time class and the datetime class. Then we compare the difference of the output corresponding to different formats, here basically include all possible outputs, the rest is that you can combine them at will. The main difference lies in case and the meaning expressed by different letters. There is no need to remember this, I think, find an output format that pleases your eyes, and then use that every time you use it.
From datetime import datetime
Now = datetime.now ()
Print now.strftime ("% Y")
Print now.strftime ("% Y% B")
Print now.strftime ("% Y_%B_%d")
Print now.strftime ("% Y_%B_%d.%H")
Print now.strftime ("% Y_%B_%d.%H_%M")
Print now.strftime ("% Y_%B_%d.%H_%M_%S")
# 2017
# 2017_May
# 2017_May_29
# 2017_May_29.22
# 2017_May_29.22_30
# 2017_May_29.22_30_26from datetime import datetime
Now = datetime.now ()
Print now.strftime ("y")
Print now.strftime ("% yearly% m")
Print now.strftime ("% y_%m_%d")
Print now.strftime ("% y_%m_%d.%I")
Print now.strftime ("% y_%m_%d.%I_%m")
Print now.strftime ("% y_%m_%d.%I_%m_%f")
# 17
# 17_05
# 17_05_29
# 17_05_29.10
# 17_05_29.10_05
# 17059.1005562000 # millisecond output after reading the above content, have you mastered the method of how to deal with time in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.