time.localtime在python中的使用

462次阅读
没有评论

time.localtime在python中的使用

time.localtime在python中的使用

time.localtime说明

将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。

time.localtime语法

time.localtime([ sec ])

time.localtime参数

sec — 转换为time.struct_time类型的对象的秒数。

time.localtime返回值

该函数没有任何返回值。

time.localtime实例

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)) # 元组转化为时间戳,struct_time --->时间戳
 
print time.localtime(1464389474.0)
 
print time.gmtime(1464389474.0) # 时间戳转化为元组, 时间戳--->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
 
#元组和字符串无法直接相互转化
 
#datetime

时间的计算是python中比较重要的模块,跟我们的实际生活结合也比较紧密。本篇要带来的是一种转换成本地时间的方法,名称对应为time.localtime,相信大家现在已经有所了解了。

以上就是time.localtime在python中的使用,对于这种特定的时间函数来说,其转换的方法也是固定的。大家学会后可以就代码的部分进行练习。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-05-09发表,共计1187字。
新手QQ群:570568346,欢迎进群讨论 Python51学习