Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How do I convert seconds into a readable time?

( #101511=categorized question: print w/ replies, xml ) Need Help??
Contributed by Monolith-0 on Aug 02, 2001 at 02:36 UTC
Q&A  > dates and times


Description:

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

Answer: How do I convert seconds into a readable time?
contributed by I0

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).
Answer: How do I convert seconds into a readable time?
contributed by tachyon

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";
Answer: How do I convert seconds into a readable time?
contributed by Limbic~Region

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

Answer: How do I convert seconds into a readable time?
contributed by toolic

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 + } }
Answer: How do I convert seconds into a readable time?
contributed by zen-japh

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); }
Answer: How do I convert seconds into a readable time?
contributed by hossman

The DateTime project has created a DateTime::Duration object, as well as DateTime::Format::Duration.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (16)
    As of 2015-07-02 13:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (40 votes), past polls