Python學習--time

time time()方法

Python time time() 返回當前時間的時間戳(1970紀元後經過的浮點秒數)。

time()方法語法:

time.time()
print(time.time()) #返回當前系統時間戳

time() 函數的使用方法:

#!/usr/bin/python
import time

print "time.time(): %f " %  time.time()


time ctime()方法

Python time ctime() 函數把一個時間戳(按秒計算的浮點數)轉化爲time.asctime()的形式。 如果參數未給或者爲None的時候,將會默認time.time()爲參數。它的作用相當於 asctime(localtime(secs))。

ctime()方法語法:

time.ctime([ sec ])
print(time.ctime()) #輸出Tue Jan 26 18:23:48 2016 ,當前系統時間
print(time.ctime(time.time()-86640)) #將時間戳轉爲字符串格式

參數

sec -- 要轉換爲字符串時間的秒數。

import time

print "time.ctime() : %s" % time.ctime()

time.ctime() : Mon JUN 18 10:00:18 2016



time gmtime()方法

Python time gmtime() 函數將一個時間戳轉換爲UTC時區(0時區)的struct_time,可選的參數sec表示從1970-1-1以來的秒數。其默認值爲 time.time(),函數返回time.struct_time類型的對象。(struct_time是在time模塊中定義的表示時間的對象)。

gmtime()方法語法:

time.gmtime([ sec ])
print(time.gmtime(time.time()-86640)) #將時間戳轉換成struct_time格式

參數

sec -- 轉換爲time.struct_time類型的對象的秒數。

import time

print "time.gmtime() : %s" % time.gmtime()

time.gmtime() : time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=2, tm_min=55, tm_sec=45, tm_wday=3, tm_yday=98, tm_isdst=0)


time localtime()方法

Python time localtime() 函數類似gmtime(),作用是格式化時間戳爲本地的時間。 如果sec參數未輸入,則以當前時間爲轉換標準。 DST (Daylight Savings Time) flag (-1, 0 or 1) 是否是夏令時。

localtime()方法語法:

time.localtime([ sec ])
print(time.localtime(time.time()-86640)) #將時間戳轉換成struct_time格式,但返回 的本地時間

參數

sec -- 轉換爲time.struct_time類型的對象的秒數。

import time

print "time.localtime() : %s" % time.localtime()

time.localtime() : (2009, 2, 17, 17, 3, 38, 1, 48, 0)



time mktime()方法

Python time mktime() 函數執行與gmtime(), localtime()相反的操作,它接收struct_time對象作爲參數,返回用秒數來表示時間的浮點數。

mktime()方法語法:

time.mktime(t)
print(time.mktime(time.localtime())) #與time.localtime()功能相反,將struct_time格式轉回成時間戳格式

參數

t -- 結構化的時間或者完整的9位元組元素。

import time

t = (2009, 2, 17, 17, 3, 38, 1, 48, 0)secs = time.mktime( t )print "time.mktime(t) : %f" %  secs


time.mktime(t) : 1234915418.000000


time sleep()方法

Python time sleep() 函數推遲調用線程的運行,可通過參數secs指秒數,表示進程掛起的時間。

sleep()方法語法:

time.sleep(t)
time.sleep(4) #sleep

參數

t -- 推遲執行的秒數。


time strftime()方法

Python time strftime() 函數接收以時間元組,並返回以可讀字符串表示的當地時間,格式由參數format決定。

strftime()方法語法:

time.strftime(format[, t])
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #將struct_time格式轉成指定的字符串格式

參數

format -- 格式字符串。

t -- 可選的參數t是一個struct_time對象。

說明

python中時間日期格式化符號:

%y 兩位數的年份表示(00-99)

%Y 四位數的年份表示(000-9999)

%m 月份(01-12)

%d 月內中的一天(0-31)

%H 24小時制小時數(0-23)

%I 12小時制小時數(01-12)

%M 分鐘數(00=59)

%S 秒(00-59)

%a 本地簡化星期名稱

%A 本地完整星期名稱

%b 本地簡化的月份名稱

%B 本地完整的月份名稱

%c 本地相應的日期表示和時間表示

%j 年內的一天(001-366)

%p 本地A.M.或P.M.的等價符

%U 一年中的星期數(00-53)星期天爲星期的開始

%w 星期(0-6),星期天爲星期的開始

%W 一年中的星期數(00-53)星期一爲星期的開始

%x 本地相應的日期表示

%X 本地相應的時間表示

%Z 當前時區的名稱

%% %號本身

import time

t = (2009, 2, 17, 17, 3, 38, 1, 48, 0)
t = time.mktime(t)
print time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t))

Feb 18 2009 00:03:38


time strptime()方法

Python time strptime() 函數根據指定的格式把一個時間字符串解析爲時間元組。

strptime()方法語法:

time.strptime(string[, format])
print(time.strptime("2016-01-28","%Y-%m-%d") ) #將字符串格式轉換成struct_time格式

參數

string -- 時間字符串。

format -- 格式化字符串。

import time

struct_time = time.strptime("30 Nov 00", "%d %b %y")
print "returned tuple: %s " % struct_time

returned tuple: (2000, 11, 30, 0, 0, 0, 3, 335, -1)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章