Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I convert seconds into a readable time?

by Monolith-0 (Beadle)
on Aug 02, 2001 at 02:36 UTC ( #101511=perlquestion: print w/replies, xml ) Need Help??

Monolith-0 has asked for the wisdom of the Perl Monks concerning the following question: (dates and times)

ex. Change 196364 into '2 days, 6 hours, 32 minutes and 44 seconds' or something like that.

Originally posted as a Categorized Question.

  • Comment on How do I convert seconds into a readable time?

Replies are listed 'Best First'.
Re: How do I convert seconds into a readable time?
by I0 (Priest) on Aug 02, 2001 at 02:55 UTC
    printf "%d days, %d hours, %d minutes and %d seconds\n",(gmtime 196364 +)[7,2,1,0];
    But NB: this only works for intervals less than one month.
    tachyon's method (below) works for arbitrarily large values (within integer range; this could be fixed by using Math::BigInt).
Re: How do I convert seconds into a readable time?
by tachyon (Chancellor) on Aug 02, 2001 at 04:39 UTC

    I love IOs answer but in the spirit of TIMTOWDI here is how to roll your own. This illustrates one of the uses for the modulus operator:

    my $sec = 196364; print "days ", int($sec/(24*60*60)), "\n"; print "hours ", ($sec/(60*60))%24, "\n"; print "mins ", ($sec/60)%60, "\n"; print "secs ", $sec%60, "\n";
Re: How do I convert seconds into a readable time?
by Hofmator (Curate) on Aug 02, 2001 at 19:35 UTC
    Just a remark to I0's solution. Though very elegant this only works for values within one year. tachyon's 'roll-your-own-method' works for arbitrary values (within integer range - this could be fixed by using Math::BigInt). Just something to keep in mind ...

    Originally posted as a Categorized Answer.

Re: How do I convert seconds into a readable time?
by Limbic~Region (Chancellor) on Aug 20, 2003 at 19:38 UTC
    You could use Time::Duration. The question was intriguing enough to me to re-invent a wheel for the sake of learning.

    Cheers - L~R

Re: How do I convert seconds into a readable time?
by toolic (Bishop) on Jan 16, 2010 at 01:50 UTC
    If you only need an approximate answer, I'll invoke the "or something like that" clause and change 196364 into '2.3d'
    print sec2human(196364), "\n"; sub sec2human { my $secs = shift; if ($secs >= 365*24*60*60) { return sprintf '%.1fy', $secs/(365 +*24*60*60) } elsif ($secs >= 24*60*60) { return sprintf '%.1fd', $secs/( + 24*60*60) } elsif ($secs >= 60*60) { return sprintf '%.1fh', $secs/( + 60*60) } elsif ($secs >= 60) { return sprintf '%.1fm', $secs/( + 60) } else { return sprintf '%.1fs', $secs + } }
Re: How do I convert seconds into a readable time?
by hossman (Prior) on Nov 01, 2005 at 20:02 UTC
Re: How do I convert seconds into a readable time?
by zen-japh (Initiate) on Aug 03, 2001 at 21:51 UTC
    Here is a method of going back to seconds from a string like "2 days, 6 hours, 32 minutes and 44 seconds":
    sub dhms2sec { my $in = shift; $in =~ s/(and|,)//g; $in =~ s/(\w+)s/\1/g; my %y = reverse split(/\s+/,$in); return ($y{'second'}) + ($y{'minute'} * 60) + ($y{'hour'} * 60*60) + ($y{'day'} * 60*60*24); }
      The answer and question don't match.... The question is "How do I convert seconds into a readable time?" but the answer given converts readable time into seconds...

      Q: How do I '$seconds => $string'??
      A: Do this '$string => $seconds'!!

      We should change one or the other to get them to match.

      -Blake

      Update After a bit more investigating, I decided that this isn't really an issue. I thought it was the only answer given for the question. Since it is more of a "thats cool, here's how you can do it backwards" post, my complaint isn't really valid. Feel free to remove this node, mr reaper...

Re: How do I convert seconds into a readable time?
by Limbic~Region (Chancellor) on Aug 20, 2003 at 19:36 UTC
    You could use Time::Duration. The question was intruguing enough to me to re-invent a wheel for the sake of learning.

    Cheers - L~R

    Originally posted as a Categorized Answer.

Re: How do I convert seconds into a readable time?
by Anonymous Monk on Apr 11, 2004 at 08:41 UTC
    23569

    Originally posted as a Categorized Answer.

Re: How do I convert seconds into a readable time?
by Anonymous Monk on Oct 21, 2003 at 15:13 UTC
    My version is slightly different, and does not quite work right. Could someone spot my mistake?
    It's not written in perl, but the language is basic enough to be portable pretty much anywhere

    function convertToTime(seconds) var thex var they var thez var mind mind = Int(seconds) thex = mind Mod 60 if thex = 0 thex = mind / 60 else mind = (int(mind ) - int(thex)) end they = mind Mod 3600 if they = 0 they = mind / 3600 else mind = mind - they end thez = mind / 60 theTime = thez/60 + ":" + they/60 + ":" + thex return theTime end

      seconds to #d_HH:MM:SS.ss and back.

      sub dhms2s { my $dhms = shift; my @dinfo = split /d_|:/, $dhms; my ($s, $m, $h, $d) = reverse @dinfo; $d ||= 0; $h ||= 0; $m ||= 0; return ( (( $d * 24 + $h ) * 60 + $m ) * 60 + $s ); } my $m = 60; my $h = 60 * $m; my $d = 24 * $h; sub s2dhms { my $secs = shift; my $D = int($secs / $d); $secs = $secs - $D * $d; my $H = int($secs / $h); $secs = $secs - $H * $h; my $M = int($secs / $m); $secs = $secs - $M * $m; my $S = $secs ; my $r; if ($D) { $r .= sprintf "%dd_", $D; } if ( $S == int $S ) { $r .= sprintf "%02d:%02d:%02d", $H, $M, $S; } else { $r .= sprintf "%02d:%02d:%05.2f", $H, $M, $S; } return $r; }
Re: How do I convert seconds into a readable time?
by Anonymous Monk on Aug 20, 2003 at 19:15 UTC
    3000

    Originally posted as a Categorized Answer.

Re: How do I convert seconds into a readable time?
by Anonymous Monk on Mar 02, 2003 at 15:46 UTC
    Put your head into your anus and sniff

    Originally posted as a Categorized Answer.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://101511]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2022-11-27 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?