____tz_zs
time.time()
返回当前时间的时间戳(1970纪元后经过的浮点秒数)
.
#!/usr/bin/python2.7 # -*- coding:utf-8 -*- """ @author: tz_zs """ import time """ time.time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。 """ print "time.time(): %f " % time.time() # time.time(): 1529379388.075540 print time.localtime( time.time()) # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=19, tm_hour=11, tm_min=36, tm_sec=28, tm_wday=1, tm_yday=170, tm_isdst=0) print time.asctime(time.localtime(time.time())) # Tue Jun 19 11:36:28 2018
.
time.clock()
函数以浮点数计算的秒数返回当前的CPU时间
.
""" time.clock() 函数以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。 这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是"进程时间",它是用秒表示的浮点数(时间戳)。 而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行时间。 (实际上是以WIN32上QueryPerformanceCounter()为基础,它比毫秒表示更为精确) 该函数有两个功能, 在第一次调用的时候,返回的是程序运行的实际时间; 以第二次之后的调用,返回的是自第一次调用后,到这次调用的时间间隔 在win32系统下,这个函数返回的是真实时间(wall time),而在Unix/Linux下返回的是CPU时间。 """ def procedure(): time.sleep(2.5) # measure process time t0 = time.clock() procedure() print time.clock() - t0, "seconds process time" # 3.9e-05 seconds process time # measure wall time t0 = time.time() procedure() print time.time() - t0, "seconds wall time" # 2.50254607201 seconds wall time
.
time.localtime()
作用是格式化时间戳为本地的时间。
.
""" time.localtime() 作用是格式化时间戳为本地的时间。 如果seconds参数未输入,则以当前时间为转换标准。 """ time_array1 = time.localtime() print type(time_array1) # <type 'time.struct_time'> print time_array1 # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=19, tm_hour=11, tm_min=44, tm_sec=1, tm_wday=1, tm_yday=170, tm_isdst=0) time_array2 = time.localtime(1528113599217683 / 1000000) print time_array2 # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=19, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=155, tm_isdst=0) print time.strftime("%Y-%m-%d %H:%M:%S", time_array2) # 2018-06-04 19:59:59.
函数接收时间元组,返回字符串表示的当地时间
.
""" time.strftime() 函数接收时间元组,并返回以可读字符串表示的当地时间,格式由参数format决定。 """ t = (2018, 6, 4, 19, 59, 59, 0, 155, -1) seconds = time.mktime(t) print seconds # 1528113599.0 print time.strftime("%Y-%m-%d %H:%M:%S", t) # 2018-06-04 19:59:59.
time.strptime()
把一个时间字符串解析为时间元组,返回struct_time对象。
.
""" time.strptime() 函数根据指定的格式把一个时间字符串解析为时间元组,返回struct_time对象。 strptime(string, format) -> struct_time """ date_time = time.strptime("2018-06-04 19:59:59", "%Y-%m-%d %H:%M:%S") print date_time # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=19, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=155, tm_isdst=-1) .
time.gmtime()
函数将一个时间戳转换为UTC时区(0时区)的struct_time对象
.
""" time.gmtime() 函数将一个时间戳转换为UTC时区(0时区)的struct_time对象,可选的参数seconds表示从1970-1-1以来的秒数。 其默认值为time.time(),函数返回time.struct_time类型的对象。(struct_time是在time模块中定义的表示时间的对象)。 """ time_time = time.time() print time_time # 1529381729.11 time_gmtime = time.gmtime() print type(time_gmtime) # <type 'time.struct_time'> print time_gmtime # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=19, tm_hour=4, tm_min=15, tm_sec=29, tm_wday=1, tm_yday=170, tm_isdst=0) print time.mktime(time_gmtime) # 1529352929.0 time_gmtime2 = time.gmtime(1528113599217683 / 1000000) print time_gmtime2 # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=11, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=155, tm_isdst=0) print time.mktime(time_gmtime2) # 1528084799.0
.
time.mktime()
它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数。
.
""" time.mktime() 函数执行与gmtime(), localtime()相反的操作,它接收struct_time对象作为参数,返回用秒数来表示时间的浮点数。 如果输入的值不是一个合法的时间,将触发 OverflowError 或 ValueError。 """ t = (2018, 6, 4, 19, 59, 59, 0, 155, -1) seconds = time.mktime(t) print seconds # 1528113599.0 print time.asctime(time.localtime(seconds)) # Mon Jun 4 19:59:59 2018 time_gmtime2 = time.gmtime(1528113599217683 / 1000000) print time_gmtime2 # time.struct_time(tm_year=2018, tm_mon=6, tm_mday=4, tm_hour=11, tm_min=59, tm_sec=59, tm_wday=0, tm_yday=155, tm_isdst=0) print time.mktime(time_gmtime2) # 1528084799.0.
文章来源: Python time