13.3 time--时间操作和转换功能

13.3 time--时间操作和转换功能

本模块提供了大量的时间相关的操作函数,与此相关的模块有datetimecalendar

time.altzone

当地夏令时与UTC时间的差异,单位为秒。

例子:

#python 3.4

import time

 

print(time.altzone)

结果输出如下:

-32400

 

time.asctime([t])

转换元组或struct_time表示的时间为字符串表示。如果没有给出参数t,就默认使用localtime()函数来获取当前的时间。

例子:

#python 3.4

import time

 

print(time.asctime())

结果输出如下:

Wed Mar  9 16:31:53 2016

 

time.clock()

Unix返回当前处理器运行的时间。在Windows返回离第一次调用本函数的时间。本函数将要丢弃。

 

time.clock_getres(clk_id)

返回指定时钟的精度。仅用于Unix

 

time.clock_gettime(clk_id)

返回指定时钟的时间。仅用于Unix

 

time.clock_settime(clk_id, time)

设置指定时钟的时间。仅用于Unix

 

time.CLOCK_HIGHRES

Solaris系统使用的高精度时钟。

 

time.ctime([secs])

转换时间为当前时间字符串表示。

例子:

#python 3.4

import time

 

print(time.ctime())

结果输出如下:

Thu Mar 10 11:31:12 2016

 

time.daylight

如果夏令时有启用,就会保存一个非零的对象。

 

time.get_clock_info(name)

获取指定时钟的信息。获取的名称如下:

'clock': time.clock()

'monotonic': time.monotonic()

'perf_counter': time.perf_counter()

'process_time': time.process_time()

'time': time.time()

例子:

#python 3.4

import time

 

print(time.get_clock_info('clock'))

结果输出如下:

namespace(adjustable=False, implementation='QueryPerformanceCounter()', monotonic=True, resolution=3.20731678764131e-07)

 

time.gmtime([secs])

转换一个以秒表示的时间为UTCstruct_time结构。

例子:

#python 3.4

import time

 

print(time.gmtime())

结果输出如下:

time.struct_time(tm_year=2016, tm_mon=3, tm_mday=10, tm_hour=3, tm_min=58, tm_sec=37, tm_wday=3, tm_yday=70, tm_isdst=0)

 

time.localtime([secs])

转换的格式与函数gmtime()一样,只不过它是转换本地时间。

 

time.mktime(t)

这个函数与函数localtime()是相反的功能,它转换本地时间字符串为秒的时间,转换出来的时间为本地时间, 不是UTC时间。

 

time.monotonic()

 返回一个只增加的时钟。在Windows里是等同于GetTickCount()函数。

 

time.perf_counter()

返回高精度的时间计数值,以小数的方式返回,单位为秒。

例子:

#python 3.4

import time

 

print(time.perf_counter())

结果输出如下:

9.62195036292393e-07

 

time.process_time()

返回当前进程使用CPU的时间。以小数方式返回,单位为秒。

 

time.sleep(secs)

暂停当前进程执行secs秒。参数secs可以小数表示,可能暂停的时间小于输入的参数的值,因为存在触发计时子程序;也可能暂停的时间多于指定的时间,因为在不同的系统里调度系统存在调度时间,因此此函数可以用来要求不严格的等待。

 

time.strftime(format[, t])

转换一个元组或者struct_time表示的时间为格式format的时间字符串。

格式 意义 注意

%a Locales abbreviated weekday name.   

%A Locales full weekday name.   

%b Locales abbreviated month name.   

%B Locales full month name.   

%c Locales appropriate date and time representation.   

%d Day of the month as a decimal number [01,31].   

%H Hour (24-hour clock) as a decimal number [00,23].   

%I Hour (12-hour clock) as a decimal number [01,12].   

%j Day of the year as a decimal number [001,366].   

%m Month as a decimal number [01,12].   

%M Minute as a decimal number [00,59].   

%p Locales equivalent of either AM or PM. (1)

%S Second as a decimal number [00,61]. (2)

%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)

%w Weekday as a decimal number [0(Sunday),6].   

%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)

%x Locales appropriate date representation.   

%X Locales appropriate time representation.   

%y Year without century as a decimal number [00,99].   

%Y Year with century as a decimal number.   

%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].   

%Z Time zone name (no characters if no time zone exists).   

%% A literal '%' character.

例子:

#python 3.4

import time

 

print(time.strftime('%a, %d %b %Y %H %H:%M:%S +0000', time.gmtime()))

结果输出如下:

Fri, 11 Mar 2016 00 00:45:44 +0000

 

time.strptime(string[, format])

根据格式format来分析字符串表示的时间,转换为struct_time结果返回。

例子:

#python 3.4

import time

 

print(time.strptime("30 Nov 00", "%d %b %y"))

结果输出如下:

time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)

 

class time.struct_time

本结构主要使用于函数gmtime()localtime()strptime()返回的结果使用。它是一个命名的元组,结构如下:

索引 属性 值

0 tm_year (for example, 1993)

1 tm_mon range [1, 12]

2 tm_mday range [1, 31]

3 tm_hour range [0, 23]

4 tm_min range [0, 59]

5 tm_sec range [0, 61]

6 tm_wday range [0, 6], Monday is 0

7 tm_yday range [1, 366]

8 tm_isdst 0, 1 or -1

N/A tm_zone abbreviation of timezone name

N/A tm_gmtoff offset east of UTC in seconds

 

time.time()

返回从某一个时间点开始计算的秒数,以浮点数来表示。

 

time.timezone

返回当地的时区与标准时间的差,与UTC时间相比,在其东面为负,在其西边为正,时间单位为秒。

 

time.tzname

返回两个时区字符串的元组。第一个表示当地非夏令时的时区名称,第二个表示当地夏令时的时区名称。

 

time.tzset()

复位时间转换的规则。仅用于Unix



蔡军生  QQ:9073204  深圳

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

caimouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值