Python中的time模块介绍

time模块

简介

time模块是一个内建的模块,它提供不同的函数来操作时间值。

有两种时间的标准表示法。一种是秒为单位来表示以自Epoch,in UTC【自1970年1月1日凌晨】到当前的时间。

它可以是一个整型或浮点型数字(表示小数秒)。


另一种表示法是以一个元组的形式返回9个整数来表示当前时间 。格式如:

>>> time.localtime()
time.struct_time(tm_year=2015, tm_mon=5, tm_mday=14, tm_hour=22, tm_min=54, tm_sec=57, tm_wday=3, tm_yday=134, tm_isdst=0)

这个元组有以下项:

tm_year:year(4位数字,例如:1998)

tm_mon:month(1-12) 

tm_mday:day(1-31)

tm_hour:hours(0-23)

tm_min:minutes(0-59)

tm_sec:seconds(0-59)

tm_wday:weekday(0-6, Monday is 0)

tm_yday:Julian day(一年中的天数,1-366)

tm_isdst:DST(Daylight Savings Time)flag(-1, 0或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.


变量

   timezone -- difference in seconds between UTC and local standard time

                     当地时区离UTC(格林威治)的偏移秒数(>0美洲,<=0 大部分欧洲,亚洲,非洲)

>>> time.timezone
-28800

   altzone -- difference in  seconds between UTC and local DST time

                  UTC与当地DST时区的偏移秒数

>>> time.altzone
-32400

   daylight -- whether local time should reflect DST

>>> time.daylight
0

   tzname -- tuple of (standard time zone name, DST time zone name)

>>> time.tzname
('\xd6\xd0\xb9\xfa\xb1\xea\xd7\xbc\xca\xb1\xbc\xe4', '\xd6\xd0\xb9\xfa\xcf\xc4\xc1\xee\xca\xb1')


函数

    time() -- return current time in seconds since the Epoch as a float
    clock() -- return CPU time since process start as a float
    sleep() -- delay for a number of seconds given as a float
    gmtime() -- convert seconds since Epoch to UTC tuple
    localtime() -- convert seconds since Epoch to local time tuple
    asctime() -- convert time tuple to string
    ctime() -- convert time in seconds to string
    mktime() -- convert local time tuple to seconds since Epoch
    strftime() -- convert time tuple to string according to format specification
    strptime() -- parse string to time tuple according to format specification
    tzset() -- change the local timezone

time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。

        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.
>>> time.time()
1431619230.52

clock() 用以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。

        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.
>>> time.clock()
428.64755962049634
>>> time.clock()
438.0747158340805
>>> time.clock()
440.45101607451244

sleep() 推迟调用线程的运行,secs指秒数。

       sleep(seconds)

       Delay execution for a given number of seconds.  The argument may be
       a floating point number for subsecond precision.

gmtime() 接收时间辍(1970纪元后经过的浮点秒数)并返回格林威治天文时间下的时间元组t。注:t.tm_isdst始终为0

        gmtime(...)
        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.

localtime() 接收时间辍(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取0或1,取决于当地当时是不是夏令时)。

        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 tim
        When 'seconds' is not passed in, convert the current time instead.

asctime()  接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。

        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.

ctime()     作用相当于asctime(localtime(secs)),未给参数相当于asctime()

        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.

mktime()    接受时间元组并返回时间辍(1970纪元后经过的浮点秒数)。

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

        Convert a time tuple in local time to seconds since the Epoch.


strftime()  接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。time.strftime(fmt[,tupletime])

        strftime(...)
        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.

strptime()  根据fmt的格式把一个时间字符串解析为时间元组。time.strptime(str,fmt='%a %b %d %H:%M:%S %Y')

        strptime(...)
        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()).

tzset()   修改当前时区


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值