是否有CPAN模块可以将数秒转换为人类可读的英语区间描述?
secToEng( 125 ); # => 2 min 5 sec
secToEng( 129_600 ); # => 1 day 12 h
只要它是人类可读的,格式就不重要了。
我知道实施起来是微不足道的。
答案 0 :(得分:13)
事实证明Time::Duration就是这样做的。
$ perl -MTime::Duration -E 'say duration(125)'
2 minutes and 5 seconds
$ perl -MTime::Duration -E 'say duration(129_700)'
1 day and 12 hours
从概要:
时间::持续时间 - 持续时间的四舍五入或精确英文表达
在程序中使用示例,该程序以记录其运行时结束:
my $start_time = time(); use Time::Duration; # then things that take all that time, and then ends: print "Runtime ", duration(time() - $start_time), ".\n";
在报告文件年龄的程序中使用示例:
use Time::Duration; my $file = 'that_file'; my $age = $^T - (stat($file))[9]; # 9 = modtime print "$file was modified ", ago($age);
答案 1 :(得分:10)
#!/usr/bin/env perl
use strict;
use warnings;
use DateTime;
use Lingua::EN::Inflect qw( PL_N );
my $dt = DateTime->from_epoch( 'epoch' => 0 );
$dt = $dt->add( 'seconds' => 129_600 );
$dt = $dt - DateTime->from_epoch( 'epoch' => 0 );
my @date;
push @date, $dt->days . PL_N( ' day', $dt->days ) if $dt->days;
push @date, $dt->hours . PL_N( ' hour', $dt->hours ) if $dt->hours;
push @date, $dt->minutes . PL_N( ' minute', $dt->minutes ) if $dt->minutes;
push @date, $dt->seconds . PL_N( ' second', $dt->seconds ) if $dt->seconds;
print join ' ', @date;
1 day 12 hours
答案 2 :(得分:5)
从Perl v5.9.5开始,模块Time :: Seconds和Time :: Piece是核心Perl发行版的一部分。因此,您无需安装其他模块即可使用它们。
perl -MTime::Seconds -e 'my $s=125; my $ts=new Time::Seconds $s; print $ts->pretty, "\n"'
# 2 minutes, 5 seconds
perl -MTime::Seconds -e 'my $s=129_600; my $ts=Time::Seconds->new($s); print $ts->pretty, "\n"'
# 1 days, 12 hours, 0 minutes, 0 seconds
您还可以使用$ts->weeks
,$ts->minutes
等内容。
答案 3 :(得分:2)
DateTime模块就是您想要的。
答案 4 :(得分:1)
我一段时间没能找到这样的代码,所以我写了这两个例程。第二个子使用第一个子,并完成你要找的东西。
#-----------------------------------------------------------
# format_seconds($seconds)
# Converts seconds into days, hours, minutes, seconds
# Returns an array in list context, else a string.
#-----------------------------------------------------------
sub format_seconds {
my $tsecs = shift;
use integer;
my $secs = $tsecs % 60;
my $tmins = $tsecs / 60;
my $mins = $tmins % 60;
my $thrs = $tmins / 60;
my $hrs = $thrs % 24;
my $days = $thrs / 24;
if (wantarray) {
return ($days, $hrs, $mins, $secs);
}
my $age = "";
$age .= $days . "d " if $days || $age;
$age .= $hrs . "h " if $hrs || $age;
$age .= $mins . "m " if $mins || $age;
$age .= $secs . "s " if $secs || $age;
$age =~ s/ $//;
return $age;
}
#-----------------------------------------------------------
# format_delta_min ($seconds)
# Converts seconds into days, hours, minutes, seconds
# to the two most significant time units.
#-----------------------------------------------------------
sub format_delta_min {
my $tsecs = shift;
my ($days, $hrs, $mins, $secs) = format_seconds $tsecs;
# show days and hours, or hours and minutes,
# or minutes and seconds or just seconds
my $age = "";
if ($days) {
$age = $days . "d " . $hrs . "h";
}
elsif ($hrs) {
$age = $hrs . "h " . $mins . "m";
}
elsif ($mins) {
$age = $mins . "m " . $secs . "s";
}
elsif ($secs) {
$age = $secs . "s";
}
return $age;
}