如何以毫秒的粒度创建Python时间戳? [英] How can I create a Python timestamp with millisecond granularity?

查看:177
本文介绍了如何以毫秒的粒度创建Python时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从历元起,我需要一个毫秒(ms)的单个时间戳。这不应该是困难的,我相信我只是错过了一些 datetime 或类似的方法。

I need a single timestamp of milliseconds (ms) since epoch. This should not be hard, I am sure I am just missing some method of datetime or something similar.

实际上微秒(μs)粒度也很好。我只需要第1/10秒的时间。

Actually microsecond (µs) granularity is fine too. I just need sub 1/10th second timing.

示例。我有一个每750毫秒发生的事件,让我们来看看它是否打开或关闭。我需要记录每个检查和结果并稍后进行审查,因此我的日志需要如下所示:

Example. I have an event that happens every 750 ms, lets say it checks to see if a light is on or off. I need to record each check and result and review it later so my log needs to look like this:

...00250 Light is on
...01000 Light is off
...01750 Light is on
...02500 Light is on

如果我只有完整的第二粒度,我的日志将如下所示:

If I only have full second granularity my log would look like this:

...00 Light is on
...01 Light is off
...01 Light is on
...02 Light is on

不够准确。

推荐答案

import time
time.time() * 1000

其中1000是每秒毫秒。如果你想要的是从时代开始的百分之一秒,乘以100。

where 1000 is milliseconds per second. If all you want is hundredths of a second since the epoch, multiply by 100.

这篇关于如何以毫秒的粒度创建Python时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