Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare time strings 2

Status
Not open for further replies.

peterve

IS-IT--Management
Mar 19, 2000
1,348
0
0
NL
Hi,

In my shell script, I have 2 time strings
I want to compare those 2 times, and determine if the second string has a larger time that the first one

so if string 1 is 1:12 and string 2 is 2:03

then the script should state that string 2 is larger

if string 1 is 1:02 and string 2 is 46
then the script should state that string 2 is smaller

I don't want to use perl for this, if possible

thanks




--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
Why have you 46 instead of 0:46 ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
because that's how the string is feeded into my script...
(but I guess I should be able to prepend "0:" if the string does not contain a colon)

anyways - any ideas on how to compare the time strings ?



--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
prepend 0: if no colon

case $t1 in
*:*) ;;
*) t1=0:$t1
esac
case $t2 in
*:*) ;;
*) t2=0:$t2
esac

convert to seconds (first part * 60 + second part)

echo $t1|tr ':' ' '|read min sec
t1s=$(expr $min \* 60 + $sec)
echo $t2|tr ':' ' '|read min sec
t2s=$(expr $min \* 60 + $sec)

compare both amounts and decide

if [ $t1s -gt $t2s ]
then
echo "t1 > t2"
elif [ $t1s -eq $t2s ]
then
echo "t1 = t2"
else
echo "t1 < t2"
fi

all code snippets are untested, but you get the idea...


HTH,

p5wizard
 
If you have gnu's version of "date" available:

gdate -d "mytime" +"%s"

that will print the unix-time version of what you have, with which math is easier.
 
both solutions work fine - thanks p5wizard & Chapter11 !


--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top