以 Unix 时间戳格式获取当前 GMT 时间的最简单方法是什么? [英] What is the easiest way to get current GMT time in Unix timestamp format?

查看:37
本文介绍了以 Unix 时间戳格式获取当前 GMT 时间的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Python provides different packages (datetime, time, calendar) as can be seen here in 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())

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

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

推荐答案

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

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

import time

time.time()

输出:

1369550494.884832

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

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

这篇关于以 Unix 时间戳格式获取当前 GMT 时间的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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