Windows中的Python时间格式不同 [英] Python time formatting different in Windows
问题描述
我可能错过了我应该适应的明显平台差异,但是在尝试创建时间格式(Python2.7)时遇到了这个问题……
I'm probably missing an obvious platform difference that I should be accommodating but I'm getting this when trying to do a time format (Python2.7)...
在Linux环境中:
>>> import time
>>> time.strftime("%a, %d-%b-%Y %T GMT", time.gmtime())
'Tue, 29-May-2012 21:42:04 GMT'
在Windows中:
>>> import time
>>> time.strftime("%a, %d-%b-%Y %T GMT", time.gmtime())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format string
从time.gmtime()返回的元组似乎是一样,所以我不确定是否需要更改。
The tuples returned from time.gmtime() appear to be the same, so I'm not completely sure what I need to change.
推荐答案
通常,您会发现python time.strftime()
支持与其运行的平台(或该平台的libc)相同的格式说明符集。但是,其中只有一部分是可移植的。有关列表,请参见 http://docs.python.org/library/time.html 。要引用文档,请执行以下操作:
In general, you'll find that python time.strftime()
supports the same set of format specifiers as the platform (or that platform's libc to be more specific) it runs on. However, only a subset of these is portable. See http://docs.python.org/library/time.html for a list. To quote the docs:
某些平台可能支持其他指令,但此处列出的仅
具有标准化的含义
Additional directives may be supported on certain platforms, but only the ones listed here have a meaning standardized by ANSI C.
在这种情况下,%T
可以替换为%H:%M:%S
。
In this case, %T
can be replaced by %H:%M:%S
.
这篇关于Windows中的Python时间格式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!