Operating System - HP-UX
1821057 Members
2505 Online
109631 Solutions
New Discussion юеВ

Calculate time difference in Unix shell script

 
SOLVED
Go to solution
Melvin Thong
Advisor

Calculate time difference in Unix shell script

Hello,

I have a question on shell scripting. I have a scenario whereby an application has captured the start timestamp on a file in the format of
2002-11-14 09:31 when the application services started up.

I am required to produce a script to calculate the time difference between the current system date with the timestamp captured on the file I have mentioned above. The checking is scheduled to run every 4 hours.

I am wondering is there a way that I can do a conversion of the time captured on file into a value where I can do the mathematical expression such as subtraction.

My objective is to subtract the current system date with the timestamp that was captured on the file.

time_difference=curr_date - prev_timestamp

How can this be done if I were to use the shell script to calculate the time difference. Appreciate if you could give me some advice on how to get this done. Thank you.

Regards,
Melvin
6 REPLIES 6
harry d brown jr
Honored Contributor

Re: Calculate time difference in Unix shell script

I suggest you read this link for Clay's script (caljd.sh):

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xea948cc5e03fd6118fff0090279cd0f9,00.html

Plus have a look here for a lot of scripts:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x026250011d20d6118ff40090279cd0f9,00.html


live free or die
harry
Live Free or Die
john korterman
Honored Contributor
Solution

Re: Calculate time difference in Unix shell script

Hi Melvin,
there is probably no easy way of doing that. I was once requested to do something similar and I ended up making the attached script. It is slow but can return the number of seconds elapsed since midnight, Jan 1 of a specified year, e.g. 1970 and a certain specified date.
Example:

# ./return_seconds.sh 1970 2002 11 14 9 31 00
# 1037266260

The attached script can - if executed without parameters - also calculate the number of seconds elapsed since midnight Jan 1 1970 and current system time, e.g.:
# ./return_seconds.sh
1039595802

Difference:
# echo 1039596799 - 1037266260 | bc
2330539


However, if you do not expect the time difference to be bigger than a few days you can speed up things by only counting back to midnight of e.g. the current year, e.g.:
# date
Ons. 11 Dec. 2002 08:45

# ./return_seconds.sh 2002 2002 Dec 11 8 45 00
29753100

./return_seconds.sh 2002 2002 11 14 9 31 00
27423060

# echo 29753100 - 27423060 | bc
2330040

As time is passing the two results are not identical...
Hope it is of some use, but if you wait long enough I am sure someone will come up with a perl one-liner!

regards,
John K.





it would be nice if you always got a second chance
Christian Gebhardt
Honored Contributor

Re: Calculate time difference in Unix shell script

Jean-Louis Phelix
Honored Contributor

Re: Calculate time difference in Unix shell script

hi,

Simple C program (compile with make) then use within a shell script like :

#!/usr/bin/sh
# get date in VAR for example :
VAR=$(cat file | line | cut -c1-2)
# use my prog (each field from date is an arg)
myprog $(echo $VAR | tr ':-' ' ')

Regards.
It works for me (┬й Bill McNAMARA ...)
F. X. de Montgolfier
Valued Contributor

Re: Calculate time difference in Unix shell script

Hi,

I am not aware of a shell command that would make this easy; but if you look at
http://docs.hp.com/cgi-bin/onlinedocs.py?mpn=B2355-90683&service=hpux&path=../B2355-90683/00/00/89&title=HP-UX%20Reference%20Volume%204%3A%20Section%203

you'll find how to use difftime() to get the time difference between two dates...

Cheers,

FiX
Robin Wakefield
Honored Contributor

Re: Calculate time difference in Unix shell script

Hi Melvin,

Quick bit of perl would also do it:

perl -e '{use Time::Local;($y,$mo,$d,$h,$mi)=split /:| |-/,shift;print time-timelocal(0,$mi,$h,$d,$mo-1,$y-1900),"\n";}' "2002-12-11 11:22"

Rgds, Robin.