python中time模块详解_time模块详解(python教程)

python time模块详解

分类: python 2009-03-28 23:35 81986人阅读 评论(9) 收藏 举报

pythonstructstringdstimportdate python 的内嵌time模板翻译及说明

一、简介

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

说明:一般有两种表示时间的方式:

第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的

第二种以数组的形式表示即(struct_time),共有九个元素,分别表示,同一个时间戳的struct_time会因为时区不同而不同

year (four digits, e.g. 1998)

month (1-12)

day (1-31)

hours (0-23)

minutes (0-59)

seconds (0-59)

weekday (0-6, Monday is 0)

Julian day (day in the year, 1-366)

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

夏令时介绍:http://baike.baidu.com/view/100246.htm

UTC介绍:http://wenda.tianya.cn/wenda/thread?tid=283921a9da7c5aef&clk=wttpcts

二、函数介绍

1.asctime()

asctime([tuple]) -&gt string

将一个struct_time(默认为当时时间),转换成字符串

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.

2.clock()

clock() -&gt floating point number

该函数有两个功能,

在第一次调用的时候,返回的是程序运行的实际时间;

以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔

示例:

import time

if __name__ == '__main__':

time.sleep(1)

print "clock1:%s" % time.clock()

time.sleep(1)

print "clock2:%s" % time.clock()

time.sleep(1)

print "clock3:%s" % time.clock()

输出:

clock1:3.35238137808e-006

clock2:1.00004944763

clock3:2.00012040636

其中第一个clock输出的是程序运行时间

第二、三个clock输出的都是与第一个clock的时间间隔

3.sleep(...)

sleep(seconds)

线程推迟指定的时间运行,经过测试,单位为秒,但是在帮助文档中有以下这样一句话,这关是看不懂

“The argument may be a floating point number for subsecond precision.”

4.ctime(...)

ctime(seconds) -&gt string

将一个时间戳(默认为当前时间)转换成一个时间字符串

例如:

time.ctime()

输出为:'Sat Mar 28 22:24:24 2009'

5.gmtime(...)

gmtime([seconds]) -&gt (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)

将一个时间戳转换成一个UTC时区(0时区)的struct_time,如果seconds参数未输入,则以当前时间为转换标准

6.localtime(...)

localtime([seconds]) -&gt (tm_year,tm_mon,tm_day,tm_hour,tm_min,

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值