• 【python基础】时间模块的time的下面的方法使用解析



    一、time三种表现形式

    • 时间戳(timestamp)表现方式:1970年1月1日之后的秒。

    举例:

    time.time()

    在这里插入图片描述

    • 时间元组(struct_itme)表现方式

    注意: 时间元组(struct_time)的格式是:

    t=(tm_year,tm_mon,tm_mday,tm_hour,tm_min,tm_sec,tm_wday,tm_yday,tm_isdst)
    
    • 1

    参数解析

    属性说明
    tm_year(例如:2023年)
    tm_mon范围 [1,12]
    tm_mday范围 [1,31]
    tm_hour范围 [0,23]
    tm_min范围 [0,59]
    tm_sec范围 [0,61]
    tm_wday范围 [0,6] ,星期一为 0
    tm_yday范围 [1,366]
    tm_isdst0、1或者-1

    举例:

    time.strptime()

    在这里插入图片描述

    • 时间字符串表现方式

    举例:默认时间字符串(defaut_time)表现方式

    time.asctime()
    默认以 %a %b %d %H:%M:%S %Y 形式展示

    在这里插入图片描述

    举例:自定义时间字符串(format_time)表现方式

    time.strftime(‘%b %d %Y %H:%M:%S’,time.gmtime(time.time()))

    在这里插入图片描述


    二、time模块常用的方法

    1.时间戳(timestamp)表现方式

    time.time()

    返回当时时间戳,值为按秒计算的浮点数。
    表示从1970年1月1日0点0分开始,到当前时间,一共经历了多少秒

    import time
    seconds=time.time()
    print("Seconds since epoch =",seconds)
    
    # 输出:Seconds since epoch = 1699325566.1240547
    
    • 1
    • 2
    • 3
    • 4
    • 5

    time.mktime()

    返回时间戳,值为按秒计算的浮点数。
    将struct_time(或包含9个元素的元组对应于struct_time)作为参数,它是localtime()反函数。

    import time
    t=(2023,11,7,11,16,31,1,311,0)
    print("local time :",time.mktime(t))
    
    # 输出:local time : 1699326991.0
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.时间元组(struct_itme)表现方式

    time.localtime()

    返回时间元组格式(struct_time)。
    以从1970年1月1日0点0分开始的秒为参数,格式化时间戳为本地的时间,年月日,时分秒等信息。若未输入参数,默认当前时间。

    import time
    
    result=time.localtime()
    print("result:",result)
    print("year:",result.tm_year)
    print("hour:",result.tm_hour)
    
    # 输出:
    # result: time.struct_time(tm_year=2023, tm_mon=11, tm_mday=7, tm_hour=15, tm_min=5, tm_sec=54, tm_wday=1, tm_yday=311, tm_isdst=0)
    # year: 2023
    # hour: 15
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    time.gmtime()

    返回时间元组格式(struct_time)。
    以从1970年1月1日0点0分开始的秒为参数,格式化时间戳为本地的时间,年月日,时分秒等信息。

    import time
    
    result1=time.gmtime(1699325566.1240547)
    print("result:",result)
    print("year:",result.tm_year)
    print("hour:",result.tm_hour)
    
    # 输出:
    # result: time.struct_time(tm_year=2023, tm_mon=11, tm_mday=7, tm_hour=15, tm_min=5, tm_sec=54, tm_wday=1, tm_yday=311, tm_isdst=0)
    # year: 2023
    # hour: 15
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    time.strptime(str,format)

    返回时间元组格式(struct_time)。
    根据format格式将str一个时间字符解析为时间元组。

    import time
    
    struct_time=time.strptime('7 Nov 23',"%d %b %y")
    print("返回元组:",struct_time)
    
    #输出:返回元组: time.struct_time(tm_year=2023, tm_mon=11, tm_mday=7, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=311, tm_isdst=-1)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.默认时间字符串(defaut_time)表现方式

    time.asctime()

    返回默认时间字符串的形式。
    将struct_time(或包含9个元素的元组对应于struct_time)作为参数。若未输入参数,默认当前时间。

    import time
    
    t=(2023,11,7,11,16,31,1,311,0)
    print(time.asctime())
    print(time.asctime(t))
    
    # 输出:'Tue Nov  7 11:16:31 2023'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    time.ctime()

    返回一个表示本地时间的字符串。
    以从1970年1月1日0点0分开始的秒为参数,把时间戳转化为time.asctime()形式

    import time
    
    local_time=time.ctime(1699325566.1240547)
    print('local time:',local_time)
    
    # 输出:local time: Tue Nov  7 10:52:46 2023
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.自定义时间字符串(format_time)表现方式

    time.strftime(format,t)

    返回自定义字符串所表示的当地时间,格式由format决定。
    参数 t 表示 struct_time(或包含9个元素的元组对应于struct_time)。

    import time
    
    time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    
    # 输出:'2023-11-07 15:29:21'
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5.其他

    time.sleep()

    功能:在给定的秒数内暂停(延迟)当前线程的执行。

    import time
    print("hello")
    time.sleep(4)
    print("4秒后:","world")
    
    • 1
    • 2
    • 3
    • 4

    三、时间输出格式转换

    1.时间戳转为字符串

    import time
    current_time=time.time()
    print(current_time)
    print(type(current_time))
    
    # 输出:
    # 1699347750.380224
    # 
    
    str_time=time.ctime(current_time)
    print(str_time)
    print(type(str_time))
    
    # 输出:
    # Tue Nov  7 17:02:30 2023
    # 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2.时间戳转为时间元组

    import time
    current_time=time.time()
    print(current_time)
    print(type(current_time))
    
    # 输出:
    # 1699347750.380224
    # 
    
    tuple_time=time.local_time(current_time)
    print(tuple_time)
    print(type(tuple_time))
    
    # 输出:
    # time.struct_time(tm_year=2023, tm_mon=11, tm_mday=7, tm_hour=17, tm_min=4, tm_sec=25, tm_wday=1, tm_yday=311, tm_isdst=0)
    # 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.字符串转为时间元组

    import time
    
    str_time=time.asctime()
    print(str_time)
    print(type(str_time))
    
    # 输出:
    # Tue Nov  7 17:14:29 2023
    # 
    
    struct_time=time.strptime(str_time)
    print(struct_time)
    print(type(struct_time))
    
    # 输出:
    # time.struct_time(tm_year=2023, tm_mon=11, tm_mday=7, tm_hour=17, tm_min=14, tm_sec=29, tm_wday=1, tm_yday=311, tm_isdst=-1)
    # 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    4.字符串转换为时间戳

    import time
    
    str_time=time.asctime()
    print(str_time)
    print(type(str_time))
    
    # 输出:
    # Tue Nov  7 17:14:29 2023
    # 
    
    struct_time=time.strptime(str_time) #先转换为struct_time或者元组
    second_time=time.mktime(struct_time)
    print(second_time)
    print(type(second_time))
    
    # 输出:
    # 1699348690.0
    # 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    5.时间元组转为字符串

    import time
    struct_time=time.localtime()
    print(struct_time)
    print(type(struct_time))
    
    # 输出:
    # time.struct_time(tm_year=2023, tm_mon=11, tm_mday=7, tm_hour=17, tm_min=22, tm_sec=44, tm_wday=1, tm_yday=311, tm_isdst=0)
    # 
    
    print(time.strftime('%Y/%m/%d',struct_time))
    print(time.strftime('%T',struct_time))
    print(time.strftime('%F',struct_time))
    
    print(type(time.strftime('%Y/%m/%d',struct_time)))
    
    # 输出:
    # 2023/11/07
    # 17:22:44
    # 2023-11-07
    # 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    6.时间元组转换为时间戳(从新纪元后的秒数)

    import time
    tuple_time=time.localtime()
    print(tuple_time)
    print(type(tuple_time))
    
    # 输出:
    # time.struct_time(tm_year=2023, tm_mon=11, tm_mday=7, tm_hour=16, tm_min=57, tm_sec=44, tm_wday=1, tm_yday=311, tm_isdst=0)
    # 
    
    second_time=time.mktime(tuple_time)
    print(second_time)
    print(type(second_time))
    
    # 输出:
    # 1699347464.0
    # 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    附录:python中时间日期格式化符号

    符号说明
    %y两位数的年份表示(00-99)
    %Y四位数的年份表示(0000-9999)
    %m月份(01-12)
    %d月内中的一天(0-31)
    %H24小时制小时数(01-23)
    %I12小时制小时数(01-12)
    %M分钟数(00-59)
    %S秒(00-59)
    %a本地简化星期名称(示例:Sat)
    %A本地完整星期名称(示例:Saturday)
    %b本地简化的月份名称(示例:Aug)
    %B本地完整的月份名称(示例:August)
    %c本地相应的日期表示 和时间表示(示例:Sat Aug 17 15:55:44 2019)
    %j年内的一天(001-366)
    %p本地A.M或P.M的等价符
    %U一年中的星期数(00-53)星期天为星期的开始
    %w星期(0-6),星期天为星期的开始
    %W一年中的星期数(00-53)星期一为星期的开始
    %x本地相应的日期表示(示例:11/07/2023)
    %X本地相应的时间表示(示例:15:54:52)
    %Z当前时区的名称
    %%%号本身
  • 相关阅读:
    AlexNet重点介绍和源码测试
    npm install 报错解决记录
    Rust 围炉札记
    2022年最新海南机动车签字授权人模拟考试及答案
    【计算机视觉】相机模型&立体视觉
    使用VSCode进行Python模块调试
    20230918使用ffmpeg将mka的音频转为AAC编码以便PR2023来识别
    Java中Map.keySet()方法具有什么功能呢?
    神经网络常见评价指标AUROC(AUC-ROC)、AUPR(AUC-PR)
    实验(六):定时器实验
  • 原文地址:https://blog.csdn.net/sodaloveer/article/details/134262015