问题:获取当前时间(以毫秒为单位)在Python中?

如何在Python中获取以毫秒为单位的当前时间?

How can I get the current time in milliseconds in Python?


回答 0

根据我上面的@samplebias的评论,这是我需要做的:

import time
millis = int(round(time.time() * 1000))
print millis

快点 谢谢大家,为您的大脑屁感到抱歉。

要重用:

import time

current_milli_time = lambda: int(round(time.time() * 1000))

然后:

>>> current_milli_time()
1378761833768

For what I needed, here’s what I did, based on @samplebias’ comment above:

import time
millis = int(round(time.time() * 1000))
print millis

Quick’n’easy. Thanks all, sorry for the brain fart.

For reuse:

import time

current_milli_time = lambda: int(round(time.time() * 1000))

Then:

>>> current_milli_time()
1378761833768

回答 1

time.time()可能只提供秒的分辨率,毫秒的首选方法是datetime

from datetime import datetime
dt = datetime.now()
dt.microsecond

time.time() may only give resolution to the second, the preferred approach for milliseconds is datetime.

from datetime import datetime
dt = datetime.now()
dt.microsecond

回答 2

def TimestampMillisec64():
    return int((datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() * 1000) 
def TimestampMillisec64():
    return int((datetime.datetime.utcnow() - datetime.datetime(1970, 1, 1)).total_seconds() * 1000) 

回答 3

3.7版开始,您可以time.time_ns()用来获取从纪元开始经过的纳秒级的时间。所以你可以做

ms = time.time_ns() // 1000000 

以毫秒为单位获取时间作为整数。

From version 3.7 you can use time.time_ns() to get time as passed nano seconds from epoch. So you can do

ms = time.time_ns() // 1000000 

to get time in mili-seconds as integer.


回答 4

只是示例代码:

import time
timestamp = int(time.time()*1000.0)

输出:1534343781311

Just sample code:

import time
timestamp = int(time.time()*1000.0)

Output: 1534343781311


回答 5

另一个解决方案是可以嵌入到自己的utils.py中的函数

import time as time_ #make sure we don't override time
def millis():
    return int(round(time_.time() * 1000))

another solution is the function you can embed into your own utils.py

import time as time_ #make sure we don't override time
def millis():
    return int(round(time_.time() * 1000))

回答 6

如果您想在代码中使用一个简单的方法来返回带有datetime的毫秒数:

from datetime import datetime
from datetime import timedelta

start_time = datetime.now()

# returns the elapsed milliseconds since the start of the program
def millis():
   dt = datetime.now() - start_time
   ms = (dt.days * 24 * 60 * 60 + dt.seconds) * 1000 + dt.microseconds / 1000.0
   return ms

If you want a simple method in your code that returns the milliseconds with datetime:

from datetime import datetime
from datetime import timedelta

start_time = datetime.now()

# returns the elapsed milliseconds since the start of the program
def millis():
   dt = datetime.now() - start_time
   ms = (dt.days * 24 * 60 * 60 + dt.seconds) * 1000 + dt.microseconds / 1000.0
   return ms

回答 7

我发现以毫秒为单位获取当前UTC时间的最简单方法是:

# timeutil.py
import datetime


def get_epochtime_ms():
    return round(datetime.datetime.utcnow().timestamp() * 1000)

# sample.py
import timeutil


timeutil.get_epochtime_ms()

The simpliest way I’ve found to get the current UTC time in milliseconds is:

# timeutil.py
import datetime


def get_epochtime_ms():
    return round(datetime.datetime.utcnow().timestamp() * 1000)

# sample.py
import timeutil


timeutil.get_epochtime_ms()

回答 8

如果您担心测量经过的时间,则应使用单调时钟(python 3)。该时钟不受系统时钟更新的影响,例如,您会看到NTP查询是否调整了系统时间。

>>> import time
>>> millis = round(time.monotonic() * 1000)

它提供了以秒为单位的参考时间,可用于以后进行比较以测量经过的时间。

If you’re concerned about measuring elapsed time, you should use the monotonic clock (python 3). This clock is not affected by system clock updates like you would see if an NTP query adjusted your system time, for example.

>>> import time
>>> millis = round(time.monotonic() * 1000)

It provides a reference time in seconds that can be used to compare later to measure elapsed time.


回答 9

如果您使用我的代码(如下所示),则时间将以秒为单位,然后在十进制后为毫秒。我认为Windows和Unix之间有区别-如果有区别,请发表评论。

from time import time

x = time()
print(x)

我的结果(在Windows上)是:

1576095264.2682993

编辑:没有区别:)谢谢tc0nn

If you use my code (below), the time will appear in seconds, then, after a decimal, milliseconds. I think that there is a difference between Windows and Unix – please comment if there is.

from time import time

x = time()
print(x)

my result (on Windows) was:

1576095264.2682993

EDIT: There is no difference:) Thanks tc0nn


回答 10

这些乘以1000毫秒的乘法对于解决或使某些先决条件可接受可能是不错的选择。它可以用来填补数据库中实际上从未使用过的空白。虽然,对于需要精确定时的实际情况,它最终会失败。我不建议任何人将这种方法用于需要执行操作或在特定时间进行处理的关键任务操作。

例如:在美国,双向ping是30-80毫秒…您不能将其四舍五入并有效地使用它。

我自己的示例要求每秒执行一次任务,这意味着如果我在响应第一个任务后将其四舍五入,仍然会导致处理时间乘以每个主循环周期。最终每60秒调用一次函数。每天大约是1440 ..不太准确。

对于那些希望寻求更准确推理而不是解决从未真正使用过数据库缺口的人们来说,这只是一个想法。

These multiplications to 1000 for milliseconds may be decent for solving or making some prerequisite acceptable. It could be used to fill a gap in your database which doesn’t really ever use it. Although, for real situations which require precise timing it would ultimately fail. I wouldn’t suggest anyone use this method for mission-critical operations which require actions, or processing at specific timings.

For example: round-trip pings being 30-80ms in the USA… You couldn’t just round that up and use it efficiently.

My own example requires tasks at every second which means if I rounded up after the first tasks responded I would still incur the processing time multiplied every main loop cycle. This ended up being a total function call every 60 seconds. that’s ~1440 a day.. not too accurate.

Just a thought for people looking for more accurate reasoning beyond solving a database gap which never really uses it.


回答 11

这是使用datetime适用于Python 3 的模块的另一种解决方案。

datetime.datetime.timestamp(datetime.datetime.now())

Just another solution using the datetime module for Python 3.

datetime.datetime.timestamp(datetime.datetime.now())

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。