Databases
cancel
Showing results for 
Search instead for 
Did you mean: 

how to calculate time difference in Shell

SOLVED
Go to solution
Suman_7
Frequent Advisor

how to calculate time difference in Shell

Is it possible to calcuate time difference in UNIX shell:

for example:

System_Time = 4:36:00PM

My_Time = 9:00:00PM

Can I get something like this..

My_Time - System_Time

I need to check whether this time interval is enough to execute a batch job.

Thank You,
Suman
9 REPLIES
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: how to calculate time difference in Shell

The best way is to convert everything to seconds using a function:

#!/usr/bin/sh

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

typeset -i10 H1
typeset -i10 M1
typeset -i10 S1

date '+%H %M %S' | read H1 M1 S1

typeset -i10 H2=21
typeset -i10 M2=0
typeset -i10 S2=0

typeset -i10 DIFF=$(( $(get_seconds ${H2} ${M2} ${S2}) - $(get_seconds ${H1} ${M1} ${S1}) ))
echo "Time left = ${DIFF} seconds"
If it ain't broke, I can fix that.
Suman_7
Frequent Advisor

Re: how to calculate time difference in Shell

That worked wonderfully Thank You. Can you answer one more question..

How do I find if the parameter_date falls on the 1st day of the month in UNIX.

I have to check for a condition if the parameter_date is the 1st day of any of the 12 months.

Is it possible in UNIX to manipulate dates for example increment date by 1 day?

Thanks
Suman






A. Clay Stephenson
Acclaimed Contributor

Re: how to calculate time difference in Shell

Well the first of the month is rather easy:

DAY=$(date '%M');
if [[ "${DAY}" = "01" ]]
then
echo "First Day"
fi

However, the general answer to how to manipulate date ranges is the use of this script that someone wrote called caljd.sh.

For example:
This will return the date 3 days from now in MM DD YYYY format:
DT=$(caljd.sh $(caljd.sh -n 3))
echo "Date = ${DT}"

If all you wanted was the month 3 days from now that would be:
MO=$(caljd.sh -M $(caljd.sh -n 3))
echo "Month = ${MO}"

Invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: how to calculate time difference in Shell

Oops,
No semicolon at the end:
DAY=$(date '%M');
should be
DAY=$(date '%M')
If it ain't broke, I can fix that.
Suman_7
Frequent Advisor

Re: how to calculate time difference in Shell

Thanks for your reply..

I tried this script but it gives me error:
date: bad conversion

#####################
DAY=$(date '%M')
if [[ "${DAY}" = "28" ]]
then
echo "Twenty Eight Day"
fi
######################

Regards
Suman


A. Clay Stephenson
Acclaimed Contributor

Re: how to calculate time difference in Shell

Missing the '+' in the format.

DAY=$(date '+%M')

Do a "man date"; it explains all of your options.

If it ain't broke, I can fix that.
Suman_7
Frequent Advisor

Re: how to calculate time difference in Shell

I tried this it works now:

DAY=`date +%d`

Thanks
Suman
Suman_7
Frequent Advisor

Re: how to calculate time difference in Shell

Is it possible to use caljd.ksh to convert a specific parameter to 1 day back
for example..

parm_date=20040501

If the parameter date is first of month
then change this parameter date to 1 day back. Actually I need to pass parm_date=20040430 as my argument.

Also my date in the format yyyymmdd with no spaces.

Thanks a lot
Suman
Fred Ruffet
Honored Contributor

Re: how to calculate time difference in Shell

Suman,

If you have a lot of those jobs to do, GNU date would make it easy. It offers the same fetures as your date command, but adds somes :
. converting a date (for example to epoch, that will let you make differences)
. have date for yesterday or n days ago
...

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)