How to calculate the time difference.

Hi All,

I've written a script which reads all the systems backup information and saves it in a log file.

ssh -l ora${sid} ${primaryhost} "tail -1 /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | awk '{print $3,$4,$5,$6}' >> ${RESULTFILE}
The output comes as below:
2008-09-30 06.00.01 2008-09-30 06.00.49

??? Now i want to calculate the time taken from start to End and display it in the last..
$3$4-$5$6 = how much time taken should display hours:minutes
2008-09-30 06.00.01 2008-09-30 06.00.49 (HR:MM)
It should also check the dates, since some of the backup start at end of the day and finish the next day...

How do we do this..??????????????????

An help would be appreciated... thank you in advance.

Since you are dealing with text, you will need to covert your text to a numeric format before you can perform math operations. So, the trick is to convert your text to a numeric format.

Try this...

ssh -l ora${sid} ${primaryhost}  "awk '{print $3,$4,$5,$6}' /oracle/$ORACLE_SID/sapbackup/back$ORACLE_SID.log" | while read t1 t2 t3 t4
do
        tap1=`echo "$t1 $t2" | sed 's/[-/\.]/ /g'`
        tap2=`echo "$t3 $t4" | sed 's/[-/\.]/ /g'`
        echo $tap1
        echo $tap2
        nawk -f mk.awk -v _tm_test_1="$tap1" -v _tm_test_2="$tap2" >> ${RESULTFILE}
done
 
mk.awk:
----------
function Init()
{
        # Initialize table of month lengths
        _tm_months[0,1] = _tm_months[1,1] = 31
        _tm_months[0,2] = 28; _tm_months[1,2] = 29
        _tm_months[0,3] = _tm_months[1,3] = 31
        _tm_months[0,4] = _tm_months[1,4] = 30
        _tm_months[0,5] = _tm_months[1,5] = 31
        _tm_months[0,6] = _tm_months[1,6] = 30
        _tm_months[0,7] = _tm_months[1,7] = 31
        _tm_months[0,8] = _tm_months[1,8] = 31
        _tm_months[0,9] = _tm_months[1,9] = 30
        _tm_months[0,10] = _tm_months[1,10] = 31
        _tm_months[0,11] = _tm_months[1,11] = 30
        _tm_months[0,12] = _tm_months[1,12] = 31
}
# decide if a year is a leap year
function _tm_isleap(year,    ret)
{
        ret = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
        return ret
}
# convert a date into seconds
function _tm_addup(a,    total, yearsecs, daysecs, hoursecs, i, j)
{
        hoursecs = 60 * 60
        daysecs = 24 * hoursecs
        yearsecs = 365 * daysecs
        total = (a[1] - 1970) * yearsecs
        # extra day for leap years
        for (i = 1970; i < a[1]; i++)
        if (_tm_isleap(i))
            total += daysecs
        j = _tm_isleap(a[1])
        for (i = 1; i < a[2]; i++)
        total += _tm_months[j, i] * daysecs
        total += (a[3] - 1) * daysecs
        total += a[4] * hoursecs
        total += a[5] * 60
        total += a[6]
        return total
}
# mktime --- convert a date into seconds, compensate for time zone
function mktime(str, res1, res2, a, b, i, j, t, diff)
{
        i = split(str, a, " ")    # don't rely on FS
        if (i != 6)
        return -1
        # force numeric
        for (j in a)
        a[j] += 0
        # validate
        if (a[1] < 1970 ||
        a[2] < 1 || a[2] > 12 ||
        a[3] < 1 || a[3] > 31 ||
        a[4] < 0 || a[4] > 23 ||
        a[5] < 0 || a[5] > 59 ||
        a[6] < 0 || a[6] > 60 )
            return -1
        res1 = _tm_addup(a)
        return res1
}
BEGIN  {
        Init()
        t = mktime(_tm_test_1)
        s = mktime(_tm_test_2)
        if (t != "-1" && s != "-1")
        {
                diff = s - t
                PerD=(24 * 60 * 60)
                DD = diff/PerD
                rem = (diff - (PerD * int(DD)))
                HH = rem / (60 * 60)
                mi = rem - (int(HH) * 60 * 60)
                MM = mi / 60
                SS = mi - (int(MM) * 60)
                printf("Time taken to Complete is %d Days %d Hour %d Minutes %d Seconds - Total Seconds %s\n", DD, HH, MM, SS, diff)
        }
        else
                print "Wrong Inputs.."
}