Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows中的Python时间格式不同_Python_Cross Platform - Fatal编程技术网

Windows中的Python时间格式不同

Windows中的Python时间格式不同,python,cross-platform,Python,Cross Platform,我可能错过了一个我应该适应的明显的平台差异,但我在尝试使用时间格式(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("

我可能错过了一个我应该适应的明显的平台差异,但我在尝试使用时间格式(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.strftime(“%a,%d-%b-%Y%T GMT”,time.gmtime()) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 ValueError:无效的格式字符串
从time.gmtime()返回的元组看起来是相同的,所以我不完全确定需要更改什么。

通常,您会发现python
time.strftime()
支持与它运行的平台(或者更具体地说是该平台的libc)相同的格式说明符集。然而,只有其中的一个子集是可移植的。请参阅以获取列表。引用文件:

某些平台上可能支持其他指令,但仅限于 这里列出的这些术语具有ANSI C标准化的含义


在这种情况下,
%T
可以替换为
%H:%M:%S

我没有看到-must不能用于/实现Windows平台中列出的
%T
格式指令(我刚才尝试了Python v2.7.3和3.2.3)

我发现我有这个问题,因为MacOS支持
%D“
,我不得不用
“%m/%d/%Y”
来替换它,使它能够跨平台运行。一般来说,组合像
%m
这样的“简单”格式字符串似乎比使用像
%T
%D
这样的“聚合”字符串更安全,以确保,您在这两个平台上使用的Python版本完全相同?下面是Windows strftime支持的格式字符列表:一个小问题:Linux/Posix/Unix
strftime
似乎会自动忽略[某些]无效格式,这会导致Windows致命异常。