Python 获取本地时间戳(包含毫秒)

Java 获取时间戳很简单,但是在使用 Python 的时候不能一次性全部取干净(没有毫秒),尴尬。
下面是一个 demo,如何通过 Python 获取一个完整的时间戳。

import time
def get_time_stamp():
    ct = time.time()
    local_time = time.localtime(ct)
    data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
    data_secs = (ct - long(ct)) * 1000
    time_stamp = "%s.%03d" % (data_head, data_secs)
    return time_stamp

实际上 Python 库里面有很多类似的实现。上面的代码参考了 logging 库

你可能感兴趣的:(Python,python,time)