是否有将秒转换为英语的 CPAN 模块?

发布于 2024-12-06 06:56:16 字数 186 浏览 6 评论 0原文

是否有一个 CPAN 模块可以将秒数转换为人类可读的间隔英文描述?

secToEng( 125 ); # => 2 min 5 sec
secToEng( 129_600 ); # => 1 day 12 h

只要人类可读,格式并不重要。

我知道实施起来很简单。

Is there a CPAN module that can convert a number of seconds to a human-readable English description of the interval?

secToEng( 125 ); # => 2 min 5 sec
secToEng( 129_600 ); # => 1 day 12 h

The format isn't important as long as it's human-readable.

I know that it would be trivial to implement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情场扛把子 2024-12-13 06:56:16

事实证明 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

从剧情简介来看:

Time::Duration - 持续时间的四舍五入或精确的英文表达

在程序中的使用示例,以记录其运行时结束:

我的 $start_time = time();
使用时间::持续时间;
# 然后事情花费了所有的时间,然后结束:
打印“运行时”,持续时间(time() - $start_time),“。\ n”;

报告文件寿命的程序中的使用示例:

使用时间::持续时间;
我的 $file = 'that_file';
我的 $age = $^T - (stat($file))[9]; # 9 = 修改时间
print "$file 已修改", ago($age);

It turns out that Time::Duration does exactly this.

$ 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

From the synopsis:

Time::Duration - rounded or exact English expression of durations

Example use in a program that ends by noting its runtime:

my $start_time = time();
use Time::Duration;
# then things that take all that time, and then ends:
print "Runtime ", duration(time() - $start_time), ".\n";

Example use in a program that reports age of a file:

use Time::Duration;
my $file = 'that_file';
my $age = $^T - (stat($file))[9];  # 9 = modtime
print "$file was modified ", ago($age);
她说她爱他 2024-12-13 06:56:16

可以使用 DateTime

#!/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

DateTime can be used:

#!/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;

Output

1 day 12 hours
好倦 2024-12-13 06:56:16

从 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->mines 等内容。

Since Perl v5.9.5, the modules Time::Seconds and Time::Piece are part of the core Perl distribution. So you can use them without installing additional modules.

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

You can also use stuff like $ts->weeks, $ts->minutes, etc.

彼岸花ソ最美的依靠 2024-12-13 06:56:16

我不久前找不到这样的代码,所以我写了这两个例程。第二个子程序使用第一个子程序并执行您正在寻找的操作。

#-----------------------------------------------------------
# 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;
}

I wasn't able to find such code a while back, so I wrote these two routines. The second sub uses the first and does what you are looking for.

#-----------------------------------------------------------
# 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;
}
梦在深巷 2024-12-13 06:56:16

DateTime 模块就是您想要的。

DateTime::DurationDateTime::Format::Duration 将满足您的需要。

The DateTime modules are what you want.

DateTime::Duration and DateTime::Format::Duration will do what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文