标签:
time模块
在Python中,通常有这几种方式来表示时间:
1 import time 2 3 # 1 time() :返回当前时间的时间戳 4 time.time() #1473835903.0278263 5 6 #---------------------------------------------------------- 7 8 # 2 localtime([secs]) 9 # 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。 10 time.localtime() # time.struct_time(tm_year=2016, tm_mon=9, tm_mday=14, tm_hour=14, tm_min=53, tm_sec=18, tm_wday=2, tm_yday=258, tm_isdst=0) 11 time.localtime(1473835903.0278263) 12 13 #---------------------------------------------------------- 14 15 # 3 gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。 16 17 #---------------------------------------------------------- 18 19 # 4 mktime(t) : 将一个struct_time转化为时间戳。 20 print(time.mktime(time.localtime()))#1473836098.0 21 22 #---------------------------------------------------------- 23 24 # 5 asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:‘Sun Jun 20 23:21:05 1993‘。 25 # 如果没有参数,将会将time.localtime()作为参数传入。 26 print(time.asctime())#Wed Sep 14 14:55:39 2016 27 28 #---------------------------------------------------------- 29 30 # 6 ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为 31 # None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。 32 print(time.ctime()) #Wed Sep 14 14:55:39 2016 33 34 print(time.ctime(time.time())) # Wed Sep 14 14:55:39 2016 35 36 # 7 strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和 37 # time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个 38 # 元素越界,ValueError的错误将会被抛出。 39 print(time.strftime("%Y-%m-%d %X", time.localtime())) # 2016-09-14 14:58:12 40 41 # 8 time.strptime(string[, format]) 42 # 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。 43 print(time.strptime(‘2016-09-14 14:58:12‘, ‘%Y-%m-%d %X‘)) 44 45 # time.struct_time(tm_year=2016, tm_mon=9, tm_mday=14, tm_hour=14, tm_min=58, tm_sec=12, tm_wday=2, tm_yday=258, tm_isdst=-1) 46 47 #在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。 48 49 50 # 9 sleep(secs) 51 # 线程推迟指定的时间运行,单位为秒。 52 53 # 10 clock() 54 # 这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。 55 # 而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行 56 # 时间,即两次时间差。
标签:
原文地址:http://www.cnblogs.com/bw13/p/5872286.html