0

I would like to convert (using perl)

05\/26\/2013 06:09:47 to 26-05-2013 06:09:47

Also how can i change the above to GMT date and time?

4

1 回答 1

6
use DateTime::Format::Strptime qw( );

my $src_format = DateTime::Format::Strptime->new(
   pattern   => '%m\\/%d\\/%Y %H:%M:%S',
   time_zone => 'local',   # or America/New_York
   on_error  => 'croak',
);

my $dst_format = DateTime::Format::Strptime->new(
   pattern   => '%d-%m-%Y %H:%M:%S',
);

my $dt = $src_format->parse_datetime('05\\/26\\/2013 06:09:47');
$dt->set_time_zone('GMT');
say $dst_format->format_datetime($dt);

如果我们专门处理本地和 UTC/GMT,那么以下内容会更轻松,尽管可能更神秘一些。

use POSIX       qw( strftime );
use Time::Local qw( timelocal );

my ($m,$d,$Y, $H,$M,$S) =
      '05\\/26\\/2013 06:09:47' =~
         m{^(\d+)\\/(\d+)\\/(\d+) (\d+):(\d+):(\d+)\z}
   or die;
say strftime('%d-%m-%Y %H:%M:%S', gmtime(timelocal($S,$M,$H, $d,$m-1,$Y-1900)));
于 2013-06-06T05:27:14.587 回答