Import time
One, time acquisition function
Time (), CTime (), Gmtime ()
>>> Import Time>>> Time. Time()1524297783.3058376>>> Time. CTime ()'Sat Apr 16:03:09 2018'>>> Time. Gmtime () Time. Struct_time (tm_year=2018, tm_mon=4, tm_mday= +, tm_hour=8, tm_min=4, tm_sec=6, tm_wday=5, tm_yday=111, tm_isdst=0)
Second, the time of the format
The Strftime (TPL, TS) TPL is a time-formatted template string used to define the output effect, and TS is the machine's internal time-type variable.
>>> t=time gmtime ()time strftime ("%y-%m-%d%h:%m:%s" , T)'2018-04-21 08:05:49'
%Y year %m month %B month name January%B month name abbreviation Jan%D Date %a week Monday%a week abbreviation Mon%H %H p last afternoon %M minutes %s seconds
Examples are as follows:
>>> Time. Strftime ("%y-%b-%d-%a-%h-%p-%s")'2018-april-21-saturday-16-pm-10'>>> Time. Strftime ("%a-%p")'saturday-pm'>>> Time. Strftime ("%m:%s")'15:39'>>> Time. Strftime ("%m:%s")'15:45'>>> Time. Strftime ("%m:%s", T)'05:49'
If Strftime does not have a second argument, the current time is obtained by default.
Strptime (Timestr, "%y-%m-%d%h:%m:%s") is converted to a struct based on the time string and the formatted output.
>>> timestr'2018-01-26 12:55:33'time. Strptime (Timestr, "%y-%m-%d%h:%m:%s")time. Struct_time (tm_year=2018, tm_mon= 1, tm_mday=, tm_hour=,tm_min=, tm_sec=, tm_wday= 4, tm_yday=, tm_isdst=-1)
Third, the timing of the program
Measurement time: Perf_counter () returns the exact time count value of a CPU level, which is good, since the starting point of this count value is not deterministic, successive calls using the difference are meaningful.
>>> start=timeperf_counter ()>>> start3.9111116077602044e-06 >>> end=timeperf_counter ()>>> end10.212393474589648 >>> End- start10.212389563478041
Generation time: Sleep (s) s wonderful sleep time, can be a floating point number, such as time.sleep (3.5)
Here is an example of a Time progress bar:
#TextProBarV3. Pyimport Time Scale= -Print ("Start". Center (Scale//2, "-"))Start = Time. Perf_counter () forIinchRange (Scale +1): A="*"*I b="."* (Scale-i) C= (i/scale) * -dur= Time. Perf_counter ()-Start Print ("\r{:^3.0f}%[{}->{}]{:.2f}s". Format (C,a,b,dur), end=""" here \ r, cursor back to the beginning of the line, end=" "means no output space, string continuous output ' ' Time.Sleep(0.1) Print ("\ n"+"End". Center (Scale//2, "-"))
Python Learning Notes (5) using the-time library