Reference Blog: http://blog.csdn.net/SeeTheWorld518/article/details/48314501
Http://www.jb51.net/article/49325.htm
Functions commonly used in the time module
Function |
Function |
Time.time () |
Get time stamp |
Time.localtime () |
Get the time element ancestor and convert the timestamp to the time element ancestor |
Time.gmtime () |
Get the time element ancestor and convert the timestamp to the time element ancestor |
Time.mktime |
Convert a time primitive to a timestamp |
Time.strftime () |
Get format time and convert time primitive to formatted time |
Time.sttptime () |
Convert a formatted time to a time element ancestor |
Time.asctime () |
Convert time-primitive ancestors to fixed formats |
Time.ctime () |
Convert timestamps to fixed formats |
Time.sleep () |
Rest |
Time.clock () |
|
In our usual code, we often need to deal with time.
In Python, the time-processing-related modules are: times, Da ' tetime, and calendar.
Learn to calculate time, the program is very important to tune, you can in the program to beat the time stamp, to judge the program in the most time-consuming, so as to find the center of the program tuning.
The following will be the most widely used time modules, the other modules follow the opportunity to continue to explain.
First, there are a few things to note:
(1) In Python, there are usually several ways to represent time: a timestamp, a formatted time string, and a time Ganso (struct_time total of nine elements).
(2) timestamp (timestamp): The timestamp indicates that the offset is calculated by seconds from 00:00:00 January 1, 1970.
(3) Time Yuan zu: The time element ancestor has nine elements, the function that returns the time element ancestor mainly has gmtime (), localtime (), Strptime ().
One, three ways to express time
1. Time stamp
Import time >>> time.time ()1510666111.4657724 # number of seconds from 0 o'clock January 1, 1970
2. Format time
>>> time.strftime ("%y-%m-%d%h:%m:%s")'2017-11-14 21:29:32'
The
meaning of parameters in a common formatted time string
%Y |
Full year (4 numbers) |
%m |
Month (1-12) |
%d |
Day of the one months (1-31) |
%H |
Number of hours of the day (0-23) |
%M |
Number of minutes (0-59) |
%s |
Seconds (0-59) |
%c |
Time string of the standard date, example: ' Wed 15 00:43:54 2017 ' |
%c |
The following two digits of the year |
%d |
Month/day/year, example: ' 11/15/17 ' |
%H |
24-hour system |
%I |
12-hour system |
%F |
Year/month/day, example: ' 2017-11-15 ' |
%T |
Display hours and seconds, example: ' 00:50:00 ' |
%x |
Standard date String, example: ' 11/15/17 ' |
%x |
Standard Time String, example: ' 00:50:55 ' |
%a |
Shorthand for the day of the week, example: ' Wed ' |
%A |
Full name of the day of the week, example: ' Wednesday ' |
%b |
Abbreviated month, example: ' Nov ' |
%B |
Full name of the month, example: ' November ' |
%V |
The week ordinal of the year |
%u |
The day ordinal of the week |
%u |
The first week of the year |
3. Time Yuan zu
>>> time.localtime () # local time time.struct_time (tm_year=2017, tm_mon=11, tm_mday=14, tm_ Hour=21, tm_min=29, tm_sec=46, Tm_wday=1, tm_yday=318, tm_isdst=0)>>> time.gmtime () # Standard Time, relative to London 8 hours earlier time.struct_time (tm_year=2017, tm_mon=11, tm_mday=14, tm_hour=13, tm_min=30, tm_sec=30, tm_ Wday=1, tm_yday=318, tm_isdst=0)
We call it time Yuan Zu, although not the Yuan Zu, but similar to the Yuan Zu.
# values can be indexed by index >>> time_tuple[0]2017>>> time_tuple[1]
11
The meaning of the elements represented in the time element ancestor
Tm_year |
Years |
Tm_mon |
Month (1-12) |
Tm_mday |
Day (1-31) |
Tm_hour |
Hours (0-23) |
Tim_min |
Points (1-59) |
Tm_sec |
Seconds (0-59) |
Tm_wday |
Day of the week (0-6,0 = Sunday) |
Tm_yday |
Day of the Year (1-365) |
Tm_isdst |
Whether it is daylight saving time (Default-1) |
Ii. functions commonly used in time modules
When using the function in the module, you must first introduce the module (import time)
1.time.time (): Returns the current time timestamp.
>>> time.time ()1510671831.6499062
2.time.localtime (): There are two functions: (1) Return to the current time element ancestor; (2) Convert a timestamp to a time element ancestor.
Time.localtime () is called by default when no arguments are passed
# get local time element ancestor Time.struct_time (tm_year=2017, tm_mon=11, tm_mday=14, tm_hour=23, tm_min=5, tm_sec=4, Tm_wday=1, tm_ yday=318, tm_isdst=0)>>> time.localtime (Time.time ())# convert a timestamp to a time element ancestor time.struct_ Time (tm_year=2017, tm_mon=11, tm_mday=14, tm_hour=23, tm_min=7, tm_sec=28, Tm_wday=1, tm_yday=318, tm_isdst=0)
3.time.gmtime (): As with the Time.localtime () function, return the current time ancestor and convert a timestamp to a time element ancestor.
# get time Yuan progenitor time.struct_time (tm_year=2017, tm_mon=11, tm_mday=14, tm_hour=15, tm_min=9, tm_sec=12, Tm_wday=1, tm_ yday=318, tm_isdst=0)>>> time.gmtime (Time.time ()) # convert a timestamp to a time element ancestor Time.struct_time (tm_year=2017, tm_mon=11, tm_mday=14, tm_hour=15, tm_min=9, tm_sec=18, Tm_wday=1, tm_yday=318, tm_ isdst=0)
4.time.mktime (): Converts a time-element ancestor to a timestamp.
>>> time_tuple = time.localtime ()>>> time.mktime (time_tuple)1510672372.0
5.time.strptime (): Converts a formatted time string to a time element ancestor.
>>> time.strptime ('2017-11-14 16:23:33',"%y-%m-%d%m:%h:%s " ) time.struct_time (tm_year=2017, tm_mon=11, tm_mday=14, tm_hour=23, tm_min=16, tm_sec=33, Tm_wday=1, tm_yday= 318, Tm_isdst=-1)
6.time.strftime (): There are two functions: (1) Get the formatted time string, (2) Convert the time primitive to the format time.
#Get format Time>>> Time.strftime ("%y-%m-%d%m:%h:%s")'2017-11-14 16:23:33'#Convert time-primitive ancestors to formatted time>>>Time_tupletime.struct_time (Tm_year=2017, tm_mon=11, tm_mday=14, tm_hour=23, tm_min=12, tm_sec=52, Tm_wday=1, tm_yday=318, tm_isdst=0)>>> Time.strftime ('%y-%m-%d%m:%h:%s', Time_tuple)#time format in front'2017-11-14 12:23:52'
7.time.asctime (): A Ganso or struct_time representing time is represented as ' Tue 14 23:12:52 2017 ' in this form, and if no arguments are passed, the default is Time.localtime () as the parameter.
# the time Ganso is expressed in fixed form >>> time_tupletime.struct_time (tm_year=2017, tm_mon=11, tm_mday=14, tm_ Hour=23, tm_min=12, tm_sec=52, Tm_wday=1, tm_yday=318, tm_isdst=0)>>> time.asctime (time_tuple) 'Tue 23:12:52'# If no parameters are specified, the default is Time.localtime () as a parameter > >> time.asctime ()'Tue-23:27:59'
8.time.ctime (): Converts a timestamp to ' Tue 14 23:32:58 2017 ' This form, if no parameters are specified, default to Time.local () as the parameter
# convert timestamp >>> time.ctime (1000000000)'Sun Sep 9 09:46:40 2001' # default >>> time.ctime ()'Tue-23:32:58'
9.time.sleep (): Thread delays the specified time run. The default unit is seconds.
>>> Time.sleep (10)
10.time.clock ()
Python Common Module--time module