【发布时间】:2018-02-02 16:13:56
【问题描述】:
我的行为与 datetime.strftime() 的输出不一致。
我使用的是 macOS Sierra 10.12.6 和 Python 3.6.2。我有一个Python程序,名为wut.py,即
#!/usr/bin/python
from datetime import datetime
import locale
if __name__ == "__main__":
print(locale.getlocale())
print(datetime.today().strftime('%c'))
在终端中,我写
$ date
Gio 24 Ago 2017 18:54:01 CEST
这是意大利人写日期的方式。那么
$ python3 ~/Desktop/wut.py
('it_IT', 'UTF-8')
Thu Aug 24 18:54:03 2017
日期和时间是在另一个语言环境中写入的。这是怎么回事?这是一个错误还是有任何原因导致这种行为?
我很困惑,因为 Python 文档提到 %c 指的是“区域设置的适当日期和时间表示。”,所以我认为系统的区域设置是“适当的”;)(参见https://docs.python.org/3/library/datetime.html,秒8.1.8)
【问题讨论】:
-
我知道这不能回答问题,但你说你正在使用 python3 但你的 shebang 调用 python 2... 应该是 #!/usr/bin/python3
-
你能看到C
strftime给你什么吗?我认为 Python 最终会委托给 Cstrftime来处理这个问题。 -
@AllenMoh 谢谢,你说得对,我已经修好了。无论如何,它应该不会改变上面的例子
-
如果您“显式”设置语言环境以使用带有
locale.setlocale(locale.LC_ALL, '')的系统语言环境,您将获得预期的意大利语输出。我不知道为什么这似乎是必要的。 -
@user2357112 我怎样才能看到 C 函数给了我什么?
man strftime只是说%c is replaced by national representation of time and date.和the strftime() function uses the current locale
标签: python python-3.x macos-sierra strftime