time模块

time模块提供各种操作时间的函数

#1、时间戳    1970年1月1日之后的秒
  #2、元组 包含了:年、日、星期等... time.struct_time
  #3、格式化的字符串    2014-11-11 11:11

    (1)asctime(p_tuple=None)

def asctime(p_tuple=None): # real signature unknown; restored from __doc__
    """
    asctime([tuple]) -> string

    Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
    When the time tuple is not present, current time as returned by localtime()
    is used.
    """
    return ""
    asctime()返回当前系统的时间,如下:

    >>> time.asctime()
  'Sun May 28 13:32:04 2017'

    (2)clock()

def clock(): # real signature unknown; restored from __doc__
    """
    clock() -> floating point number

    Return the CPU time or real time since the start of the process or since
    the first call to clock(). This has as much precision as the system
    records.
    """
    return 0.0

clock()返回CPU系统当前的时间,或者真实时间,从开始到第一次使用clock()。

    >>> time.clock()
  0.44741
    (3)clock_getres(clk_id)
    def clock_getres(clk_id): # real signature unknown; restored from __doc__
    """
    clock_getres(clk_id) -> floating point number

    Return the resolution (precision) of the specified clock clk_id.
    """
    return 0.0

    (4)clock_gettime(clk_id)

def clock_gettime(clk_id): # real signature unknown; restored from __doc__
    """
    clock_gettime(clk_id) -> floating point number

    Return the time of the specified clock clk_id.
    """
    return 0.0

    (5)clock_settime(clk_id,time)

def clock_settime(clk_id, time): # real signature unknown; restored from __doc__
    """
    clock_settime(clk_id, time)

    Set the time of the specified clock clk_id.
    """
    pass

    (6)ctime(seconds=None)

def ctime(seconds=None): # known case of time.ctime
    """
    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.
    """
    return ""

ctime()返回系统当前的时间:

>>> time.ctime()
  'Sun May 28 13:55:39 2017'
    (7)get_clock_info(name)

def get_clock_info(name): # real signature unknown; restored from __doc__
    """
    get_clock_info(name: str) -> dict

    Get information of the specified clock.
    """
    return {}

    (8)gmtime(seconds=None)

def gmtime(seconds=None): # real signature unknown; restored from __doc__
    """
    gmtime([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 UTC (a.k.a.
    GMT). When 'seconds' is not passed in, convert the current time instead.

    If the platform supports the tm_gmtoff and tm_zone, they are available as
    attributes only.
    """
    pass

gmtime(seconds=None)返回对应的时间格式,time.struct_time。

>>> time.gmtime()
  time.struct_time(tm_year=2017, tm_mon=5, tm_mday=28, tm_hour=6, tm_min=1, tm_sec=28, tm_wday=6, tm_yday=148, tm_isdst=0)
    (9)localtime(seconds=None)    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

localtime()返回时间time.struct_time格式的日期。

    >>> time.localtime()
  time.struct_time(tm_year=2017, tm_mon=5, tm_mday=28, tm_hour=14, tm_min=3, tm_sec=58, tm_wday=6, tm_yday=148, tm_isdst=0)
    (10)mktime()

def mktime(p_tuple): # real signature unknown; restored from __doc__
    """
    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.
    """
    return 0.0
    (11)monotonic()

def monotonic(): # real signature unknown; restored from __doc__
    """
    monotonic() -> float

    Monotonic clock, cannot go backward.
    """
    return 0.0

>>> time.monotonic()
  27333.713613735
    (12)perf_counter()
    def perf_counter(): # real signature unknown; restored from __doc__
    """
    perf_counter() -> float

    Performance counter for benchmarking.
    """
    return 0.0

>>> time.perf_counter()
  27418.099319872
    (13)process_time()

