日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python中time庫的使用(日期時間)_python

作者:emanlee ? 更新時間: 2023-04-02 編程語言

time庫是python中處理時間的標(biāo)準(zhǔn)庫。

計算機時間的表達(dá):time()、ctime()、gmtime();

提供獲取系統(tǒng)時間并格式化輸出功能: strftime()、strptime()

提供系統(tǒng)級精確計時功能可用于程序性能分析: sleep()、perf_counter()

時間獲取 time.time(): 獲取當(dāng)前時間戳,即計算機內(nèi)部時間值,浮點數(shù)

>>> import time
>>> time.time()
1655598609.7932513

time.ctime(): 獲取當(dāng)前時間并以一種易讀的形式表示,返回字符串

>>> import time
>>> time.ctime()
'Sun Jun 19 08:31:03 2022'

time.gmtime(): 獲取當(dāng)前時間,表示為計算機可以處理的時間格式

>>> import time
>>> time.gmtime()
time.struct_time(tm_year=2022, tm_mon=6, tm_mday=19, tm_hour=0, tm_min=31, tm_sec=56, tm_wday=6, tm_yday=170, tm_isdst=0)

時間格式化將計算機內(nèi)部表達(dá)的年月日時分秒等與時間有關(guān)的信息,用一種變量的形式合理組合并且合理輸出,通過控制表達(dá)輸出格式,展示模板由特定的格式化控制符組成。

time.strftime(tpl , ts) tpl是格式化模板字符串,用于定義輸出效果 ; ts是計算機內(nèi)部時間類型變量

時間格式化,指strftime的tpl部分

>>> import time
>>> time.strftime('%Y-%m-%d %H:%M:%S')
'2022-06-19 08:33:33

time.strptime(str,tpl) 反格式化-將時間字符串編程計算機內(nèi)部可以操作的時 str是字符串形式的時間值;tpl是格式化模板字符串,用來定義輸出效果。

>>> import time
>>> print(time.strptime("2020-12-15","%Y-%m-%d"))
time.struct_time(tm_year=2020, tm_mon=12, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=350, tm_isdst=-1)
>>> import time
>>> print(time.localtime())
time.struct_time(tm_year=2022, tm_mon=6, tm_mday=19, tm_hour=8, tm_min=39, tm_sec=36, tm_wday=6, tm_yday=170, tm_isdst=0)

程序計時應(yīng)用測量起止動作所經(jīng)歷時間的進(jìn)程。

測量時間:time.perf_counter()

#導(dǎo)入時間模塊
import time
#求素數(shù)的程序
def is_prime(number):
   for i in range(2, number):
      if number % i == 0:
         return False
         return True
if __name__ == '__main__':
   number = 17377
   start_time = time.perf_counter()
   is_prime(number)
   end_time = time.perf_counter()
#按差異打印執(zhí)行時間
print(end_time - start_time)

產(chǎn)生時間:time.sleep(s) s是擬休眠的時間,單位是秒,可以是浮點數(shù)。

REF

https://baijiahao.baidu.com/s?id=1728816399629285523&wfr=spider&for=pc

https://baijiahao.baidu.com/s?id=1685495648757727003&wfr=spider&for=pc

https://www.nhooo.com/note/qa0bhu.html

原文鏈接:https://www.cnblogs.com/emanlee/p/16389886.html

欄目分類
最近更新