时间戳中的 T 和 Z 到底是什么意思?

2022-01-13 00:00:00 python datetime timestamp rfc3339 strftime

问题描述

我有这个时间戳值由 Web 服务返回 "2014-09-12T19:34:29Z"

I have this timestamp value being return by a web service "2014-09-12T19:34:29Z"

我知道它的意思是时区,但它到底是什么意思?

I know that it means timezone, but what exactly does it mean?

我正在尝试模拟这个网络服务,那么有没有办法在 python 中使用 strftime 生成这个时间戳?

And I am trying to mock this web service, so is there a way to generate this timestamp using strftime in python?

很抱歉,这很明显,但 Google 并没有提供多大帮助,strftime() 参考页也没有提供帮助.

Sorry if this is painfully obvious, but Google was not very helpful and neither was the strftime() reference page.

我目前正在使用这个:

x.strftime("%Y-%m-%dT%H:%M:%S%Z")
'2015-03-26T10:58:51'


解决方案

T 并不代表任何东西.它只是 ISO 8601 组合日期时间格式 所需的分隔符.您可以将其理解为 Time 的缩写.

The T doesn't really stand for anything. It is just the separator that the ISO 8601 combined date-time format requires. You can read it as an abbreviation for Time.

Z 代表 Zero 时区,因为它从 协调世界时 (UTC).

The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).

这两个字符都只是格式中的静态字母,这就是 datetime.strftime() 方法没有记录它们的原因.您可以使用 QMMonty Python 并且该方法也会将它们原封不动地返回;该方法仅查找以 % 开头的模式,以用来自 datetime 对象的信息替换这些模式.

Both characters are just static letters in the format, which is why they are not documented by the datetime.strftime() method. You could have used Q or M or Monty Python and the method would have returned them unchanged as well; the method only looks for patterns starting with % to replace those with information from the datetime object.

相关文章