time.time time.time()函数返回从1970年1月1日以来的秒数,这是一个浮点数 >>> import time >>> time.time() 1382406948.053174time.sleep 可以通过调用time.sleep来挂起当前的进程。time.sleep接收一个浮点型参数,表示进程挂起的时间 >>> time.sleep(3.5) 相当于bash中sleep 3.5time.clock time.clock() 返回第一次调用该方法到现在的秒数,其精确度高于1微秒。可以使用该函数来记录程序执行的时间。下面是一个简单的例子 (cat time_clock.py): #!/usr/bin/env python import time def procedure(): time.sleep(2.5) # measure process time t0 = time.clock() procedure() print time.clock() - t0, "seconds process time" # measure wall time t0 = time.time() procedure() print time.time() - t0, "seconds wall time" # python time_clock.py 0.0 seconds process time 2.50261807442 seconds wall timetime.gmtime 该函数原型为:time.gmtime([sec]),可选的参数sec表示从1970-1-1以来的秒数。其默认值为time.time(),函数返回 time.struct_time类型的对象。(struct_time是在time模块中定义的表示时间的对象),下面是一个简单的例子: >>> import time >>> time.gmtime() #获取当前时间的struct_time对象 time.struct_time(tm_year=2013, tm_mon=10, tm_mday=22, tm_hour=2, tm_min=13, tm_sec=14, tm_wday=1, tm_yday=295, tm_isdst=0) >>> time.gmtime(time.time() - 24*60*60) #获取昨天这个时间的struct_time对象 time.struct_time(tm_year=2013, tm_mon=10, tm_mday=21, tm_hour=2, tm_min=13, tm_sec=37, tm_wday=0, tm_yday=294, tm_isdst=0)time.localtime time.localtime与time.gmtime非常类似,也返回一个struct_time对象。但是,localtime表示系统时间,gmtime表示标准格林威治时间 >>> import time >>> time.gmtime() time.struct_time(tm_year=2013, tm_mon=10, tm_mday=22, tm_hour=2, tm_min=52, tm_sec=21, tm_wday=1, tm_yday=295, tm_isdst=0) >>> time.localtime() time.struct_time(tm_year=2013, tm_mon=10, tm_mday=22, tm_hour=10, tm_min=52, tm_sec=28, tm_wday=1, tm_yday=295, tm_isdst=0)time.mktime time.mktime执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数。例如: >>> import time >>> time.mktime(time.localtime()) 1382408265.0 >>> time.time() 1382408266.693491time.strftime time.strftime将日期转换为字符串表示,它的函数原型为:time.strftime(format[, t])。参数format是格式字符串(格式字符串的知识可以参考下面),可选的参数t是一个struct_time对象。
>>> import time >>> time.strftime('%Y-%m-%d %H:%M:%S') '2013-10-21 16:58:43' >>> time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time() - 24 * 60 * 60)) # '2013-10-21 10:53:45' >>> time.strftime('Weekday: %w; Day of the year: %j') 'Weekday: 2; Day of the year: 295'time.strptime 按指定格式解析一个表示时间的字符串,返回struct_time对象。该函数原型为:time.strptime(string, format)(format默认为:'%a %b %d %H:%M:%S %Y'),两个参数都是字符串,下面是一个简单的例子,演示将一个字符串解析为一个struct_time对象 >>> time.strptime('10:53:45 2013-10-21','%H:%M:%S %Y-%m-%d') time.struct_time(tm_year=2013, tm_mon=10, tm_mday=21, tm_hour=10, tm_min=53, tm_sec=45, tm_wday=0, tm_yday=294, tm_isdst=-1)我们来对time模块进行一个总结。根据之前描述,在Python中共有三种表达方式:1)timestamp 2)tuple或者struct_time 3)格式化字符串。 它们之间的转化如图所示: datetime模块 datetime.date:表示日期的类。常用的属性有year, month, day datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond datetime.datetime:表示日期时间 datetime.timedelta:表示时间间隔,即两个时间点之间的长度 datetime.tzinfo:与时区有关的相关信息 >>> import datetime >>> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') '2013-10-21 20:55:38' >>> (datetime.datetime.now() - datetime.timedelta(days = 5)).strftime("%Y-%m-%d") '2013-10-16' >>> d1 = datetime.date(2013,10,21) >>> d2 = datetime.date(2013,8,21) >>> d1 - d2 datetime.timedelta(61)参考:http://qinxuye.me/article/details-about-time-module-in-python 转自:http://blog.linuxeye.com/374.html |