python之time模块

time模块下之只有函数,所以只需要通过time.调用即可,常用的函数有以下几种:

1.time()--return current time in seconds since the Epoch as a float   返回1970年到此时的秒数  #时间戳

 

2.clock()--return CPU time since process start as a float  返回当前程序cpu执行时间

 

3.sleep() -- delay for a number of seconds given as a float  

 

4.localtime() -- convert seconds since Epoch to local time tuple

返回本地time.time_struct格式的时间元组  #格式化时间

print(time.localtime)  #time.struct_time(tm_year=2018, tm_mon=1, tm_mday=20, tm_hour=14, tm_min=37, tm_sec=33, tm_wday=5, tm_yday=20, tm_isdst=0)

 

5.asctime(tuple) -- convert time tuple to string

将格式化时间转化为字符串

print(time.asctime(time.localtime()))  #Sat Jan 20 14:55:13 2018

 

6.ctime() -- convert time in seconds to string

将时间戳转化为字符串

print(time.ctime(time.time()))  #Sat Jan 20 14:59:49 2018

 

7.mktime(tuple) -- convert local time tuple to seconds since Epoch

将格式化时间转化为时间戳

print(time.mktime(time.localtime()))  #1516431848.0

 

8.strftime() -- convert time tuple to string according to format specification

strftime(format[, tuple]) -> string将格式化时间以你指定的格式输出字符串 

   %Y  Year with century as a decimal number.
        %m  Month as a decimal number [01,12].
        %d  Day of the month as a decimal number [01,31].
        %H  Hour (24-hour clock) as a decimal number [00,23].
        %M  Minute as a decimal number [00,59].
        %S  Second as a decimal number [00,61].

print(time.strftime('%Y:%m:%d  %H:%M:%S',time.loclatime))  #2018:01:20  15:16:21

9.strptime(...)
        strptime(string, format) -> struct_time
        Parse a string to a time tuple according to a format specification.

 

 

 


 

posted @ 2018-01-20 15:19  木已成木炭  阅读(147)  评论(0)    收藏  举报