Shell Elapsed time

I there such a utility which will display the elapsed time between two times, eg:-
Code:
Script started on Fri May 24 14:51:46 2019
Script done on Sun May 26 00:54:45 2019
 
To may knowledge the answer to your question is no.
However, a little script can take care of the process for you.

Code:
#!/usr/bin/env perl6

my %months = (
  Jan => '01',
  Feb => '02',
  Mar => '03',
  Apr => '04',
  May => '05',
  Jun => '06',
  Jul => '07',
  Aug => '08',
  Sep => '09',
  Oct => '10',
  Nov => '11',
  Dec => '12',
);

sub MAIN($str1, $str2)
{
        my @parts1 = $str1.split(' ');
        my @parts2 = $str2.split(' ');
        my $date1 = DateTime.new(sprintf("%u-%02u-%02uT%s+00:00", @parts1[3], %months{@parts1[0]}, @parts1[1], @parts1[2]));
        my $date2 = DateTime.new(sprintf("%u-%02u-%02uT%s+00:00", @parts2[3], %months{@parts2[0]}, @parts2[1], @parts2[2]));
        say $date2 - $date1;
}

Then just:

Code:
# ./blah.pl6 'May 24 14:51:46 2019' 'May 26 00:54:45 2019'
122579

I've chosen perl6 to do this but any language is capable of getting the duration between two dates.
 
I'm using a very simple approach:
Get Start and End time in seconds by running:
Code:
date +%s
Then you can easily calculate the difference:
Code:
START=`date +%s`
END=`date +%s`
DELTA=$(( END-START ))
echo $START $END $DELTA
1559147114 1559167743 20629
...and convert the result to HH:MM:SS:
Code:
HOURS=$(( DELTA/3600 ))
MIN=$(( (DELTA-HOURS*3600)/60 ))
SEC=$(( DELTA-HOURS*3600-MIN*60 ))
echo $HOURS:$MIN:$SEC
5:43:49
If needed, you can convert the time in seconds back to normal:
Code:
date -r $END
Wed May 29 16:09:03 MDT 2019
 
I'm using a very simple approach:
Get Start and End time in seconds by running:
Code:
date +%s

That's the sort of answer I like :) ... It prompted me into looking what date() was capable of and I found that GNU date could do something like date --date='Fri May 24 14:15:46 2019' +%s which returns 1558703746. But Freebsd can't do this. Fortunately you can install sysutils/coreutils which installs date as gdate.

Now to come up with a script to show the elapsed time between the start and finish time...
 
I hadn't spotted that, but as a result of your post, I've managed to come up with:-
echo `date -j -f "%a %b %d %T %Y" 'Sun May 26 00:54:45 2019' "+%s"` - `date -j -f "%a %b %d %T %Y" 'Fri May 24 13:51:46 2019' "+%s"` | bc

This comes up with 126179, although I'd rather show the answer in hours, but haven't worked out how to apply the rules of precedence in this case... I know that I need to insert brackets if I want to use '/ 3600' at the end, but getting the syntax right is proving difficult.
 
If you use the Korn shell (shells/ksh93) (not sure about other shells), you can put these commands in a script:

Code:
$ (( start = SECONDS ))
$ print $start
33.053455114364624
$ (( elapsed = SECONDS - start ))
$ print $elapsed
23.5835580825805664
 
Back
Top