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')
函数
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纪元后经过的浮点秒数)。
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 numberReturn 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指秒数。
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([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.