class - time

 

 

 


import:

import time

sleep:

time.sleep(0.1)  # Time in seconds.

time:

time.time()      # Return the time in seconds since the epoch

                                 # Result: 1463623486.037193

localtime: (epoch -> truple)

>>> time.localtime()                  # 另有 time.gmtime()

time.struct_time(tm_year=2016, tm_mon=5, tm_mday=19, tm_hour=10, tm_min=5, tm_sec=7, tm_wday=3, tm_yday=140, tm_isdst=0)

P.S.

# TypeError: structseq index must be integer

myt = time.localtime()

myt[0]

2016

格式化輸出 (strftime):

# Convert a tuple or struct_time representing a time as returned by gmtime() or localtime()

# to a string as specified by the format

# time.strftime(format[, t])

Usage:

>>> time.strftime("%Y-%m-%d %H:%M:%S")

'2013-12-13 17:50:58'

>>> time.strftime("%H %M %S", t1)

'20 00 1900 01 20'

asctime

>>> time.asctime()

'Wed Oct 12 17:55:20 2016'

>>> time.asctime( time.localtime(time.time()) )

'Wed Oct 12 17:47:56 2016'

 

格式化輸入 (strptime):

# Parses str according to format string fmt and returns the instant in time-tuple format.
# time.strptime(str[, format])

t1 = time.strptime("20:00:00", '%H:%M:%S')

 

time

 

  • sleep(sec)
  • time()
  • time()
  • time.localtime()
  • strptime() -- parse string to time tuple

time() -- return 現在的時間 - Epoch

1354464149.0836041

ctime() -- convert time in seconds to string

'Mon Dec  3 00:03:24 2012'

localtime() -- convert seconds since Epoch to local time tuple

time.struct_time(tm_year=2012, tm_mon=12, tm_mday=3, tm_hour=0, tm_min=6, tm_sec=7, tm_wday=0, tm_yday=338, tm_isdst=0)

strptime() -- parse string to time tuple