• [python] 之 time模块


    time模块提供了各种功能和方式标识时间值,主要有两种标准标识时间的格式:一种是以时间戳的形式标识,该时间戳是从纪元1970年1月1日0点0分0秒至当前时间的,总共经历的秒数,以浮点小数标识,该纪元时间也依赖于个人的操作系统;另一种标识时间的形式是以一种含有9个元素的元组(标识local time)。该元组的9个元素为:

    year (four digits, e.g. 1998)
    month (1-12)
    day (1-31)
    hours (0-23)
    minutes (0-59)
    seconds (0-59)
    weekday (0-6, Monday is 0)
    Julian day (day in the year, 1-366)
    DST (Daylight Savings Time) flag (-1, 0 or 1)
    If the DST flag is 0, the time is given in the regular time zone;
    if it is 1, the time is given in the DST time zone;
    if it is -1, mktime() should guess based on the date and time.

    现对其中常用的时间函数作一简单介绍:

    asctime([tuple]) -> string convert time tuple to string
    ctime(seconds) -> string convert time in seconds to string
    strftime(format[, tuple]) -> string convert time tuple to string according to format specification
    strptime(string, format) -> struct_time parse string to time tuple according to format specification
    clock() -> floating point number return CPU time since process start as a float

    mktime(tuple) -> floating point number

    convert local time tuple to seconds since Epoch

    time() -> floating point number

    return current time in seconds since the Epoch as a float

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

    convert seconds since Epoch to UTC tuple

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

    convert seconds since Epoch to local time tuple
    sleep(seconds) delay for a number of seconds given as a float

    1. time.asctime([tuple]) -> string

      将一个时间(元组)转换为一个字符串,默认为当前时间。

    1 >>> time.asctime()
    2 'Sun Oct 30 10:00:57 2016'
    3 >>> time.asctime(time.localtime())
    4 'Sun Oct 30 10:02:22 2016'

    2. time.clock() -> floating point number

      返回进程启动或调用 clock() 时的CPU时间或当前真实时间,第一次调用时标识程序运行的实际时间,第二次调用时标识自第一次调用后,到本次调用的时间间隔,以浮点秒数标识。

     1 >>> time.clock() 2 0.01 

    3. time.ctime(seconds) -> string

      返回自纪元时间(一般1970)经历seconds时间的时间;如果不提供seconds,则默认为当前时间,返回值是以字符串的形式标识的。

    1 >>> time.ctime()
    2 'Sun Oct 30 10:24:08 2016'
    3 >>> time.ctime(34124)
    4 'Thu Jan  1 17:28:44 1970'
    5 >>> time.ctime(3412456780)
    6 'Sat Feb 19 08:39:40 2078'

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

      返回自纪元时间(一般1970)经历seconds时间的时间;如果不提供seconds,则默认为当前时间,返回一个9元素元组形式的UTC时间格式,可通过time.asctime()转换为一种友好的时间格式。

    1 >>> time.gmtime()
    2 time.struct_time(tm_year=2016, tm_mon=10, tm_mday=30, tm_hour=2, tm_min=30, tm_sec=0, tm_wday=6, tm_yday=304, tm_isdst=0)
    3 >>> time.gmtime(423545)
    4 time.struct_time(tm_year=1970, tm_mon=1, tm_mday=5, tm_hour=21, tm_min=39, tm_sec=5, tm_wday=0, tm_yday=5, tm_isdst=0)
    5 >>> time.asctime(time.gmtime())
    6 'Sun Oct 30 02:34:58 2016'

    5. time.localtime() -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)

      返回自纪元时间(一般1970)经历seconds时间的时间;如果不提供seconds,则默认为当前时间,返回一个9元素元组形式的本地时间格式,可通过time.asctime()转换为一种友好的时间格式。

    1 >>> time.localtime()
    2 time.struct_time(tm_year=2016, tm_mon=10, tm_mday=30, tm_hour=10, tm_min=41, tm_sec=5, tm_wday=6, tm_yday=304, tm_isdst=0)
    3 >>> time.localtime(76899)
    4 time.struct_time(tm_year=1970, tm_mon=1, tm_mday=2, tm_hour=5, tm_min=21, tm_sec=39, tm_wday=4, tm_yday=2, tm_isdst=0)
    5 >>> time.asctime(time.localtime())
    6 'Sun Oct 30 10:41:50 2016'

    6. time.mktime(tuple) -> floating point number

      将给定的元组时间转换为浮点秒数,该秒数为自纪元时间(1970)起到给定时间之间的秒数。

    1 >>> time.mktime(time.localtime())
    2 1477795487.0
    3 >>> time.mktime(time.gmtime())
    4 1477766742.0
    5 >>> time.mktime(time.gmtime(76384))
    6 47584.0

    7. time.sleep(seconds)

      延迟seconds时间执行之后的程序进程。

    8. time.strftime(format[, tuple]) -> string

      将一个9元素时间元组转换为给定的时间格式,并以字符串的形式输出;若未指定时间,则默认为local time。具体格式 See the library reference manual for formatting codes。

    1 >>> a = (2016,2,12,12,12,12,4,5,6)
    2 >>> time.mktime(a)
    3 1455250332.0
    4 >>> time.strftime('%Y%m%d%H%M%S', a)
    5 '20160212121212'

     python中时间日期格式化符号:
      %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 当前时区的名称
      %% %号本身 

    9. time.strptime(string, format) -> struct_time

      按照给定的时间格式,将字符串解析为时间元组。具体格式 See the library reference manual for formatting codes (same as strftime())。

    10. time.time())-> floating point number

      返回自纪元时间(1970)起到当前时间的时间戳,以浮点秒数标识。

     1 >>> time.time()
     2 1477799217.188667
     3 >>> time.time()
     4 1477799244.483566
     5 >>> time.time()
     6 1477799245.771593
     7 >>> time.time()
     8 1477799246.657197
     9 >>> time.time()
    10 1477799247.236571
  • 相关阅读:
    资源链接
    Node.js—学习
    Node.js—基本知识
    创建服务器证书命令
    解决:Could not load type 'System.ServiceModel.Activation.HttpModule' from assemb
    应用框架的设计与实现——.NET平台(10 授权服务.CodeAccessSecurityAttribute)
    .NET 实现自定义ContextUser的Identity和Principal实现自定义用户信息,权限验证。
    MS对WCF配置中security节点的解释
    Makecert.exe(证书创建工具)
    JAVA操作Mysql数据库
  • 原文地址:https://www.cnblogs.com/xiaofeiIDO/p/6012803.html
Copyright © 2020-2023  润新知