把python基本功搞扎实(3)

来源:互联网 发布:h5投票系统源码 编辑:程序博客网 时间:2024/05/21 10:56

python中关于time的操作

python中提供了time模块,这个模块提供了多样的时间相关函数,相关类似的模块还有datetime和calendar模块。

这个模块定义了一下的函数和数据条目

time.accept2dyear

表明是否接受双位的years的布尔值,默认是true,但是如果环境变量PYTHONY2K被设置为一个不是空的string,它就变成false。它可以在运行时修改

time.asctime([t])

转化一个struct_time的元组成一个time,即将一个struct_time对象转化为time形式。

time.clock()

在Unix中,返回当前时间的秒数

time.ctime([secs])

转化秒数变成当地时间。如果参数secs没有给出,那么将会返回当前时间,以string形式输出,ctime(secs)等同于asctime(localtime(secs)).

time.gmtime([secs])

将秒数转化为struct_time,不过这个可以转化为其他地区的时间。如果secs参数没有给出,那么将返回当前时间,不同的是,返回形式将是struct_time的类型。

time.localtime([secs])

像gmtime()函数,但是这个是转化为当前时间的

time.mktime()

这个是localtime()函数的反转,它的参数是9个,如果输入的值不能转化为一个合法的时间,将生成OverflowError或者ValueError异常。、

time.sleep(secs)

挂起当前线程,时间为给定的参数。

time.strftime(format[,t])

转变tuple或者struct_time成time,返回的将是格式化后的时间。

fom time import gmtime,strftimestrftime('%a,%d %b %Y %H:%S +0000',gmtime())

time.strptime(string[,format])

根据format来格式化string.

class.time.struct_time

这是个时间类型

time.time()

返回当前时间距UTC时期的总秒数