python基礎第二十二章----------時間模塊:time

時間模塊:time

查看時間戳:在百度上搜索:時間戳轉換工具函數

導入包:import time工具

time();獲取本地時間戳  ****spa

# res=time.time()
# print(res)
會輸出時間戳:
# 1564486575

ctime(時間戳):獲取本地時間的字符串(參數若是不寫,默認是當前)orm

# res=time.ctime(1564486575)
# print(res)
會輸出:
# Tue Jul 30 19:36:15 2019

localtime(時間戳):獲取本地時間元組(參數若是不寫,默認是當前)
# res=time.localtime(1564486575)
# (相似於dict中的keys(),values(),items()函數的返回效果)
# print(res)
#轉換成能看的懂的元組
# print(tuple(res))
# time.struct_time(tm_year=2019, tm_mon=7, tm_mday=30, tm_hour=19, tm_min=36, tm_sec=15, tm_wday=1, tm_yday=211, tm_isdst=0)
# (2019, 7, 30, 19, 36, 15, 1, 211, 0)

mktime(時間元組):經過時間元組獲取時間戳
# res=time.mktime((2019,6,18,8,8,8,4,66,0))
# print(res)
會輸出:
# 1560816488

asctime(時間元組):經過時間元組獲取時間字符串
# res=time.asctime((2019,6,18,8,8,8,4,66,0))
# print(res)
會輸出:
# Fri Jun 18 08:08:08 2019

sleep(秒數):休眠幾秒(在爬蟲中經常使用)****
# print(1)
# time.sleep(5)
# print(2)

# strftime() 格式化時間字符串(格式化字符串,時間元祖) *
# res=time.strftime('{}%Y{}%m{}%d{}%H{}%M{}%S{}',(2019,7,30,21,51,9,0,0,0)).format('如今是','年','月','日','時','分','秒')
# print(res)

#strptime() 將時間字符串經過指定格式提取到時間元組中(時間字符串,格式化字符串) *
# res=time.strptime('2019 7 30 21 48 56','%Y %m %d %H %M %S')
# print(res)
# time.struct_time(tm_year=2019, tm_mon=7, tm_mday=30, tm_hour=21, tm_min=48, tm_sec=56, tm_wday=1, tm_yday=211, tm_isdst=-1)

per_counter() 用於計算程序運行時間
start_time=time.perf_counter()time.sleep(3)end_time=time.perf_counter()print('程序運行時間爲{}'.format(end_time-start_time))會輸出:程序運行時間爲2.999698792
相關文章
相關標籤/搜索