sds*_*sds 5 perl datetime duration datediff
我如何在 Perl 中漂亮地打印持续时间?
到目前为止我唯一能想到的是
my $interval = 1351521657387 - 1351515910623; # milliseconds
my $duration = DateTime::Duration->new(
seconds => POSIX::floor($interval/1000) ,
nanoseconds => 1000000 * ($interval % 1000),
);
my $df = DateTime::Format::Duration->new(
pattern => '%Y years, %m months, %e days, ' .
'%H hours, %M minutes, %S seconds, %N nanoseconds',
normalize => 1,
);
print $df->format_duration($duration);
Run Code Online (Sandbox Code Playgroud)
这导致
0 years, 00 months, 0 days, 01 hours, 35 minutes, 46 seconds, 764000000 nanoseconds
Run Code Online (Sandbox Code Playgroud)
这对我来说没有好处,原因如下:
pattern” (如果下次我确实需要几年怎么办?)您可以根据某些值是否为“true”来动态构建模式。
...
push @pattern, '%Y years' if $duration->year;
push @pattern, '%m months' if $duration->month;
...
my $df = DateTime::Format::Duration->new(
pattern => join(', ', @pattern),
normalize => 1,
);
print $df->format_duration($duration);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3240 次 |
| 最近记录: |