DateTime::Format::Strptimeで"PST"をパース
作業メモ。
「2009-06-30 08:37:45 JST」という日時表記を、PerlのDateTime::Format::Strptimeでパースしてみます。
$ cat hoge1.pl #!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; my $fmt = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %T %Z'); my $date = $fmt->parse_datetime($ARGV[0]); print $date->ymd, "\n"; $ perl hoge1.pl '2009-06-30 08:37:45 JST' 2009-06-30
これはうまくいきました。が、タイムゾーンが"PST"だとパースできません。
$ perl hoge1.pl '2009-06-30 08:37:45 PST' Can't call method "ymd" on an undefined value at hoge1.pl line 8.
on_error => 'croak'をつけて調べてみます。
$ cat hoge2.pl #!/usr/bin/perl use strict; use warnings; use DateTime::Format::Strptime; my $fmt = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %T %Z', on_error => 'croak'); my $date = $fmt->parse_datetime($ARGV[0]); print $date->ymd, "\n"; $ perl hoge2.pl '2009-06-30 08:37:45 PST' The timezone 'PST' is ambiguous. at hoge2.pl line 7
PSTはambiguousだそうです。しょうがないのでPSTのオレ定義を突っこんでみます。
$ cat hoge3.pl
#!/usr/bin/perl
use strict;
use warnings;
use DateTime::Format::Strptime;
$DateTime::Format::Strptime::ZONEMAP{PST} = '-0800';
my $fmt = DateTime::Format::Strptime->new(pattern => '%Y-%m-%d %T %Z', on_error => 'croak');
my $date = $fmt->parse_datetime($ARGV[0]);
print $date->ymd, "\n";
$ perl hoge3.pl '2009-06-30 08:37:45 PST'
2009-06-30
めでたしめでたし。
コメント
コメントの投稿
トラックバック
http://emasaka.blog65.fc2.com/tb.php/628-23ad3387
