Operating System - HP-UX
1753510 Members
5541 Online
108795 Solutions
New Discussion юеВ

False NTP, how to compare 2 time stamps?

 
SOLVED
Go to solution
John J Read
Frequent Advisor

False NTP, how to compare 2 time stamps?

I need to figure out a script to determine how many seconds two time stamps are apart. I am writing a script in HP-UX POSIX shell but am willing to try other languages if the task is better suited.

Easy example ( 31 seconds difference )
14:45:32
14:45:01

Hard example: ( 31 seconds differnce )
23:59:01
00:00:29

Any ideas or assistance would be appreciated. Here's the short answer to "why." My MPE/iX admins can't or won't install NTP on our HP3000 servers so I wrote and Expect script that telnets to MPE and retreives the system time. I can then compare that to the current system (Unix ) time. I simply want to know if they are more than 1 minute apart, then I'll generate an HP Openview and/or email alert so we know to set the system time asap.

Of course, points will be awarded.

3 REPLIES 3
John J Read
Frequent Advisor

Re: False NTP, how to compare 2 time stamps?

forgive the typo.. I'm aware that the times are not 31 seconds apart but you get the idea.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: False NTP, how to compare 2 time stamps?

Note: Your second example is wrong; shpould be 88 seconds.

First if you have Perl on your MPE box then this is trivially simple:

try
perl -e 'print scalar time'

That will yield epoch seconds gives you values you can readily subtract -- regardless of timezones.

Plan B: Pure shell but I assume you can parse HH:MM:SS into 3 distinct variables

#!/usr/bin/sh

seconds()
{
typeset -i10 HR=${1}
typeset -i10 MIN=${2}
typeset -i10 SEC=${3}
shift 3
typeset -i10 TM=$(( (${HR} * 3600) + (${MIN} * 60) + ${SEC} ))
echo "${TM}"
return 0
} # seconds

typeset -i10 DIFF=0
typeset -i10 T1=$(seconds 23 59 01)
typeset -i10 T2=$(seconds 00 00 29)
if [[ ${T1} -gt ${T2} ]]
then
DIFF=$(( (86400 - ${T1}) + ${T2} ))
else
DIFF=$(( ${T2} - ${T1} ))
fi
echo "Diff = ${DIFF} seconds."

If I ain't made no typo's that should do it but it only allows for 1 day's wraparound and does assume that both times are same timezone.

If it ain't broke, I can fix that.
RAC_1
Honored Contributor

Re: False NTP, how to compare 2 time stamps?

Get the epoch time using perl etc. and do some mathes to know the difference in seconds.

Such as
perl -e 'print time(), "\n"'
This will print time in epoch

then
echo "epoch_time_1-epoch_time_2"|bc

Hope this helps.

Anil
There is no substitute to HARDWORK