Python uses the time library to deal with time issues

The time library is a standard library for processing time in python, which mainly has the following three functions:

  • Expression of computer time

  • Get system time and format output

  • System and precise timing for program performance analysis

The time library mainly includes three types of functions:

  • Time acquisition: time() ctime() gtime()

  • Time formatting: strftime() strptime()

  • Program timing: sleep() perf_counter()


Time acquisition

function description
time() Get the current computer internal time value, which is a floating point number with extremely high precision
>>>time.time()
1516939876.6022282
ctime() Get the current time and return a readable string
>>>time.ctime()
'Tues Apr 24 23:11:16 2018'
gmtime () Get the current time, expressed as a format that the computer can handle
>>time.gmtime()
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=23, tm_min=11, tm_sec=16, tm_wday=4, tm_yday=26, tm_isdst=0)

Time formatting

function description
strftime(tpl, ts) Display the time in a reasonable way.
tpl is a formatted template string used to control the output format, and ts is a computer internal time variable
>>>t = time.gmtime()
>>>time.strftime("%Y- %m-%d %H:%M:%S", t)
'2018-04-24 23:21:28'
strptime(str, tpl) Contrary to the strftime() function, it converts the formatted time string into a format that can be processed by the computer
>>>timeStr = '2018-04-24 23:21:28'
>>>time,strptime(timeStr, "% Y-%m-%d %H:%M:%S”)
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=24, tm_hour=23, tm_min=21, tm_sec=28, tm_wday=4, tm_yday =26, tm_isdst=0)

Program timing

function description
sleep(s) s is the sleep time, in seconds
>>>time.sleep(3.3) #The program will sleep for 3.3 seconds
perf_counter() Returns a CPU-level accurate time count value in seconds. Since the starting point of the count is uncertain, the difference between continuous counts is generally meaningful
>>>start = time.perf_counter()
>>>stop = time.perf_counter()
> >>stop-start
22.563765376384787

Guess you like

Origin blog.csdn.net/zzh2910/article/details/80072220