Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://809720]
Approved by rovf
help
Chatterbox?
[1nickt]: I don;t think beliefs should be down-voted, just behaviours.
[james28909]: im not quite how to explain it any better nick. you evolved from ignorance to intelligence. not the other way. the universe evolves from gas coulds and debris into planets stars and galaxies ect. it doesnt happen any other way. hence it has ....
[james28909]: some kind of logic behind it
[james28909]: and that is also anothe rpoint i made, i think it has to do with perception of the world around you. most people think of evolution on a human scale. why could life evolve on this planet? because this planet evolved in this solar system. and so on.
[holli]: here's something for you to watch, James. I think you will like it
[erix]: for the record: I have not downvoted anyone on that subthread that was my fault
[james28909]: there are all kinds of things that had to happen to let life come to be. but at the same time, life may not be the end goal IF there is any kind of end goal lol
[james28909]: well who is the person who gets to decide which behaviour is worthy of a downvote? a person with their own beliefs? xD
[erix]: teleology -- I've never understood why that was thunk up
[erix]: ( and when teleology was brought up, during my biology-study., I couldn't get an answer either )

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (18)
As of 2017-12-15 14:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (433 votes). Check out past polls.

    Notices?