• 技术文章 >Python技术 >Python基础教程

    time.localtime在python中的使用

    小妮浅浅小妮浅浅2021-04-24 10:01:21原创4405

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    1、说明

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

    2、语法

    1

    time.localtime([ sec ])

    3、参数

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

    4、返回值

    该函数没有任何返回值。

    5、实例

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    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中的使用,对于这种特定的时间函数来说,其转换的方法也是固定的。大家学会后可以就代码的部分进行练习。

    专题推荐:python timelocaltime
    上一篇:python time.clock()的出错解决 下一篇:python字典获取对应键的方法

    相关文章推荐

    • python中datetime的基本介绍• datetime在python中获取时间并格式化• python中datetime转时间戳• python timedelta函数是什么?• python中time.clock()的使用• python time.clock()的出错解决

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网