python time 模块

2018-10-11  本文已影响0人  智勇双全的小六

time 模块主要包含两种时间,一种是 UTC 时间,一种是本地时间。
UTC 时间可以通过 gmtime 调用。

import time
time.gmtime()
>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=11, tm_hour=7, tm_min=58, tm_sec=38, tm_wday=3, tm_yday=284, tm_isdst=0)

本地时间可以通过 localtime 调用。

import time
time.localtime()
>>> time.struct_time(tm_year=2018, tm_mon=10, tm_mday=11, tm_hour=15, tm_min=59, tm_sec=34, tm_wday=3, tm_yday=284, tm_isdst=0)

相关函数

import time
time.time()
>>>  1539244967.6424007
import time

def procedure():
    time.sleep(2.5)

def calc_procedure():
    t0 = time.clock()
    procedure()
    cost_time = time.clock() - t0
    print(cost_time)

calc_procedure()
>>> 2.4999686415654168
import time
time.asctime(time.gmtime())
>>> 'Thu Oct 11 08:16:38 2018'
time.asctime()
>>> Thu Oct 11 16:14:08 2018
time.ctime()
>>> 'Thu Oct 11 16:14:08 2018'
time.mktime(time.localtime())
>>> 1539246152.0
# 传1个参数时默认是本地时间
time.strftime("%Y/%m/%d %H:%M-%S")
>>> '2018/10/11 16:24-24'
# 第2个参数可以是指定的时间元组
time.strftime("%Y/%m/%d %H:%M-%S", time.gmtime())
>>> '2018/10/11 08:25-09'
time.strptime("2018/10/11 08:25-09", "%Y/%m/%d %H:%M-%S")
上一篇 下一篇

猜你喜欢

热点阅读