python的time模块的主要功能:获取时钟时间和cpu处理器运行的时间,以及时间格式的转换。
time.time()返回当前系统时间距离1970-01-01 00:00多少秒
>>> time.time()
1356871542.263558
>>>
time.clock()返回该程序到目前为止cpu运行的时间
>>> time.clock()
0.31
time.sleep()使程序停止的秒数(此时间不记录cpu运行时间)
time.localtime()转换秒显示时间为一个tuple,默认是当前时间
>>> time.localtime()
time.struct_time(tm_year=2012, tm_mon=12, tm_mday=30, tm_hour=20, tm_min=50, tm_sec=46, tm_wday=6, tm_yday=365, tm_isdst=0)
>>> tm=time.localtime()
>>> tm.tm_year
2012
>>> time.localtime(1)
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0)
>>>
time.ctime()与time.localtime()类似,只不过会转换时间为一个字符串,默认也是当前时间
>>> time.ctime()
'Sun Dec 30 20:55:19 2012'
>>> time.ctime(1)
'Thu Jan 1 08:00:01 1970'
>>>
time.mkdtime()与time.localtime()正好相反
>>> time.mktime(time.localtime())
1356872879.0
>>> time.time()
1356872885.819205
>>>
time.strftime()格式化输出时间格式
>>> time.strftime('%Y-%M-%d %H:%m:%S',time.localtime())
'2012-24-30 21:12:27'
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's full month name (e.g., January)
%c locale's date and time (e.g., Thu Mar 3 23:05:25 2005)
%C century; like %Y, except omit last two digits (e.g., 20)
%d day of month (e.g., 01)
%D date; same as %m/%d/%y
%e day of month, space padded; same as %_d
%F full date; same as %Y-%m-%d
%g last two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%r locale
time.strptime()将时间转换成元组模式