• python时间模块time


    时间模块

      时间模块主要处理和时间相关的事件,我们可以通过模块获取不同数据类型的时间以便我们需求。

    表现时间的三种方式:

      在pythn中表现时间的方式主要有三种:时间戳(stamptime)、元祖时间(structtime)、格式化字符串时间,在大多数编程里面也有这些概念。

      1、时间戳时间:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。返回的是float类型

      2、格式化字符串时间:列如:‘1992-12-1’

      3、元祖时间:该元祖共有9个元素(年,月,日,时,分,秒,一年中第几周,一年中第几天等)

    时间戳时间

      在使用time模块前记得导入模块,下面来看下如何获取时间戳,语法:time.time().

    
    >>> import time
    >>> time.time()
    1541072964.2124543
    
    

      注意:time.time()不接受参数,返回是当前时间的时间戳时间,float型。

    字符串时间

      语法:time.strftime(format,p_tuple=None),该方法接受两个参数,一个是要显示时间的字符串格式,以及元祖时间,该参数为默认参数,不传则获取当前元祖时间来转化成字符串时间,以下是该方法的详细介绍:

    
    def strftime(format, p_tuple=None): # real signature unknown; restored from __doc__
        """
        strftime(format[, tuple]) -> string
        
        Convert a time tuple to a string according to a format specification.
        See the library reference manual for formatting codes. When the time tuple
        is not present, current time as returned by localtime() is used.
        
        Commonly used format codes:
        
        %Y  Year with century as a decimal number.
        %m  Month as a decimal number [01,12].
        %d  Day of the month as a decimal number [01,31].
        %H  Hour (24-hour clock) as a decimal number [00,23].
        %M  Minute as a decimal number [00,59].
        %S  Second as a decimal number [00,61].
        %z  Time zone offset from UTC.
        %a  Locale's abbreviated weekday name.
        %A  Locale's full weekday name.
        %b  Locale's abbreviated month name.
        %B  Locale's full month name.
        %c  Locale's appropriate date and time representation.
        %I  Hour (12-hour clock) as a decimal number [01,12].
        %p  Locale's equivalent of either AM or PM.
        
        Other codes may be available on your platform.  See documentation for
        the C library strftime function.
        """
        return ""
    

    字符串格式中文对照表:

    %y 两位数的年份表示(00-99)
    %Y 四位数的年份表示(000-9999)
    %m 月份(01-12)
    %d 月内中的一天(0-31)
    %H 24小时制小时数(0-23)
    %I 12小时制小时数(01-12)
    %M 分钟数(00=59)
    %S 秒(00-59)
    %a 本地简化星期名称
    %A 本地完整星期名称
    %b 本地简化的月份名称
    %B 本地完整的月份名称
    %c 本地相应的日期表示和时间表示
    %j 年内的一天(001-366)
    %p 本地A.M.或P.M.的等价符
    %U 一年中的星期数(00-53)星期天为星期的开始
    %w 星期(0-6),星期天为星期的开始
    %W 一年中的星期数(00-53)星期一为星期的开始
    %x 本地相应的日期表示
    %X 本地相应的时间表示
    %Z 当前时区的名称
    %% %号本身
    

    接下来看两个例子:

    >>> time.strftime("%Y-%m-%d %X")
    '2018-11-01 20:08:46'
    
    >>>time.strftime("%Y-%m-%d %H-%M-%S")
    '2017-07-24 13-55-04'
    

    时间元祖

    语法1:获取当前时区的时间time.localtime(secends=None),接受一个默认参数(stamptime)时间戳,没传参时默认获取当前的时间戳。
    方法介绍:

    def localtime(seconds=None): # real signature unknown; restored from __doc__
        """
        localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
                                  tm_sec,tm_wday,tm_yday,tm_isdst)
        
        Convert seconds since the Epoch to a time tuple expressing local time.
        When 'seconds' is not passed in, convert the current time instead.
        """
        pass
    
    

    语法2:获取英国伦敦时间(时间戳是伦敦时间开始time.gmtime(seconds=None)
    用法与上面一样。
    举例:

    >>> time.localtime()
    time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=20, tm_min=18, tm_sec=5, tm_wday=3, tm_yday=305, tm_is
    dst=0)
    >>> time.gmtime()
    time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=12, tm_min=18, tm_sec=13, tm_wday=3, tm_yday=305, tm_i
    sdst=0)
    
    
    

    可以看出北京时间比伦敦时间早8小时。
    小结:时间戳是计算机能够识别的时间;时间字符串是人能够看懂的时间;元组则是用来操作时间的

    几种时间格式之间的转换

    1、时间戳<--->元祖时间

    时间戳--->元祖时间

    语法1:time.localtime(seconds=None),接受时间戳时间参数转化成当地元祖时间。不传参默认获取当前时间戳时间。

    #不传参数
    >>> time.localtime()
    time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=20, tm_min=24, tm_sec=34, tm_wday=3, tm_yday=305, tm_i
    sdst=0)
    
    #自定义传时间戳参数
    >>> time.localtime(1231231411)
    time.struct_time(tm_year=2009, tm_mon=1, tm_mday=6, tm_hour=16, tm_min=43, tm_sec=31, tm_wday=1, tm_yday=6, tm_isds
    t=0)
    
    

    time.gmtime()用法与time.localtime()用法相同.

    元祖时间--->时间戳时间

    语法:time.mktime(p_tuple),参数为元祖时间必须传参.

    #先获取一个元祖时间
    >>> struct_time =time.localtime()
    #查看元祖时间
    >>> struct_time
    time.struct_time(tm_year=2018, tm_mon=11, tm_mday=1, tm_hour=20, tm_min=30, tm_sec=6, tm_wday=3, tm_yday=305, tm_is
    dst=0)
    
    #将元祖时间转化成时间戳时间
    >>> stamp_time = time.mktime(struct_time)
    >>> stamp_time
    1541075406.0
    

    2字符串时间<--->元祖时间

    2.1字符串--->元祖时间

    语法:strptime(string, format) ,接受两个参数,字符串时间以及时间字符串格式

    >>> time.strptime('2007-10-10', '%Y-%m-%d')
    time.struct_time(tm_year=2007, tm_mon=10, tm_mday=10, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=283, tm_isd
    st=-1)
    

    2.2、元祖时间--->字符串时间

    语法:time.strftime(format,p_tuple),接受两个参数,字符串时间格式及元祖时间,元祖时间不传默认为当前时间的元祖时间

    #只传字符串格式参数
    >>> time.strftime('%c')
    'Thu Nov  1 20:42:35 2018'
    
    #传入指定的元祖时间
    #先获取一个元祖时间
    >>> struct_time = time.localtime()
    
    #传入两个参数获取字符串时间
    >>> time.strftime('%c',struct_time)
    'Thu Nov  1 20:44:23 2018'
    

    总结:

    获取时间戳时间方法:

    1、time.time()直接获取
    2、time.mktime(p_tuple)元祖时间转化时间戳
    

    获取元祖时间方法:

    1、time.localtime(seconds=None)、time.gmtime(seconds=None)时间戳转化元祖时间
    2、time.strptime(strtime,format)字符串时间转化成元祖时间
    

    获取字符串时间方法:

    1、time.strftime(format,p_tuple=None)元祖转化成字符串时间。
    

    小练习题:

    要求

    输入出生时间和当前时间,计算出过了多长时间。

    import time
    
    
    def livetime(birtime, nowtime):
        """
        计算你到现在活了多长时间
        :param birtime: 出生时间 字符串时间格式 '%Y-%m-%d %H:%M:%S'
        :param nowtime: 现在时间 字符串时间格式 '%Y-%m-%d %H:%M:%S'
        :return:返回拼接的字符串  '活了%s年%s月%s天%s时%s分%s秒'
        """
        # 1、将出生时间以及现在时间转化成元组时间
        bir_struct_time = time.strptime(birtime, '%Y-%m-%d %H:%M:%S')
        now_struct_time = time.strptime(nowtime, '%Y-%m-%d %H:%M:%S')
    
        # 2、转化成时间戳时间
        bir_stamptime = time.mktime(bir_struct_time)
        now_stamptime = time.mktime(now_struct_time)
    
        # 3、计算两者的时间戳差值
        live_stamptime = now_stamptime - bir_stamptime
    
        # 4、将差值转化成伦敦元祖时间(伦敦时间1970-1-1 0:0:0)方便计算不会出现借位减的情况
        live_struct_time = time.gmtime(live_stamptime)
    
        # 5、返回拼接好的字符串
        return '活了%s年%s月%s天%s时%s分%s秒' % (live_struct_time.tm_year - 1970,
                                         live_struct_time.tm_mon - 1,
                                         live_struct_time.tm_mday - 1,
                                         live_struct_time.tm_hour,
                                         live_struct_time.tm_min,
                                         live_struct_time.tm_sec
                                         )
    
    
    print(livetime('2000-10-10 0:0:0', '2010-10-10 0:0:0'))
    
  • 相关阅读:
    bzoj4476: [Jsoi2015]送礼物
    牛客练习赛42 E.热爆了
    bzoj3561: DZY Loves Math VI
    bzoj3560: DZY Loves Math V
    bzoj3512: DZY Loves Math IV
    bzoj3481: DZY Loves Math III
    使用WebUploader本地生成缩略图
    centos 7 安装JDK1.8
    APK反编译
    centos 7 配置pytorch运行环境
  • 原文地址:https://www.cnblogs.com/Kingfan1993/p/9892114.html
Copyright © 2020-2023  润新知