Perl strptime格式与strftime不同

前端之家收集整理的这篇文章主要介绍了Perl strptime格式与strftime不同前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用格式为的日期字符串:

YYYY-MM-DDThh:mm:ss

生成此字符串的strftime格式为%FT%T:

perl -MPOSIX=strftime -E 'say strftime q{%FT%T},localtime;'

但是当我尝试用Time::Piece解析这个日期字符串时:

perl -MPOSIX=strftime -MTime::Piece -E '
  say strftime q{%FT%T},localtime;
  Time::Piece->strptime( strftime(q{%FT%T},localtime),q{%FT%T});
'

我收到了这个错误

2013-11-15T17:32:58
Error parsing time at /usr/local/lib/perl/5.14.2/Time/Piece.pm line 469.

或者如果我试试这个:

perl -MPOSIX=strftime -MTime::Piece -E 'Time::Piece->strptime(strftime(q{%F},q{%F});'

我懂了:

garbage at end of string in strptime: 2013-11-15 at /usr/local/lib/perl/5.14.2/Time/Piece.pm line 469.

在男人说,%F是strptime和strftime的ISO8601日期格式. strptime向后兼容strftime吗?

解决方法

该模块使用自己的strptime实现,它不理解%F.它没有实现自己的strftime.这是一场等待发生的灾难 – 你绝对应该使用来自同一来源的strftime和strptime. Time :: Piece :: strptime看起来像是一个没有广泛可用,标准化的strptime的时代的移民.

既然strptime在POSIX中,我希望POSIX.pm可以导出它,这样你就可以使用系统的strptime了.但显然该模块也未能跟上.有一个单独的POSIX::strptime module,它可以让你的C库的strptime没有干扰.我建议使用它.

猜你在找的Perl相关文章