[英]Timestamp to UTC ISO 8601 date
This 这个
import datetime
datetime.datetime.fromtimestamp(1501545600).isoformat()
should output 2017-08-01T00:00:00Z
or 2017-08-01T00:00:00
but I get 应该输出
2017-08-01T00:00:00Z
或2017-08-01T00:00:00
但我得到
2017-08-01T02:00:00
instead, probably because of local timezone. 相反,可能是因为当地时区。
How to make .isoformat()
output an UTC time instead, ie 2017-08-01T00:00:00
? 如何使
.isoformat()
改为输出UTC时间,即2017-08-01T00:00:00
?
If possible I'd like to use only datetime
, and no other third party library like arrow
or dateutils
. 如果可能的话,我只想使用
datetime
,不要使用任何其他第三方库,例如arrow
或dateutils
。
Use utcfromtimestamp()
rather than fromtimestamp()
. 使用
utcfromtimestamp()
而不是fromtimestamp()
。 From the docs : 从文档 :
Return the UTC datetime corresponding to the POSIX timestamp, with tzinfo None.
返回与POSIX时间戳相对应的UTC日期时间,并使用tzinfo None。
With no tzinfo specified in fromtimestamp()
: 在
fromtimestamp()
未指定tzinfo:
If optional argument tz is None or not specified, the timestamp is converted to the platform's local date and time, and the returned datetime object is naive.
如果可选参数tz为None或未指定,则时间戳将转换为平台的本地日期和时间,并且返回的datetime对象是天真的。
Which doesn't seem to be what you want. 似乎不是您想要的。
Shouldn't it be datetime.utcfromtimestamp()
then? 那不应该是
datetime.utcfromtimestamp()
吗?
From python docs: 从python文档:
classmethod datetime.utcfromtimestamp(timestamp)¶ Return the UTC datetime corresponding to the POSIX timestamp, with tzinfo None.
classmethod datetime.utcfromtimestamp(timestamp)¶返回与POSIX时间戳相对应的UTC日期时间,其中tzinfo为None。 This may raise ValueError, if the timestamp is out of the range of values supported by the platform C gmtime() function.
如果时间戳超出平台C gmtime()函数支持的值范围,则可能会引发ValueError。 It's common for this to be restricted to years in 1970 through 2038.
通常将其限制在1970年到2038年之间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.