python入門(七)time庫

2021-08-23 14:23:56 字數 1147 閱讀 9036

time():獲取計算機內部的時間值,是乙個浮點數,意義是從2023年1月1日到當前為止的時間。

ctime():獲取該時刻的年月日,星期幾,幾時幾分幾秒,是乙個字串。

gmtime():獲取計算機可處理的時間格式。返回乙個time.struct_time(tm_year,tm_mon,tm_day,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)

strftime(tpl,ts):tpl是格式化模板字串,用來定義輸出效果;ts是計算機內部時間型別變數。

eg:t=time.gmtime()

time.strftime("%y-%m-%d %h:%m:%s",t)

>>'2018-01-26 12:55:20'

時間格式化:

%y      年份        0000-9999

%m     月份        01-12

%b      月份名稱  january-december

%b      月份名稱縮寫    jan-dec

%d     日期        01-31

%a     星期        monday-sunday

%a     星期縮寫  mon-sun

%h     小時(24h)00-23

%h      小時(12h)01-12

%p      上/下午    am/pm

%m     分鐘        00-59

%s      秒           00-59

strptime(str,tpl):str是字串形式的時間值;tpl是格式化模板字串,用來定義輸入效果。

eg:timestr='2018-01-26 04:11:16'

time.strptime(timestr,'%y-%m-%d %h:%m:%s')

>>time.struct_time(tm_year=2018,tm_mon=1,tm_day=26,tm_hour=4,tm_min=11,tm_sec=16,tm_wday=4,tm_yday=26,tm_isdst=0)

測量程式從開始到結束所用時間

eg:def wait():

time.sleep(3.3)

>>wait() #程式等待3.3秒繼續執行。

Python學習筆記 七 (time庫的使用)

time庫是python處理時間的標準庫,在了解程式執行程度時有很大幫助,下面我對我所學習的time庫進行簡單總結,希望能對你我有益。一 time庫的介紹 time庫是python中處理時間的標準庫,常用於時間的獲取輸出以及提供系統級時間來了解程式效能。二 time庫操作函式 time庫操作函式大致...

Python常用標準庫 time

這個time庫提供了各種操作時間值。方法 描述示例 time.asctime tuple 將乙個時間元組轉換成乙個可讀的24個時間字串 time.asctime time.localtime sat nov 12 01 19 00 2016 time.ctime seconds 字串型別返回當前時間...

python標準庫模組 time

標準庫模組 time 時間 import time 1.獲取當前時間戳 從1970年1月1日到現在經過的秒數 print time.time 1571724503.9006603 2.獲取當前時間元組 年,月,日,時,分,秒,星期,一年的第幾天,與夏令時的偏移量 time.struct time t...