使用 Perl 将秒转换为天、小时、分钟 [英] Convert seconds to days, hours, minutes using Perl

查看:106
本文介绍了使用 Perl 将秒转换为天、小时、分钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 perl 在几秒钟内控制生成的输出.我想以天、小时、分钟和秒的形式显示时间(以秒为单位),以使其人类可读的格式.

I would like to take control of the output generated in seconds using perl. I would like to show the time (in seconds) in the form of days, hours, minutes and seconds to make it human readable format.

代码

my $start = time;
<some other perl codes>
my $duration = time - $start;
print "Total Execution time: $duration s\n";

输出为 311052 s

所需输出是 3 天 14 小时 24 分 12 秒

Required Output is 3 days 14 hours 24 minutes 12 seconds

注意:我是 Perl 的新手.在您将其标记为重复之前,我已经完成了研究.我已经浏览了所有讨论类似解决方案的stackoverflow,没有任何帮助.我的代码很小.可以添加的几行更改可能是什么.

Note: I am new to Perl. Before you flag this is as duplicate, I have done my research. I have gone through all the stackoverflow where similar solutions were discussed, none helped. My code is very small. What could be the few line changes that could be added.

推荐答案

您可以使用 Time::PieceTime::Seconds 模块,它们将完成所有工作这个给你

You could use the Time::Piece and Time::Seconds modules, which will do all of this for you

在这个程序中,我只是将 pi 天添加到当前日期和时间以获得具有代表性的持续时间.当然,您将使用实际时间

In this program I have simply added pi days onto the current date and time to get a representative duration. You will, of course, use an actual time

use strict;
use warnings;

use Time::Piece;
use Time::Seconds qw/ ONE_DAY /;

my $start = localtime;
my $end = $start + int(355/113 * ONE_DAY);

my $duration = ($end - $start)->pretty;
print "Total Execution time: $duration\n";

输出

Total Execution time: 3 days, 3 hours, 23 minutes, 53 seconds

这篇关于使用 Perl 将秒转换为天、小时、分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