时间的计算是python中比较重要的模块,跟我们的实际生活结合也比较紧密。常见的时间转换,想必大家都有所学习,那么本篇要带来的是一种转换成本地时间的方法,名称对应为time.localtime。相信很多人还没有接触过,下面我们就time.localtime的函数说明、语法、参数、返回值、实例带来介绍。
1.说明
将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
2.语法time.localtime([ sec ])
3.参数
sec -- 转换为time.struct_time类型的对象的秒数。
4.返回值
该函数没有任何返回值。
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)) # 元组转化为时间戳,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
以上就是time.localtime在python中的使用,对于这种特定的时间函数来说,其转换的方法也是固定的。大家学会后可以就代码的部分进行练习。