Python datetime.now in cygwin console is incorrect

纵饮孤独 提交于 2021-02-11 07:53:10

问题


If you could help me understand why: From Cygwin terminal:

This is correct:

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

This is also correct:

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

Timezone is also correct:

$ echo $TZ
Europe/Zurich

But when ask for local time in Python3 from the same Cygwin terminal it shows this:

$ 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)

The hour is wrong? It shows 22h20m4s but it should be 23h20h4s as shown previously.

What am I missing?

Thanks


回答1:


you are using Windows Python and not Cygwin one.
The cygwin one matches with the rest of Cygwin programs

$ 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


来源:https://stackoverflow.com/questions/63713891/python-datetime-now-in-cygwin-console-is-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!