为什么Perl的POSIX :: strftime%F说明符不适用于Windows?

时间:2013-09-24 00:03:21

标签: windows perl posix

我是解析Apple属性列表格式的Mac::PropertyList模块的作者。我将其设计为跨平台工作,但我遇到了POSIXstrftime函数的问题,我使用该函数以Apple指定的格式创建日期字段。

use POSIX; 
print 'Epoch is: ' . POSIX::strftime( "%FT%H:%M:%SZ\n", gmtime(978307200) );

在类似unix的平台上,包括darwin,这个调用产生了正确的日期:

2013-09-23T12:34:56Z

在Windows上,它会生成:

T12:34:56Z

报告为CPAN RT #83460。 Windows在这里发生了什么?

1 个答案:

答案 0 :(得分:15)

当我写这个问题并研究一切时,我找到了自己的答案。我不希望所有这些工作都浪费掉。

我使用的%F格式不可移植,而POSIX模块在​​其strftime条目中显示:

If you want your code to be portable, your format ("fmt")
argument should use only the conversion specifiers defined by
the ANSI C standard (C89, to play safe).  These are
"aAbBcdHIjmMpSUwWxXyYZ%".

%F真的是%Y-%m-%d,所以我应该使用它。

我的特殊问题是,我知道POSIX模块告诉您可以使用哪些格式说明符可移植,但我仍然需要查看strftime手册页以查看它们的作用。在查看手册页时,我忘记检查哪些是可移植的。