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
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|