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);
その結果、
0 years, 00 months, 0 days, 01 hours, 35 minutes, 46 seconds, 764000000 nanoseconds
これは、次の理由から私にとっては良くありません。
- " 0 years " (スペースの浪費) を見たくない &c と " %Y years "を削除したくない
pattern
(次に年が必要な場合はどうすればよいですか?) - 私の精度はミリ秒にすぎないことを事前に知っています。ナノ秒の部分に6つのゼロを見たくありません。
- 私は、精度/機械可読性よりも、かわいらしさ/コンパクトさ/人間の可読性に関心があります。つまり、「 1.2 年」または「3.22 か月」または「7.88 日」または「5.7 時間」または「75.5 分」(または「1.26 時間」、あなたにとってより良いと思われるもの) または「24.7 秒」のようなものを見たいと思っています。または " 133.7 ミリ秒" &c ( Rがdifftimeを出力する方法と同様)