Python 以 Unix 时间戳格式获取当前 GMT 时间的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16755394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:34:28  来源:igfitidea点击:

What is the easiest way to get current GMT time in Unix timestamp format?

pythontimeunix-timestamp

提问by Cory

Python provides different packages (datetime, time, calendar) as can be seen herein order to deal with time. I made a big mistake by using the following to get current GMT time time.mktime(datetime.datetime.utcnow().timetuple())

Python 提供了不同的包(datetime, time, calendar),可以在此处看到以处理时间。通过使用以下内容获取当前 GMT 时间,我犯了一个大错误time.mktime(datetime.datetime.utcnow().timetuple())

What is a simple way to get current GMT time in Unix timestamp?

在 Unix 时间戳中获取当前 GMT 时间的简单方法是什么?

回答by Nick Caplinger

Does this help?

这有帮助吗?

from datetime import datetime
import calendar

d = datetime.utcnow()
unixtime = calendar.timegm(d.utctimetuple())
print unixtime

How to convert Python UTC datetime object to UNIX timestamp

如何将 Python UTC 日期时间对象转换为 UNIX 时间戳

回答by Edmond Burnett

I would use time.time() to get a timestamp in seconds since the epoch.

我会使用 time.time() 来获取自纪元以来以秒为单位的时间戳。

import time

time.time()

Output:

输出:

1369550494.884832

For the standard CPython implementation on most platforms this will return a UTC value.

对于大多数平台上的标准 CPython 实现,这将返回一个 UTC 值。

回答by vschmidt

I like this method:

我喜欢这个方法:

import datetime, time

dts = datetime.datetime.utcnow()
epochtime = round(time.mktime(dts.timetuple()) + dts.microsecond/1e6)

The other methods posted here are either not guaranteed to give you UTC on all platforms or only report whole seconds. If you want full resolution, this works, to the micro-second.

此处发布的其他方法要么不能保证在所有平台上都能为您提供 UTC,要么只能报告整秒。如果你想要全分辨率,这可以工作到微秒。

回答by Maresh

Or just simply using the datetime standard module

或者只是简单地使用 datetime 标准模块

In [2]: from datetime import timezone, datetime
   ...: int(datetime.now(tz=timezone.utc).timestamp() * 1000)
   ...: 
Out[2]: 1514901741720

You can truncate or multiply depending on the resolution you want. This example is outputting millis.

您可以根据所需的分辨率进行截断或相乘。此示例输出毫秒。

If you want a proper Unixtimestamp (in seconds) remove the * 1000

如果您想要一个正确的Unix时间戳(以秒为单位),请删除* 1000

回答by Max

import time

int(time.time()) 

Output:

输出:

1521462189

回答by 2ni

from datetime import datetime as dt
dt.utcnow().strftime("%s")

Output:

输出:

1544524990

回答by Chandan Sharma

#First Example:
from datetime import datetime, timezone    
timstamp1 =int(datetime.now(tz=timezone.utc).timestamp() * 1000)
print(timstamp1)

Output: 1572878043380

输出:1572878043380

#second example:
import time
timstamp2 =int(time.time())
print(timstamp2)

Output: 1572878043

输出:1572878043

  • Here, we can see the first example gives more accurate time than second one.
  • Here I am using the first one.
  • 在这里,我们可以看到第一个示例给出的时间比第二个更准确。
  • 这里我使用的是第一个。

回答by MikA

python2and python3

蟒蛇2蟒蛇3

it is good to use time module

使用时间模块很好

import time
int(time.time())

1573708436

1573708436

you can also use datetime module, but when you use strftime('%s'), but strftime convert time to your local time!

您也可以使用 datetime 模块,但是当您使用 strftime('%s') 时,但 strftime 将时间转换为您的本地时间!

python2

蟒蛇2

from datetime import datetime
datetime.utcnow().strftime('%s')

python3

蟒蛇3

from datetime import datetime
datetime.utcnow().timestamp()

回答by usernamenumber

At least in python3, this works:

至少在python3中,这是有效的:

>>> datetime.strftime(datetime.utcnow(), "%s")
'1587503279'