yyyymmddhhmmss到YYYY-MM-DD hh:mm:ss in perl? [英] yyyymmddhhmmss to YYYY-MM-DD hh:mm:ss in perl?
本文介绍了yyyymmddhhmmss到YYYY-MM-DD hh:mm:ss in perl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将yyyymmddhhmmss转换为YYYY-MM-DD的最佳方式是什么?mm:ss和perl?
What is the best way to convert yyyymmddhhmmss to YYYY-MM-DD hh:mm:ss and back in perl?
例如:20130218165601到2013-02 -18 16:56:01回来?
(可以 https://metacpan.org/module/Rose::DateTime
for example: 20130218165601 to 2013-02-18 16:56:01 and back? (can https://metacpan.org/module/Rose::DateTime do it)?
推荐答案
没有regexp >一个模块是这样的。
A module is overkill for this.
# Packed -> ISO
(my $iso_date = $packed_date) =~
s/^(....)(..)(..)(..)(..)(..)\z/$1-$2-$3 $4:$5:$6/s;
# ISO -> Packed
(my $packed_date = $iso_date) =~
s/^(....)-(..)-(..) (..):(..):(..)\z/$1$2$3$4$5$6/s;
Rose :: DateTime 无法按预期解析打包格式,但您可以使用 DateTime :: Format :: Strptime 。
Rose::DateTime cannot parse the "packed" format as intended, but you could use DateTime::Format::Strptime.
use DateTime::Format::Strptime qw( );
my $packed_format = DateTime::Format::Strptime->new(
pattern => '%Y%m%d%H%M%S',
on_error => 'croak',
);
my $iso_format = DateTime::Format::Strptime->new(
pattern => '%Y-%m-%d %H:%M:%S',
on_error => 'croak',
);
# Packed -> ISO
my $iso_date = $iso_format->format_datetime(
$packed_format->parse_datetime($packed_date)
);
# ISO -> Packed
my $packed_date = $packed_format->format_datetime(
$iso_format->parse_datetime($iso_date)
);
这篇关于yyyymmddhhmmss到YYYY-MM-DD hh:mm:ss in perl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文