cygwin 控制台中的 Python datetime.now 不正确

Python datetime.now in cygwin console is incorrect

如果你能帮我理解为什么: 来自 Cygwin 终端:

这是正确的:

$ date
Wed, Sep  2, 2020 11:19:07 PM

这也是正确的:

$ date --utc
Wed, Sep  2, 2020  9:19:14 PM

时区也正确:

$ echo $TZ
Europe/Zurich

但是当从同一个 Cygwin 终端询问 Python3 中的当地时间时,它会显示:

$ python
Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2020, 9, 2, 22, 20, 4, 339547)

时间不对?它显示 22h20m4s 但它应该是 23h20h4s 如前所示。

我错过了什么?

谢谢

您正在使用 Windows Python 而不是 Cygwin。
cygwin 与其余 Cygwin 程序相匹配

$ python3
Python 3.8.3 (default, May 23 2020, 15:50:53)
[GCC 9.3.0] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2020, 9, 2, 23, 45, 26, 525415)
>>> quit()

 $ date
Wed, Sep  2, 2020 11:45:40 PM

… 此处解释了可能的原因: 简而言之:cygwin 将 TZ 环境变量设置为不是的值 被 msvc libc 正确理解,所以程序是用 visual C++ 构建的 (就像上面使用的官方 non-cygwin python 一样)并且从 cygwin 开始将使用 当地时间的时区不正确。

unset TZ 在启动这样的程序之前应该有所帮助(但当然它会破坏 cygwin-compiled 程序)。不确定 TZ="" python3 是否足够(空变量仍然是变量...)