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

1、說明
將一個時間戳轉(zhuǎn)換為當前時區(qū)的struct_time。secs參數(shù)未提供,則以當前時間為準。
2、語法
time.localtime([ sec ])
3、參數(shù)
sec -- 轉(zhuǎn)換為time.struct_time類型的對象的秒數(shù)。
4、返回值
該函數(shù)沒有任何返回值。
5、實例
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)化為時間戳,struct_time --->時間戳
print time.localtime(1464389474.0)
print time.gmtime(1464389474.0) # 時間戳轉(zhuǎn)化為元組, 時間戳--->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
#元組和字符串無法直接相互轉(zhuǎn)化
#datetime時間的計算是python中比較重要的模塊,跟我們的實際生活結合也比較緊密。本篇要帶來的是一種轉(zhuǎn)換成本地時間的方法,名稱對應為time.localtime,相信大家現(xiàn)在已經(jīng)有所了解了。
以上就是time.localtime在python中的使用,對于這種特定的時間函數(shù)來說,其轉(zhuǎn)換的方法也是固定的。大家學會后可以就代碼的部分進行練習。
當前題目:創(chuàng)新互聯(lián)Python教程:time.localtime在python中的使用
轉(zhuǎn)載注明:http://www.dlmjj.cn/article/cdddjhp.html


咨詢
建站咨詢
