Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert seconds to hh:mm:ss

Status
Not open for further replies.
Well, I had to do it myself once, and I didn't know of any other way than this:

Code:
my $fTotalTime = 3911; # amount of seconds
my $iMinutes = $fTotalTime/60;
# round it
$iMinutes =~ s/\..+$//;
# seconds
my $iSeconds = $fTotalTime % 60;

print "$fTotalTime seconds is $iMinutes minutes, and $iSeconds seconds\n";

And you can do something similar for hours af course, but I didn't need that...
 
Blast. Invented the wheel again... [neutral]

All the same, my solution does save you some overhead...
 
my $Hours = $interval/3600;
$Hours =~ s/\..+$//; # round it
my $Minutes = ($interval - $Hours * 3600) / 60;
$Minutes =~ s/\..+$//; # round it
my $Seconds = $interval - ($Hours * 3600) - ($Minutes * 60);
$time=$Hours . ":" . $Minutes . ":" . $Seconds;


Long live king Moshiach !
 
There you go!

You can also use floor() by the way, in stead of the regular expression rounding.
Does the same thing of course, but it's a bit neater I think.

So then it will be:
Code:
use strict;

use POSIX;

my $interval = 123412;

my $Hours = floor($interval/3600); # calculate precise, and then floor it
my $Minutes = floor(($interval - $Hours * 3600) / 60);
my $Seconds = $interval % 60;

print "$Hours:$Minutes:$Seconds";
 
Or just use the perl built-in int


Code:
use strict;

my $interval = 123412;

my $Hours = int($interval/3600); # calculate precise, and then floor it
my $Minutes = int(($interval - $Hours * 3600) / 60);
my $Seconds = $interval % 60;

print "$Hours:$Minutes:$Seconds";
 
If you're not going to use a module or Regex, use int. Floor will give you incorrect values for negative deltas.

int(1.5) = 1
floor(1.5) = 1

int(-1.5) = -1
floor(-1.5) = -2
 
And finally, just remember to use sprintf to ensure left padding for your minutes and seconds.


Code:
use strict;

my $interval = 123412;

my $hours = int($interval/3600); # calculate precise, and then floor it
my $minutes = int(($interval - $hours * 3600) / 60);
my $seconds = $interval % 60;

my $time = sprintf"("%d:%02d:%02d", $hours, $minutes, $seconds);

print "$time";
 
y finalmente ... ahh.... forget it [wink]

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Yes, I know that about floor() vs. int().
It is always a good thing to point out of course, but to be fair, negatice values are quite unlikely here, no...?!?
[ponder]
 
Quite unlikely is still quite likely, in the general scheme of things. None of us know how the number of seconds is being generated and so to assume a positive amount is folly, IMO. I raised the point because it's code I have had to fix in a variety of languages.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top