python基礎:datetime模塊-毫秒時間差-微秒時間差


1 datetime

import datetime
access_start = datetime.datetime.now()
access_start_str = access_start.strftime('%Y-%m-%d %H:%M:%S')

access_end = datetime.datetime.now()
access_end_str = access_end.strftime('%Y-%m-%d %H:%M:%S')
access_delta = (access_end-access_start).seconds*1000
import os
import time
hello = '%s-%s' % (time.strftime('%Y%m%d-%H%M%S'), os.urandom(2).encode('hex'))
print(hello)
# 20180522-144343-d6f1

hello = '%s%s' % (time.strftime('%Y%m%d%H%M%S'), os.urandom(2).encode('hex'))
print(hello)
# 20180522144436c412

 

2 簡單封裝以及獲取毫秒級別時間差和微秒級別時間差

from __future__ import division
import datetime
import time



def t():
    t1 = datetime.datetime.now()
    ts1 = t1.strftime('%Y-%m-%d %H:%M:%S')
    return t1,ts1


def millis(t1, t2):
    micros = (t2 - t1).microseconds
    print("micros: ",micros)
    delta = micros/1000
    return delta


def micros(t1, t2):
    delta = (t2-t1).microseconds
    return delta


def access_log(route_rule, headers, t1, t2, delta, db_delta, api_delta):

    access = {}
    access['route'] = route_rule
    access['headers'] = headers
    access['route_start'] = t1
    access['route_end'] = t2
    access['route_delta'] = delta
    access['db_delta'] = db_delta
    access['api_delta'] = api_delta
    return access

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM