新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:time.localtime在python中的使用
本文教程操作環(huán)境:windows7系統(tǒng)、python 3.9.1,DELL G3電腦。

1、說(shuō)明
將一個(gè)時(shí)間戳轉(zhuǎn)換為當(dāng)前時(shí)區(qū)的struct_time。secs參數(shù)未提供,則以當(dāng)前時(shí)間為準(zhǔn)。
2、語(yǔ)法
time.localtime([ sec ])
3、參數(shù)
sec -- 轉(zhuǎn)換為time.struct_time類(lèi)型的對(duì)象的秒數(shù)。
4、返回值
該函數(shù)沒(méi)有任何返回值。
5、實(shí)例
import time
import re
print time.time()
print time.localtime()
a = 'tm_year=2016, tm_mon=5, tm_mday=28, tm_hour=6, tm_min=51, tm_sec=14, tm_wday=5, tm_yday=149, tm_isdst=0'
b = re.findall('\d+', a)
print b
c = []
for tup in b:
t = int(tup)
c.append(t)
c = tuple(c)
print type(c)
print c
# c = (2016, 5, 28, 6, 51, 14, 5, 149, 0)
print time.mktime(c)
print time.mktime(time.localtime(1464389474.0)) # 元組轉(zhuǎn)化為時(shí)間戳,struct_time --->時(shí)間戳
print time.localtime(1464389474.0)
print time.gmtime(1464389474.0) # 時(shí)間戳轉(zhuǎn)化為元組, 時(shí)間戳--->struct_time
print time.strftime('%Y-%m-%d %H:%M:%S:%a:%b:%c:%j %p %U %w %W %x', time.localtime(1464389474.0))
print time.strftime('%Y-%m-%d %H:%M:%S') # struct_time --->格式化字符串
print time.strptime('2016-05-28 06:51:14', '%Y-%m-%d %H:%M:%S') #格式化字符串 --->struct time
#元組和字符串無(wú)法直接相互轉(zhuǎn)化
#datetime時(shí)間的計(jì)算是python中比較重要的模塊,跟我們的實(shí)際生活結(jié)合也比較緊密。本篇要帶來(lái)的是一種轉(zhuǎn)換成本地時(shí)間的方法,名稱(chēng)對(duì)應(yīng)為time.localtime,相信大家現(xiàn)在已經(jīng)有所了解了。
以上就是time.localtime在python中的使用,對(duì)于這種特定的時(shí)間函數(shù)來(lái)說(shuō),其轉(zhuǎn)換的方法也是固定的。大家學(xué)會(huì)后可以就代碼的部分進(jìn)行練習(xí)。
分享名稱(chēng):創(chuàng)新互聯(lián)Python教程:time.localtime在python中的使用
分享地址:http://www.5511xx.com/article/cdddjhp.html


咨詢(xún)
建站咨詢(xún)
