1 datetime
import datetimeaccess_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 osimport timehello = '%s-%s' % (time.strftime('%Y%m%d-%H%M%S'), os.urandom(2).encode('hex'))print(hello)# 20180522-144343-d6f1hello = '%s%s' % (time.strftime('%Y%m%d%H%M%S'), os.urandom(2).encode('hex'))print(hello)# 20180522144436c412
2 简单封装以及获取毫秒级别时间差和微秒级别时间差
from __future__ import divisionimport datetimeimport timedef t(): t1 = datetime.datetime.now() ts1 = t1.strftime('%Y-%m-%d %H:%M:%S') return t1,ts1def millis(t1, t2): micros = (t2 - t1).microseconds print("micros: ",micros) delta = micros/1000 return deltadef micros(t1, t2): delta = (t2-t1).microseconds return deltadef 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