Comment detail
文字列型日時ののN秒後時間取得 (Nested Flatten)しまった、お題は「関数を作れ」だった。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | use strict;
use warnings;
use DateTime;
use DateTime::Format::DateParse;
print DateEx(@ARGV),$/;
sub DateEx
{
(@_>=2 ? DateTime::Format::DateParse->parse_datetime(shift)
: DateTime->now(time_zone => q{Asia/Tokyo}))
->add(seconds => shift||0)->iso8601;
}
|
Perlにまでパーサっがあったのですね。。。 僕が妥協した時はPOSIXを使ったべた書きでした。 # ほとんどCとかわりません(笑
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #!/usr/bin/env perl
use strict;
use POSIX qw/strftime mktime/;
sub DateEx($$)
{
my $outDate = '';
my @t = ($_[0] =~ m/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/);
$t[0] -= 1900; $t[1] -= 1; @t = reverse @t;
$t[0] += $_[1];
mktime( @t );
return strftime("%Y%m%d%H%M%S", @t);
}
print DateEx("20080827235925",40) . "\n";
__END__
#EOF
|





turugina
#7514()
[
Perl
]
Rating-1/1=-1.00
see: DateTime::Format::DateParse
Rating-1/1=-1.00-0+
1 reply [ reply ]