zoukankan      html  css  js  c++  java
  • time库基本使用

    概述:time库是python中处理时间的标准库

    import time

    time.<b>()

    包括3类函数:

    常用

    时间获取:time.time() 时间戳,浮点数(秒)   time.ctime()字符串   time.gmtime()时间元组

    时间格式化:time.strftime()时间元组-->字符串   time.strptime()字符串-->时间元组

    程序计时:time.sleep()   time.perf_counter

    --------------------时间获取

    >>> time.time()  # 获取系统当前时间戳,浮点数
    1543109157.805488

    >>> time.gmtime()  # 获取当前时间,返回时间元组(格林威治时间)
    time.struct_time(tm_year=2018, tm_mon=11, tm_mday=25, tm_hour=1, tm_min=45, tm_sec=7, tm_wday=6, tm_yday=329, tm_isdst=0)

    gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)

    >>> time.localtime()  # 获取当前时间,返回时间元组(本地时间)
    time.struct_time(tm_year=2018, tm_mon=11, tm_mday=25, tm_hour=9, tm_min=39, tm_sec=4, tm_wday=6, tm_yday=329, tm_isdst=0)

    localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
    tm_sec,tm_wday,tm_yday,tm_isdst)

    >>> time.ctime()  # 获取当前时间以易读方式表示,返回字符串
    'Sun Nov 25 09:27:39 2018'

    ctime(seconds) -> string

    Convert a time in seconds since the Epoch to a string in local time.
    This is equivalent to asctime(localtime(seconds)). When the time tuple is
    not present, current time as returned by localtime() is used.

    time.mktime()  # 把时间元组,转换为秒

    mktime(tuple) -> floating point number

    >>> time.mktime(time.strptime('20181125 102030','%Y%m%d %H%M%S'))
    1543112430.0

    >>> time.asctime()
    'Sun Nov 25 09:33:48 2018'

    asctime([tuple]) -> string

    ----------------时间格式化

    strftime(tpl, ts) # tpl是格式化模板字符串,用来定义输出效果 。ts是时间元组

    strftime(format[, tuple]) -> string
    >>> time.strftime('%Y-%m-%d %H:%M:%S')
    '2018-11-25 09:51:44'

    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.

    time.strptime(str,tmp)  # 把字符串格式的时间,转换为时间元组

    str是字符串形式的时间值 tpl是格式化模板字符串,用来定义输入效果

    >>> time.strptime('20181125','%Y%m%d')
    time.struct_time(tm_year=2018, tm_mon=11, tm_mday=25, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=329, tm_isdst=-1)

    -----------------------程序计时

    测量时间 time.perf_counter()

    返回一个CPU级别的精确时间计数值,单位为秒 由于这个计数值起点不确定,连续调用差值才有意义

    >>> start = time.perf_counter()
    >>> end = time.perf_counter()
    >>> end - start
    9.91788138099946

    产生时间 time.sleep()

    >>> time.sleep(2)
    >>>

  • 相关阅读:
    Ant学习实例
    ant 使用指南
    tomcat配置管理用户名密码
    纯JAVA驱动:sqlserver版本不同,驱动与连接也有所区别
    STRUTS2获得session和request
    SSH整合,"sessionFactory " or "hibernateTemplate " is required异常
    tmp
    让Tomcat支持中文文件名
    postgreSQL初步使用总结
    postgreSQL9.1忘记postgres用户密码怎么办
  • 原文地址:https://www.cnblogs.com/lisi01/p/10014789.html
Copyright © 2011-2022 走看看