Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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 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 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 hossman (Prior) on Nov 01, 2005 at 20:02 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
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 contemplating the Monastery: (4)
As of 2021-04-10 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?