linux - Perl 的 DateTime::Format::Strptime 中的 parse_datetime 方法无法解析时区名称

标签 linux ubuntu strptime

我有一台装有 ubuntu 12.04 的笔记本电脑。 在控制台执行 date 命令结果如下:

$ date
Thu May  8 15:28:12 WIB 2014

下面的 perl 脚本将运行良好。

#!/usr/bin/perl

use DateTime::Format::Strptime;

$parser = DateTime::Format::Strptime->new( pattern => "%a %b %d %H:%M:%S %Y %Z");

$date = "Fri Sep 20 08:22:42 2013 WIB";
$dateimap = $parser->parse_datetime($date);
$date = $dateimap->strftime("%d-%b-%Y %H:%M:%S %z");
print "$date\n";

$date = "Fri Jan  8 16:49:34 2010 WIT";
$dateimap = $parser->parse_datetime($date);
$date = $dateimap->strftime("%d-%b-%Y %H:%M:%S %z");
print "$date\n";

结果是

20-Sep-2013 08:22:42 +0700
08-Jan-2010 16:49:34 +0900

但是,为什么时区名称“WIT”会转换为时区“+0900”? AFAIK,WIT 是印度尼西亚西部时间。恕我直言,它应该有时区“+0700”而不是“+0900”。

另一台计算机正在运行 CentOS 5.9。

在CentOS上执行date命令结果:

$ date
Thu May  8 15:38:24 WIT 2014

但是上面的 perl 脚本的执行结果是这样的:

20-Sep-2013 08:22:42 +0700
Can't call method "strftime" on an undefined value at strptime.pl line 14.

实际上parse_datetime方法无法解析包含“WIT”时区的日期。 返回值 $dateimap 为空或 undef。

CentOS 已设置为亚洲/ Jakarta 本地时间。

$ ls -l /etc/localtime 
lrwxrwxrwx 1 root root 32 Sep 23  2013 /etc/localtime -> /usr/share/zoneinfo/Asia/Jakarta

有什么建议吗?

谢谢。

最佳答案

实际上,问题的发生是因为 CentOS 5.9 的 DateTime::Format::Strptime 模块的版本是 1.2000,而 ubuntu 12.04 的版本是 1.54。

使用旧版本的 DateTime::Format::Strptime 时出现另一个问题。

$ perl -e '
> use DateTime::Format::Strptime;
> $parser   = DateTime::Format::Strptime->new( pattern => "%a %b %d %H:%M:%S %Y");
> $datembox = "Wed Jan  1 06:42:18 2014 WIT";
> $date = $parser->parse_datetime($datembox);
> print "$date\n";'

$ 

如果我们删除变量 $datembox 处的双空格。

$ perl -e '
> use DateTime::Format::Strptime;
> $parser   = DateTime::Format::Strptime->new( pattern => "%a %b %d %H:%M:%S %Y");
> $datembox = "Wed Jan  1 06:42:18 2014 WIT";
> $datembox =~ s/[\s]+/ /g;
> $date = $parser->parse_datetime($datembox);
> print "$date\n";'
2014-01-01T06:42:18
$

关于linux - Perl 的 DateTime::Format::Strptime 中的 parse_datetime 方法无法解析时区名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23536606/

相关文章:

linux - 将 SDL2 库与 pkg-config 链接

r - dplyr 不按日期对数据进行分组

python - 使用 datetime.strptime() 时出现问题

python - 在字符串列表上使用 strptime()。不能使用循环

java - 在 ubuntu 中解析不同的 java 和 javac 版本

linux - bash 没有 $(函数参数) 的参数

linux - 为什么几个信号编号体系结构依赖于 Linux?

linux - Bash脚本,请帮我制作一下

linux - 在 Linux 中保存钩子(Hook)文件

ubuntu - 由于端口冲突,无法运行 kubectl,但也无法通过在线解决方案修复它们