Python time時間模組 學習猿地

2021-10-04 02:11:10 字數 1415 閱讀 1143

### time 時間模組

```python

import time

概念:1。 時間戳: 1574905882.6581771 表示從2023年1月1日0時0分0秒到現在的乙個秒數,目前可以計算到2023年

2。 時間字串: thu nov 28 09:54:08 2019  

3。 時間元組: time.struct_time(tm_year=2019, tm_mon=11, tm_mday=28, tm_hour=9, tm_min=55, tm_sec=32, tm_wday=3, tm_yday=332, tm_isdst=0)

# *** 1. 獲取當前系統的時間戳

res = time.time()

# 2. 獲取當前系統時間,時間字串

res = time.ctime()

# 3. 獲取當前系統時間, 時間元組

res = time.localtime()

# 4. 以上時間字串和時間元組可以通過指定的時間戳來獲取

t = 1564000082.6581771

res = time.ctime(t)

res = time.localtime()

# 5. 使用localtime方法獲取的時間元組,如何格式化成為 ***x年xx月xx日 時:分:秒 星期幾

# print(f'年月日 :: 星期')

# *** 6. strftime() 格式化時間 年-月-日 時:分:秒 星期幾

res = time.strftime('%y-%m-%d %h:%m:%s %w')

# *** 7。 sleep(秒) 在給定的秒數內暫停呼叫執行緒的執行。該引數可以是浮點數,以指示更精確的睡眠時間。

# print(time.strftime('%y-%m-%d %h:%m:%s %w'))

# time.sleep(3)

# print(time.strftime('%y-%m-%d %h:%m:%s %w'))

# 、計算程式的執行時間

# time.perf_counter()

# 100萬次的字串比較 需要執行的時間

start = time.perf_counter()

for i in range(1000000):

if 'abc' > 'acd':

pass

end = time.perf_counter()

print(end-start) # 0.14171751

start = time.perf_counter()

for i in range(1000000):

if 103 > 100 :

pass

end = time.perf_counter()

print(end-start) # 0.164985942

python time時間模組

想讓程式停頓幾秒鐘 time.sleep 秒數 例如 print 1 time.sleep 2 print 2 結果,在列印了1後會停2秒,然後再列印2 三種時間格式 時間戳,是計算機可識別的乙個時間形態 格式化時間字串,是人類可識別的乙個時間形態 時間元組,則是二個時間的過渡體 可以把這三者間的關...

Python time 時間模組

time 模組提供各種時間相關的功能 在 python 中,與時間處理有關的模組包括 time,datetime 以及 calendar import time 該模組方法中包含三種時間的形式 時間戳 元組時間 字串時間 時間形式的轉換 t time.time 取當前時間的 時間戳 tt time....

python Time(時間)模組

要使用乙個模組,首先要把模組匯入進來 import time 我們先把這一篇文章需要用的模組匯入進來 首先說一下time模組,time模組中的函式 sleep 休眠指定的秒數 可以是小數 import time print 開始 time.sleep 5 print 結束 如果在pycharm中輸入...