Python的time模块中的常用方法整理

WBOY
Release: 2016-06-10 15:10:28
Original
1304 people have browsed it

在应用程序的开发过程中,难免要跟日期、时间处理打交道。如:记录一个复杂算法的执行时间;网络通信中数据包的延迟等等。Python中提供了time, datetime calendar等模块来处理时间日期,今天对time模块中最常用的几个函数作一个介绍。
time.time

time.time()函数返回从1970年1月1日以来的秒数,这是一个浮点数。
time.sleep

可以通过调用time.sleep来挂起当前的进程。time.sleep接收一个浮点型参数,表示进程挂起的时间。
time.clock

在windows操作系统上,time.clock() 返回第一次调用该方法到现在的秒数,其精确度高于1微秒。可以使用该函数来记录程序执行的时间。下面是一个简单的例子:
 

import time
 
print time.clock() 
#1
time.sleep(2)
print time.clock() 
#2
time.sleep(3)
print time.clock() 
#3
 
#---- result
#3.91111160776e-06
#1.99919151736
#4.99922364435
Copy after login
time.gmtime

该函数原型为:time.gmtime([sec]),可选的参数sec表示从1970-1-1以来的秒数。其默认值为time.time(),函数返回time.struct_time类型的对象。(struct_time是在time模块中定义的表示时间的对象),下面是一个简单的例子:

import time
 
print time.gmtime() 
#获取当前时间的struct_time对象
print time.gmtime(time.time() - 24 * 60 * 60) 
#获取昨天这个时间的struct_time对象
 
#---- result
#time.struct_time(tm_year=2009, tm_mon=6, tm_mday=23, tm_hour=15, tm_min=16, tm_sec=3, tm_wday=1, tm_yday=174, tm_isdst=0)
#time.struct_time(tm_year=2009, tm_mon=6, tm_mday=22, tm_hour=15, tm_min=16, tm_sec=3, tm_wday=0, tm_yday=173, tm_isdst=0)
time.localtime
Copy after login

time.localtime与time.gmtime非常类似,也返回一个struct_time对象,可以把它看作是gmtime()的本地化版本。
time.mktime

time.mktime执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数。例如:

import time
 
#下面两个函数返回相同(或相近)的结果
print time.mktime(time.localtime())
print time.time()
Copy after login
time.strftime

time.strftime将日期转换为字符串表示,它的函数原型为:time.strftime(format[, t])。参数format是格式字符串(格式字符串的知识可以参考:time.strftime),可选的参数t是一个struct_time对象。下面的例子将struct_time对象转换为字符串表示:

import time
 
print time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
print time.strftime('Weekday: %w; Day of the yesr: %j')
 
#---- result
#2009-06-23 15:30:53
#Weekday: 2; Day of the yesr: 174
Copy after login
time.strptime

按指定格式解析一个表示时间的字符串,返回struct_time对象。该函数原型为:time.strptime(string, format),两个参数都是字符串,下面是一个简单的例子,演示将一个字符串解析为一个struct_time对象:

import time
 
print time.strptime('2009-06-23 15:30:53', '%Y-%m-%d %H:%M:%S')
 
#---- result
#time.struct_time(tm_year=2009, tm_mon=6, tm_mday=23, tm_hour=15, tm_min=30, tm_sec=53, tm_wday=1, tm_yday=174, tm_isdst=-1)
Copy after login

以上介绍的方法是time模块中最常用的几个方法,在Python手册中还介绍了其他的方法和属性,如:time.timezone, time.tzname …感兴趣的朋友可以参考Python手册 time 模块。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!