Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Date/Time Arithmetic

by newbie01.perl (Sexton)
on Nov 27, 2009 at 10:30 UTC ( #809720=perlquestion: print w/replies, xml ) Need Help??
newbie01.perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Need some help please on doing date/time arithmetic. Basically I have a script where I save the starttime and endtime using time(). And at the end of the run, I do a endtime - starttime to get the difference of how long the script took to run which I believe is in seconds.

Question is if there is any way of converting the result so that it can be displayed in x-days, x-hours, x-mins, x-seconds instead of second?

At the moment, am thinking of writing some sort of function to convert the seconds to hours, minute, seconds but before I go to that length, I am wishfully thinking that maybe someone had already come across the same requirement that am looking for. Any advise will be very much appreciated.

Excerpt of the Perl script as below. Note that am very restricted to what modules I have available and cannot install additional Date/Time modules that I've found very useful. At the moment, am only allowed Time::Local.

Any response or suggestions will be very much appreaciated. Thanks in advance.

use strict; use POSIX; use File::Basename; use Time::Local; ...... ...... my $start_time = scalar localtime; my $start = time(); ...... ...... my $end_time = scalar localtime; my $end = time(); #my $end_timelocal = timelocal($sec, $min, $hour, $mday, $mon, $year); print "end_time ==> $end_time" . "\n"; #print "end_timelocal ==> $end_timelocal" . "\n"; my $diff = $end - $start;

Replies are listed 'Best First'.
Re: Date/Time Arithmetic
by Ratazong (Monsignor) on Nov 27, 2009 at 10:38 UTC
Re: Date/Time Arithmetic
by Anonymous Monk on Nov 27, 2009 at 10:39 UTC
Re: Date/Time Arithmetic
by gmargo (Hermit) on Nov 27, 2009 at 14:52 UTC

    Take a look at the parseInterval method of Time::Interval.

    For your entertainment, here is my own adaptation of parseInterval. I found myself doing what you are doing so frequently that I added code for various output formats. Adapt as desired.

    #--------------------------------------------------------------- # Time Intervals, used by 'parseInterval2()' # what everything is worth in seconds #--------------------------------------------------------------- my %time_intervals = ( 'days' => (60 * 60 * 24), 'hours' => (60 * 60), 'minutes' => 60, 'seconds' => 1 ); #--------------------------------------------------------------- # # parseInterval2 - Subset of Time::Interval functionality # Only parses 'seconds' intervals. # #--------------------------------------------------------------- =pod =item parseInterval2 ( I<seconds>, I<stringflag> ) Return a hash describing how many 'hours', 'minutes', 'seconds' are represented by the I<seconds> argument. If I<stringflag> is 1, then the time is returned in a wordy manner similar to Time::Interval. If I<stringflag> is 2, then the time is returned in a DD:HH:MM:SS format. If I<stringflag> is 3, then the time is returned in an HH:MM:SS format where the days value is added into the hours position. Otherwise, return a reference to a hash. =cut #--------------------------------------------------------------- sub parseInterval2 { my ($seconds, $String) = @_; $String = 0 if !defined $String; #do the thang my %time = ( 'days' => 0, 'hours' => 0, 'minutes' => 0, 'seconds' => 0 ); foreach ("days","hours","minutes","seconds") { while ($seconds >= $time_intervals{$_}) { $time{$_} ++; $seconds -= $time_intervals{$_}; } } if ($String == 1) { # [[[DD days,] HH hours,] MM minutes,] SS seconds my @temp; my $show = 0; foreach ("days","hours","minutes","seconds") { $show = 1 if $time{$_} > 0; next if !$show; push (@temp, "$time{$_} $_"); } return (join (", ", @temp)); } elsif ($String == 2) { # [DD:]?HH:MM:SS my $out = ""; $out .= sprintf("%02d:", $time{'days'}) if $time{'days'}; $out .= sprintf("%02d:%02d:%02d", @time{qw(hours minutes secon +ds)}); return $out; } elsif ($String == 3) { # [H]*HH:MM:SS return sprintf("%02d:%02d:%02d", (24 * $time{'days'}) + $time{'hours'}, @time{qw(minutes se +conds)}); } else { #return a data structure return (\%time); } }

Re: Date/Time Arithmetic
by GrandFather (Sage) on Nov 27, 2009 at 21:31 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://809720]
Approved by rovf
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2017-06-26 14:56 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (583 votes). Check out past polls.