• Python的time模块随笔。


    个人的datetime记录:https://www.cnblogs.com/sidianok/p/11832621.html

    2020年8月15日重新更新

    Python的time与datetime一直没有好好的深入记录过,今天想到了,先把time的一些基本功能记下来。

    首先先上time.time与time.ctime(其实ctime应该与time.asctime一起用比较好):

    time.ctime与time.asctime都是格式化输出简单的时间字符串,但操作的对象不同,一个是操作时间戳timestamp,一个是操作时间元祖struct_time

    既然说到timestamp可以从time.time返回从1970年1月1日0:00到现在的过的时间总秒,类型为浮点型。time.localtime()可以返回struct_time对象(时间精度只能到秒)。

    In [34]: timestamp                                                                                        
    Out[34]: 1597476594.6344671
    
    In [35]: struct_time = time.localtime()                                                                   
    
    In [36]: struct_time                                                                                      
    Out[36]: time.struct_time(tm_year=2020, tm_mon=8, tm_mday=15, tm_hour=15, tm_min=30, tm_sec=28, tm_wday=5, tm_yday=228, tm_isdst=0)
    
    In [37]:  
                    
    

     现在通过time.ctime与time.asctime来简单格式化输出时间。timestamp与struct_time返回的都是本地时间。

    In [37]: time.ctime(timestamp)                                                                            
    Out[37]: 'Sat Aug 15 15:29:54 2020'
    
    In [38]: time.asctime(struct_time)                                                                        
    Out[38]: 'Sat Aug 15 15:30:28 2020'
    
    In [39]:  
    

     time.localtime与time.ctime都可以接收时间戳参数,且返回本地时间。

    In [39]: time.localtime(0)                                                                                
    Out[39]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
    
    In [40]: time.ctime(0)                                                                                    
    Out[40]: 'Thu Jan  1 08:00:00 1970'
    
    In [41]: t0 = time.localtime(0)                                                                           
    
    In [42]: time.asctime(t0)                                                                                 
    Out[42]: 'Thu Jan  1 08:00:00 1970'
    
    In [43]:  
    

    获取格林威治时间也就是0时区的时间,可以通过time.gmtime(),里面可以添加时间戳.

    In [57]: time.gmtime()                                                                                    
    Out[57]: time.struct_time(tm_year=2020, tm_mon=8, tm_mday=15, tm_hour=7, tm_min=45, tm_sec=40, tm_wday=5, tm_yday=228, tm_isdst=0)
    
    In [58]: time.gmtime(timestamp)                                                                           
    Out[58]: time.struct_time(tm_year=2020, tm_mon=8, tm_mday=15, tm_hour=7, tm_min=29, tm_sec=54, tm_wday=5, tm_yday=228, tm_isdst=0)
    
    In [59]: time.localtime()                                                                                 
    Out[59]: time.struct_time(tm_year=2020, tm_mon=8, tm_mday=15, tm_hour=15, tm_min=46, tm_sec=8, tm_wday=5, tm_yday=228, tm_isdst=0)
    

     time.gmtime返回的也是struct_time,只不过这个返回的时间为0时区的时间。当然,你也可以通过自己的时区与零时区进行加减法运算进行计算。time模块里面,除了时间戳为

    浮点型数字,可以进行加减运算,另外的对象不能进行大小比较

    struct_time可以指定格式化输出,也可以转换成timestamp,同时struct_time的输出,里面的各个参数也有一些特殊的意义。

    time.struct_time(tm_year=2020, tm_mon=8, tm_mday=15, tm_hour=15, tm_min=50, tm_sec=29, tm_wday=5, tm_yday=228, tm_isdst=0)
    

     参数tm_wday为星期几,从0开始星期一就是0,星期日为6,tm_yday=1为一年中的第几天,m_isdst = 1 的时候表示时间是夏令时,值为0的时候表示非夏令时

    比较有用的是星期几还有一年中的第几天。

    开始做格式化输出:
    %a     Locale的缩写工作日名称。     
    %A     Locale的整个工作日名称。     
    %b     语言环境的缩写月份名称。     
    %B     Locale的完整月份名称。     
    %c     语言环境的适当日期和时间表示。     
    %d     一个十进制数字[01,31]。     
    %H     小时(24小时制),十进制数[00,23]。     
    %I     小时(12小时制)十进制数[01,12]。     
    %j     一年中的十进制数[001,366]。     
    %m     月为十进制数[01,12]。     
    %M     以十进制数分钟[00,59]。     
    %p     Locale相当于AM或PM。     (1)
    %S     秒为十进制数[00,61]。     (2)
    %U     年的星期数(星期日为星期的第一天)为十进制数[00,53]。在第一个星期日之前的新的一年的所有天被认为是在第0周。     (3)
    %w     工作日为十进制数[0(星期日),6]。     
    %W     年的星期数(星期一作为星期的第一天)作为十进制数[00,53]。在第一个星期一之前的新的一年中的所有天被认为是在第0周。     (3)
    %x     语言环境的适当日期表示。     
    %X     语言环境的适当时间表示。     
    %y     年,无世纪作为十进制数[00,99]。     
    %Y     年份以世纪为十进制数。     
    %z     指示与+ HHMM或-HHMM形式的UTC / GMT的正或负时差的时区偏移,其中H表示十进制小时数字,M表示十进制分数字[-23:59,+23:59]。     
    %Z     时区名称(如果没有时区,则不包含字符)。     
    %%     字面值'%'字符。     

    In [63]: time.strftime?                                                                                   
    Docstring:
    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.
    Type:      builtin_function_or_method
    
    In [64]: time.strftime("%Y-%m-%d %X")                                                                     
    Out[64]: '2020-08-15 16:01:03'
    
    In [65]: time.strftime("%Y-%m-%d %X", time.localtime())                                                   
    Out[65]: '2020-08-15 16:01:37'
    
    In [66]:  
    

     从示例可以看出来,传入两个参数,第一个参数是必填的,格式化输出的格式要求,第二个参数默认为current struct_time,当然你也可以添加自己定义的struct_time

    把struct_time转换成timestamp相对简单多了,只需要再mktime里面传入struct_time就可以了

    In [68]: time.mktime?                                                                                     
    Docstring:
    mktime(tuple) -> floating point number
    
    Convert a time tuple in local time to seconds since the Epoch.
    Note that mktime(gmtime(0)) will not generally return zero for most
    time zones; instead the returned value will either be equal to that
    of the timezone or altzone attributes on the time module.
    Type:      builtin_function_or_method
    
    In [69]: time.mktime(time.localtime())                                                                    
    Out[69]: 1597478852.0
    
    In [70]: time.time()                                                                                      
    Out[70]: 1597478856.857926
    
    In [71]:  
    

    最后还有一个把时间戳与字符串转化为struct_time的,第一个已经介绍了通过time.localtime传入参数

    第二个通过time.strptime可以实现

    In [71]: time.strptime?                                                                                   
    Docstring:
    strptime(string, format) -> struct_time
    
    Parse a string to a time tuple according to a format specification.
    See the library reference manual for formatting codes (same as
    strftime()).
    
    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.
    Type:      builtin_function_or_method
    
    In [72]: str_time = '2011-05-05 16:37:06'                                                                 
    
    In [73]: time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X')                                              
    Out[73]: time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6, tm_wday=3, tm_yday=125, tm_isdst=-1)
    

     注意这个里面必须填写两个参数,第一个为输入被转换的时间字符串,第二个为转换的格式,必须对应。

    这个图可以比较直观的看到一些方法的使用.

    下面是Python3标准库的一些知识

    time.monotonic用来计算相对时间,因为time.time读取的是计算机时间,如果计算机时间调整的化,会对相对时间差发生变化,该函数可以避免发生。

    从时间精度来看,还是time.time的精度高。

    import time
    t1 = time.time()
    tt1 = time.monotonic()
    time.sleep(0.5)
    t2 = time.time()
    tt2 = time.monotonic()
    print(t2 - t1)
    print(tt2 - tt1)

    # 0.5024559497833252
    # 0.502452992

     time.perf_counter跟上面的monotonic实际我使用差不多,主要用于测试性能时测试,该函数具体运行出来的数值,只能用于比较计算,不能用于绝对时间。该函数时间精度与monotoni一样。

    import time
    t1 = time.time()
    tt1 = time.monotonic()
    ttt1 = time.perf_counter()
    time.sleep(0.5)
    t2 = time.time()
    tt2 = time.monotonic()
    ttt2 = time.perf_counter()
    
    print(t2 - t1)
    print(tt2 - tt1)
    print(ttt2 - ttt1)

    # 0.5030510425567627
    # 0.503047529
    # 0.503047695
    ti
  • 相关阅读:
    构造 BestCoder Round #52 (div.2) 1001 Victor and Machine
    multiset || 线段树 HDOJ 4302 Holedox Eating
    BFS(最短路) HDOJ 4308 Saving Princess claire_
    组合数专题
    余数专题
    数论 HDOJ 5407 CRB and Candies
    异或+构造 HDOJ 5416 CRB and Tree
    构造 HDOJ 5414 CRB and String
    背包DP HDOJ 5410 CRB and His Birthday
    博客贴自定义高亮代码
  • 原文地址:https://www.cnblogs.com/sidianok/p/11831019.html
Copyright © 2020-2023  润新知