1. 程式人生 > >python中的時間模組(time)

python中的時間模組(time)

可以從原始碼中看到time有如下的方法:

Functions:
time() -- return current time in seconds since the Epoch as a float
clock() -- return CPU time since process start as a float
sleep() -- delay for a number of seconds given as a float
gmtime() -- convert seconds since Epoch to UTC tuple
localtime() -- convert seconds since Epoch to local time tuple
asctime() -- convert time tuple to string
ctime() -- convert time in seconds to string
mktime() -- convert local time tuple to seconds since Epoch
strftime() -- convert time tuple to string according to format specification
strptime() -- parse string to time tuple according to format specification
tzset() -- change the local timezone

以下是實際應用:

import time

#print(time.altzone)  # 返回格林威治西部的夏令時地區的偏移數
#print(time.asctime()) # 接受一個時間元組,並返回一個格式化之後的時間---時間字串
#print(time.asctime(time.localtime()))

# print(time.clock()) # 第一次呼叫表示程序時間,第三次呼叫,表示第三次到第二次程序的時間
# time.sleep(1) #阻塞當前執行緒n秒
# print(time.clock())
# time.sleep(1)
# print(time.clock())
# time.sleep(2)
# print(time.clock())
#
# print(time.ctime()) # 返回當前時間的字串
# print(time.time()) # 返回當前時間的時間戳---從1970年1月1日0點0分0秒到現在所經歷的秒數
# print(time.ctime(time.time())) # ctime() 接受一個時間戳,轉換成對應的時間字串
#
# print(time.gmtime()) # 返回一個時間元組,時間為夏令時
# print(time.localtime()) # 返回一個時間元組,時間為正常時間
#
# print(time.mktime(time.localtime()))  # 接受時間元組,轉換成時間戳
# print(time.strftime('%Y*%m*%d %H:%M:%S',time.localtime())) # 接受一個時間元組,按照指定的格式返回時間字串
# print(time.strptime('2018-6-28 16:0:0','%Y-%m-%d %H:%M:%S')) # 接受一個含有時間資訊的字串,通過指定格式進行解析,並返回對應的時間元組