python如何获取更多时间戳精度
调度任务中常常要用到精确的时间,如果获取的13位时间戳不够准确的话,会造成数据的重复或者缺失
通常的不准确的13位时间戳的获取方式(秒*1000)
import time millis = int(round(time.time() * 1000)) print millis
准确的13位时间戳获取(微妙/1000)
# -*- coding: utf-8 -*- import datetime import time """ 获取精确毫秒数时间戳、 """ def get_seconds(): datetime_object = datetime.datetime.now() now_timetuple = datetime_object.timetuple() now_second = time.mktime(now_timetuple) mow_millisecond = long(now_second*1000 + datetime_object.microsecond/1000) print "timetuple-- "+ str(now_timetuple) print "datimeobject-- " + str(datetime_object) print "second-- " + str(now_second) print "millisecond-- " + str(mow_millisecond) if __name__ == "__main__": get_seconds()
结果
timetuple-- time.struct_time(tm_year=2018, tm_mon=12, tm_mday=3, tm_hour=11, tm_min=55, tm_sec=6, tm_wday=0, tm_yday=337, tm_isdst=-1) datimeobject-- 2018-12-03 11:55:06.254679 second-- 1543809306.0 millisecond-- 1543809306254
推荐学习《Python教程》!
来源:PY学习网:原文地址:https://www.py.cn/article.html