Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The following snippet turns a number of seconds to a compact string representing the equivalent amount of seconds in weeks, days, hours, minutes and seconds. This is useful for reporting elapsed time in an easily-graspable human-readable format (just how many weeks or days 123 456 789 seconds anyway)?

Here are some examples

1212s
601m0s
36001h0m0s
36011h0m1s
36611h1m1s
108003h0m0s
864011d0h0m1s
123456789204w0d21h33m9s

The code has a rather pleasing symmetry.

sub wdhms { my( $weeks, $days, $hours, $minutes, $seconds, $sign, $res ) = qw/ +0 0 0 0 0/; $seconds = shift; $sign = $seconds == abs $seconds ? '' : '-'; $seconds = abs $seconds; ($seconds, $minutes) = ($seconds % 60, int($seconds / 60)) if $sec +onds; ($minutes, $hours ) = ($minutes % 60, int($minutes / 60)) if $min +utes; ($hours, $days ) = ($hours % 24, int($hours / 24)) if $hou +rs; ($days, $weeks ) = ($days % 7, int($days / 7)) if $day +s; $res = sprintf '%ds', $seconds; $res = sprintf "%dm$res", $minutes if $minutes or $hours or $days +or $weeks; $res = sprintf "%dh$res", $hours if $hours or $days +or $weeks; $res = sprintf "%dd$res", $days if $days +or $weeks; $res = sprintf "%dw$res", $weeks if + $weeks; return "$sign$res"; }

In reply to Formatting elapsed time by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 04:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found