def process_time(): # real signature unknown; restored from __doc__
    """
    process_time() -> float

    Process time for profiling: sum of the kernel and user-space CPU time.
    """
    return 0.0
     >>> time.process_time()
  0.521978947

    (14)sleep(seconds)

  def sleep(seconds): # real signature unknown; restored from __doc__
    """
    sleep(seconds)

    Delay execution for a given number of seconds. The argument may be
    a floating point number for subsecond precision.
    """
    pass
    time.sleep(seconds)是程序停止运行一段时间。休眠。例如time.sleep(10)代表程序停止等待10秒钟。

    (15)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 ""

strftime(format,p_tuple)将时间进行格式转换,只能转换localtime()和gmtime()的struct_time格式时间情况;

>>> time.strftime("%Y-%m-%d %p",time.localtime())
  '2017-05-28 PM'
  >>> time.strftime("%Y-%m-%d %I:%M:%S%p",time.localtime())
  '2017-05-28 02:23:21PM'
    日期格式的转换,只能转换struct_time的格式;

>>> time.strftime("%a",time.localtime())    %a返回星期的简写
  'Sun'
    >>> time.strftime("%A",time.localtime())    %A返回日期的全拼
  'Sunday'

(16)strptime(string,format)
    def strptime(string, format): # real signature unknown; restored from __doc__
    """
    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.
    """
    return struct_time

strptime(string,format)将字符串的日期类型转化为struct_time类型。

>>> time.strptime("2017-5-18","%Y-%m-%d")
  time.struct_time(tm_year=2017, tm_mon=5, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=138, tm_isdst=-1)
    (17)time()

def time(): # real signature unknown; restored from __doc__
    """
    time() -> floating point number

    Return the current time in seconds since the Epoch.
    Fractions of a second may be present if the system clock provides them.
    """
    return 0.0

time()返回浮点数。

>>> time.time()
  1495953536.3603268

    (18)tzset()

def tzset(): # real signature unknown; restored from __doc__
    """
    tzset()

    Initialize, or reinitialize, the local timezone to the value stored in
    os.environ['TZ']. The TZ environment variable should be specified in
    standard Unix timezone format as documented in the tzset man page
    (eg. 'US/Eastern', 'Europe/Amsterdam'). Unknown timezones will silently
    fall back to UTC. If the TZ environment variable is not set, the local
    timezone is set to the systems best guess of wallclock time.
    Changing the TZ environment variable without calling tzset *may* change
    the local timezone used by methods such as localtime, but this behaviour
    should not be relied on.
    """
    pass

    datetime模块

"""

    import datetime

    datetime.date:表示日期的类。常用的属性有year, month, day

datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond

datetime.datetime:表示日期时间

datetime.timedelta:表示时间间隔,即两个时间点之间的长度

timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

strftime("%Y-%m-%d")

"""

    (1)datetime.date:表示日期的类。常用的属性有year,month,day

>>> datetime.date.today()
  datetime.date(2017, 5, 28)
    返回日期的格式情况,包含的属性有year(年)、month(月)、日(day)。

    (2)datetime.time:表示时间的类。常用的属性有hour,minute,second,microsecond

>>> datetime.time(12,30,59,99)
  datetime.time(12, 30, 59, 99)

返回日期时间的格式情况,如datetime.time()

    (3)datetime.datetime:表示日期时间

>>> datetime.datetime(2016,5,12,7,59,59,99)
  datetime.datetime(2016, 5, 12, 7, 59, 59, 99)

    >>> datetime.datetime.now()
  datetime.datetime(2017, 5, 28, 15, 34, 1, 105235)

>>> datetime.datetime.today()
  datetime.datetime(2017, 5, 28, 15, 35, 9, 384407)

    (4)datetime.timedelta:表示时间间隔,两个时间点之间的长度

    (5)timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

    (6)strftime("%Y-%m-%d")

    实例:

>>> now_date = datetime.datetime.now() + datetime.timedelta(days=10)      (1)比现在日期多十天
  >>> now_date
  datetime.datetime(2017, 6, 7, 15, 37, 7, 936368)

>>> str_to_date = datetime.datetime.strptime("16/11/17 16:30","%d/%m/%y %H:%M")   (2)将字符串时间格式化转化为时间
  >>> str_to_date
  datetime.datetime(2017, 11, 16, 16, 30)

