time 時間模組

2022-06-30 11:15:10 字數 2285 閱讀 4523

# ### time 時間模組

import time

#time() 獲取本地時間戳

res = time.time()

print(res)

#mktime() 通過[時間元組]獲取[時間戳] (引數是時間元組)

ttp = (2019,5,16,10,55,30,0,0,0)

res = time.mktime(ttp)

print(res)

#localtime() 通過[時間戳]獲取[時間元組] (預設當前時間)

res = time.localtime()

print(res)

res = time.localtime(1557975330)

print(res)

'''time.struct_time

(tm_year=2019,

tm_mon=5,

tm_mday=16,

tm_hour=10,

tm_min=57,

tm_sec=10,

tm_wday=3,

tm_yday=136,

tm_isdst=0

)'''

#ctime() 通過[時間戳]獲取[時間字串] (預設當前時間)

res = time.ctime()

print(res)

res = time.ctime(1557975330)

print(res)

#asctime() 通過[時間元組]獲取[時間字串](引數是時間元組)

ttp = (2019,5,1,3,3,3,2,0,0)

res = time.asctime(ttp)

print(res)

# asctime 內部實現有個小bug,不能夠自動識別是週幾,只能手動填寫

# 優化寫法

ttp = (2019,5,1,3,3,3,0,0,0)

res = time.mktime(ttp)

print(res)

res = time.ctime(res)

print(res)

#strftime() 通過[時間元組]格式化[時間字串] (格式化字串,[可選時間元組引數])

res = time.strftime("%y=%m=%d %h|%m|%s")

print(res)

ttp = (2019,5,1,3,3,3,0,0,0)

res = time.strftime("%y=%m=%d %h|%m|%s",ttp)

print(res)

# 注意點 windows 不支援strftime的中文字元 linux 完全可以

# res = time.strftime("%y-%m-%d %h:%m:%s 今天是段奕巨集的生日")

# print(res)

#strptime() 通過[時間字串]提取出[時間元組] (時間字串,格式化字串)

# 注意點:strptime 在匹配字串當中的時間時,字串必須嚴絲合縫,不能隨意修改原有字元.

res = time.strptime("2023年5月17號,5期同學會看著4期同學在早上8點9分20秒的時候去下野","%y年%m月%d號,5期同學會看著4期同學在早上%h點%m分%s秒的時候去下野")

print(res)

'''time.struct_time(

tm_year=2019,

tm_mon=5,

tm_mday=17,

tm_hour=8,

tm_min=9,

tm_sec=20,

tm_wday=4,

tm_yday=137,

tm_isdst=-1)

'''#sleep() 程式睡眠等待

# time.sleep(10) # 程式在此加阻塞,10秒之後載向下執行

# print("我睡醒了")

#perf_counter() 用於計算程式執行的時間

# 用time.time() 一樣可以實現

startime = time.perf_counter()

print(startime)

for i in range(1000000000):

pass

endtime = time.perf_counter()

res = endtime - startime

print(res)

時間模組 time模組

1.時間戳 time time 從1970 至今過了多少秒 時間戳 格式化時間 時間物件 print time.time 2.格式時間 time.strftime y m d h m s str formate time 格式為字串 y year with century as a decimal ...

time時間模組

一,時間戳 計算時間 import time print time.time 二,結構化時間,計算當下的時間 import time print time.localtime 結構化時間utc 將結構化時間轉化為時間戳 print time.mktime time.localtime 將結構化時間轉...

模組 time(時間)

import time import random print time.time 從1970 01 01到現在過了多少秒。time.time應用講解 先獲取啟動時間在獲取結束時間,結束時間減去啟動時間 執行時間 start time.time 記錄啟動時間 time.sleep random.ra...