Solved time difference korn shell

October 24, 2011 at 09:57:08
Specs: Windows XP
need to compute time difference in seconds in Korn shell:
t1='Oct 19 19:41:58 2011'
t2='Oct 20 00:33:52 2011'

diff= $t2 - $t1

See More: time difference korn shell

Report •

October 25, 2011 at 10:03:42
✔ Best Answer
A solution to this problem entirely in shell would be a lot of work so here is a solution using perl to get the number of seconds from the epoch.

I choose to change the date time fo the number of seconds from the epoch. Then you can subtract the two:

# perl autosplits the string and uses timelocal to return
# the number of seconds from the Epoch.
# No error checking!
function seconds_from_epoch {
echo $*| perl -MTime::Local -ane '
my $epochseconds = timelocal($F[5], $F[4], $F[3], $F[2], $F[1] - 1, $F[0]);
print "$epochseconds\n"; '

t1='10 19 19:41:58 2011'

# parse the date string
set -- $(echo $t1)

# parse the time string
set - $(IFS=':'; echo $tt)
hh=$1 # hour
mm=$2 # min
ss=$3 # seconds

sb=$(seconds_from_epoch "$yy $mo $dd $hh $mm $ss")
echo $sb
# end script.

For non-Linux systems, obtaining the number of seconds is best used with one of the modern scripting languages such as perl. (Note that I am leaving changing the month to a number to you.)

Let me know if you have any questions.

Report •
Related Solutions

Ask Question