#!/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;
}
# ./blah.pl6 'May 24 14:51:46 2019' 'May 26 00:54:45 2019'
122579
date +%s
START=`date +%s`
END=`date +%s`
DELTA=$(( END-START ))
echo $START $END $DELTA
1559147114 1559167743 20629
HOURS=$(( DELTA/3600 ))
MIN=$(( (DELTA-HOURS*3600)/60 ))
SEC=$(( DELTA-HOURS*3600-MIN*60 ))
echo $HOURS:$MIN:$SEC
5:43:49
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
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.Really? The man page date(1) has even an example of such usage. Perfectly works here:But Freebsd can't do this.
date -j -f "%a %b %d %T %Y" 'Sat Jun 01 07:02:31 2019' "+%s"
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
$ (( start = SECONDS ))
$ print $start
33.053455114364624
$ (( elapsed = SECONDS - start ))
$ print $elapsed
23.5835580825805664