>>> new_date = datetime.datetime.now() + datetime.timedelta(hours=-10)     (3)比现在时间少10个小时
  >>> new_date
  datetime.datetime(2017, 5, 28, 20, 1, 11, 805686)

day6 time和datetime模块的更多相关文章

  1. Day6 Python常用的模块

    一.logging模块 一.日志级别 critical=50 error=40 waring=30 info=20 debug=10 notset=0 二.默认的日志级别是waring(30),默认的 ...

  2. python中datetime模块

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供 的接口与C标准库time.h基本一致.相比于time模块 ...

  3. python datetime模块参数详解

    Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块,它提供 的接口与C标准库time.h基本一致.相比于time模块,datetime模块的接 ...

  4. Python处理时间 time && datetime 模块

    Python处理时间 time  &&  datetime 模块 个人整理,获取时间方式: import datetime import time #获取当前时间:Thu Nov 03 ...

  5. python time模块和datetime模块详解

    一.time模块 time模块中时间表现的格式主要有三种: a.timestamp时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量 b.struct_time时间元组,共 ...

  6. python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块

    正则表达式   语法:             mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...

  7. python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客

    python datetime模块strptime/strptime format常见格式命令_施罗德_新浪博客     python datetime模块strptime/strptime form ...

  8. Python datetime模块的datetime类

    datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day. datetime.time:表示时间的类.常用的属性有hour, m ...

  9. python处理时间--- datetime模块

    1   Python提供了多个内置模块用于操作日期时间,像calendar,time,datetime.time模块我在之前的文章已经有所介绍,它提供的接口与C标准库time.h基本一致.相比于tim ...

随机推荐

  1. _MSC_VER

    https://msdn.microsoft.com/en-us/library/vstudio/b0084kay.aspx Evaluates to an integer literal that ...

  2. vs下取得资源文件中的版本信息

    在Windows Mobile和Wince(Windows Embedded CE)下开发的产品,有时候需要显示当前产品的版本信息.一般来说,版本信息是保存在资源文件里面的,例如下图: 为了保持一致, ...

  3. C++并发编程 互斥和同步

    C++并发编程 异步任务(async) 线程基本的互斥和同步工具类, 主要包括: std::mutex 类 std::recursive_mutex 类 std::timed_mutex 类 std: ...

  4. poppo大根堆的原理与实现。

    大根堆的定义:1 大根堆是一个大根树 2 大根堆是一个完全二叉树 所以大根堆用数组表示是连续的,不会出现空白字段. 对于大根堆的插入 对于大根堆的插入,可以在排序前确定大根堆的形状,可以确定元素5从位 ...

  5. centos 前端环境搭建

    Node.js 安装 wget 下载安装 yum -y install gcc make gcc-c++ openssl-devel wget node v6.11.0 下载 wget https:/ ...

  6. vue 拦截器

    拦截器:请求发送之前和请求返回之后的处理 使用:1.config---dev.env.js 开发环境配置 2. prod.env.js 生产 API:'http://www.wpdic.com' 3. ...

  7. Spring 与 SpringMVC 容器父子关系引出的相应问题

    1)关系说明 spring 与 springmvc 父子关系:spring (父容器),springmvc (子容器) springmvc(子)--- 可调用 --> spring(父) 中的 ...

  8. HDU 4778 状压DP

    一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...

  9. 【Atcoder】AGC 020 D - Min Max Repetition 二分+构造

    [题意]定义f(A,B)为一个字符串,满足: 1.长度为A+B,含有A个‘A',B个'B'. 2.最长的相同字符子串最短. 3.在满足以上2条的情况下,字典序最小. 例如, f(2,3) = BABA ...

  10. .NET中的异常和异常处理

    .NET中的异常(Exception) .net中的中异常的父类是Exception,大多数异常一般继承自Exception. 可以通过编写一个继承自Exception的类的方式,自定义异常类! 异常 ...