time difference calculation
Hi i am trying to calculate the difference between 2 time stamps but unable to sort out hw to do it.
my time format is like 05:36:11 (24 hr clock) can anyone suggest a method or utility so i can calculate a time diff in shell script. |
more details please.. is it a bash script?
did you read about zsh which is more powerful? I think your problem is about data types ..may be converting it to another format will be helpful.. asking this question in the right forum will help you a lot which is the "Programming forum".. |
sorry for posting here
yes it is a bash script no i havenot looked in zsh yup i m converting the format to seconds. |
The following should give you some clues:
Code:
#!/bin/bash Code:
Start 05:36:11 |
thanks karim and tentenths
was stuck at converting seconds to hh:mm:ss format |
You might be interested in these advanced date processing utilities, including ddiff, which does just what you're asking.
http://www.fresse.org/dateutils/ |
I dont think i will get those utilities even though it does exactly what i need
any how i figured out the arithmetics as below Quote:
thanks for ur help and i dont know hw to give reputation and cant see a white scale either |
Quote:
Code:
SEC1=`date -d "${starttime}" +%s` ##in hh:mm:ss formant to convert in seconds from epoch time |
yup
totaldiff=`date +%H:%M:%S -ud @${difftemp}` was giving blank data in totaldiff variable and i can't find why |
also i have one more thing
tempapp.txt 06:32:33 awk -F : '{ hours = 3600*$1 }{ minutes = 60*$2 }{ seconds = $3 }{SEC1 = hours+minutes+seconds} END { print SEC1 }' tempapp.txt 23553 gives proper output but i cannot use SEC1 once i come out of awk and also cannot assign the output to any other variable i assigned it gives null value |
Quote:
Anyways, was just curious, good you got sorted. |
any idea about the awk command
|
Variables created inside AWK programs aren't exposed to the bash shell, you can however assign the output of AWK to a variable:
Code:
#!/bin/bash |
Thanks to TenTenth
Thanks to TenTenth for this replay. I think that is a cool solution and just what i need.
#!/bin/bash # Time Arithmetic TIME1=05:36:11 TIME2=06:46:00 # Convert the times to seconds from the Epoch SEC1=`date +%s -d ${TIME1}` SEC2=`date +%s -d ${TIME2}` # Use expr to do the math, let's say TIME1 was the start and TIME2 was the finish DIFFSEC=`expr ${SEC2} - ${SEC1}` echo Start ${TIME1} echo Finish ${TIME2} echo Took ${DIFFSEC} seconds. # And use date to convert the seconds back to something more meaningful echo Took `date +%H:%M:%S -ud @${DIFFSEC}` |
Code:
#!usr/bin/env/bash |
All times are GMT -5. The time now is 08:10 AM. |