EDIT2:这个问题假设了一个POSIX平台,它与Glibc链接在一起。
在我的系统中,使用使用Python库的%z格式化指令的往返转换不能解析ISO8601格式化时间戳的偏移部分。这个片段:
import time
time.daylight = 0
fmt = "%Y-%m-%dT%H:%M:%SZ%z"
a=time.gmtime()
b=time.strftime(fmt, a)
c=time.strptime(b, fmt)
d=time.strftime(fmt, c)
print ("»»»»", a == c, b == d)
print ("»»»»", a.tm_zone, b)
print ("»»»»", c.tm_zone, d)产出:
»»»» False False
»»»» GMT 2018-02-16T09:26:34Z+0000
»»»» None 2018-02-16T09:26:34Z而预期的产出将是
»»»» True True
»»»» GMT 2018-02-16T09:26:34Z+0000
»»»» GMT 2018-02-16T09:26:34Z+0000我如何让%z尊重这种抵消?
编辑: Glibc可以被宣告无罪,这是通过这个C模拟:
#define _XOPEN_SOURCE
#define _DEFAULT_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* 2018-02-16T09:59:21Z+0000 */
#define ISO8601_FMT "%Y-%m-%dT%H:%M:%SZ%z"
int main () {
const time_t t0 = time (NULL);
struct tm a;
char b [27];
struct tm c;
char d [27];
(void)setenv ("TZ", "UTC", 1);
tzset ();
daylight = 0;
(void)gmtime_r (&t0, &a); /* a=time.gmtime () */
(void)strftime (b, sizeof(b), ISO8601_FMT, &a); /* b=time.strftime (fmt, a) */
(void)strptime (b, ISO8601_FMT, &c); /* c=time.strptime (b, fmt) */
(void)strftime (d, sizeof(d), ISO8601_FMT, &c); /* d=time.strftime (fmt, c) */
printf ("»»»» b ?= d %s\n", strcmp (b, d) == 0 ? "yep" : "hell, no");
printf ("»»»» %d <%s> %s\n", a.tm_isdst, a.tm_zone, b);
printf ("»»»» %d <%s> %s\n", c.tm_isdst, c.tm_zone, d);
}哪种输出
»»»» b ?= d yep
»»»» 0 <GMT> 2018-02-16T10:28:18Z+0000
»»»» 0 <(null)> 2018-02-16T10:28:18Z+0000发布于 2018-02-16 09:51:22
对于"time.gmtime()“,您自然会得到UTC的时间,因此偏移量总是+0000,因此输出字符串"2018-02-16T09:26:34Z”对ISO8601是正确的。如果您绝对想要"+0000“,请手动添加它,因为它总是相同的:
d = time.strftime(fmt, c) + '+0000'发布于 2018-02-16 10:01:18
我并不是假装有办法根据时区来产生适当的时差,但我可以解释这里发生了什么。
正如Python timezone '%z' directive for datetime.strptime() not available答案中所暗示的那样:
strptime是在纯python中实现的,因此它具有恒定的行为。strftime依赖于它所链接的平台/C库。在我的系统(WindowsPython3.4)上,%z返回与%Z (“巴黎,马德里”)相同的内容。因此,当strptime试图将其解析为数字时,它将失败。你的密码给了我:
ValueError: time data '2018-02-16T10:00:49ZParis, Madrid' does not match format '%Y-%m-%dT%H:%M:%SZ%z'系统依赖于生成,而不是解析。
这种不对称解释了奇怪的行为。
https://stackoverflow.com/questions/48823828
复制相似问题