While there are many date & time packages available on CPAN, for my purposes, I generally just store times as returned by time() (seconds since 1970/01/01 00:00:00 IIRC). Then taking the difference is a simple subtraction. Of course, you'll need to format the results when you're done. But I typically do something like:
my $start=time;
# ... long processing job goes here...
my $dur = time - $start;
# Print out in appropriate format:
print "Job took $dur seconds\n";
# For longer jobs, I often use one of:
printf "Job took %5.2 minutes\n", $dur/60.0;
printf "Job took %5.2 hours\n", $dur/(60*60.0);
printf "Job took %5.2 days\n", $dur/(24*60*60.0);
printf "Job took %5.2 weeks\n", $dur/(7*24*60*60.0);
# etc.
Pretty simple and adequate for what I do. If I cared for more accuracy or features, I'd check out one of the Date modules on CPAN.
...roboticus
I have a fancier formatting module to show in format "Days HH:MM:SS", but the margin is too small for me to note it here. (Actually, it's on my server at home, and I haven't brought MY::Utils.pm into work with me yet. It's on my TODO list.)
-
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.
|