本文介绍了使用 perl 格式化字符串和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想转换(使用 perl)
I would like to convert (using perl)
05\/26\/2013 06:09:47
到 26-05-2013 06:09:47
另外,我如何将上述内容更改为 GMT 日期和时间?
Also how can i change the above to GMT date and time?
推荐答案
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,那么以下内容会比较轻松,但可能有点神秘.
If we're specifically dealing with local and UTC/GMT, then the following is lighter, though perhaps a bit more cryptic.
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)));
这篇关于使用 perl 格式化字符串和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!