LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   elapsed time calculation from two date strings (http://www.linuxquestions.org/questions/linux-newbie-8/elapsed-time-calculation-from-two-date-strings-936808/)

hisunday 03-28-2012 01:42 AM

elapsed time calculation from two date strings
 
Looking for help.

Have got two variables defined as below. Actually the values are date strings (yyyymmddHHMMSS year month day 24hour min second)
x1="20120228204515"
x2="20120228204936"

Looking for some logic (that could be used in shell script) to find the difference of x2 and x1 in terms of seconds (or minutes). Thanks.

pan64 03-28-2012 02:24 AM

you may need to use perl, awk or something to parse that string and evaluate.
based on this manpage: http://unixhelp.ed.ac.uk/CGI/man-cgi?date
you can also try:
Code:

date --date="string1" --rfc-3339=seconds
to convert it to seconds if it works for you.

hisunday 03-28-2012 06:48 PM

Thanks for your response.

I tried: date --date="20120228204515" --rfc-3339=seconds

but this returns with "date: invalid date `20120228204515'" error. It looks that input string has to be in a specific format - it can't be in yyyymmddHHMMSS format for this command to work.

Also could you suggest some sample code to use 'awk' as a solution. Thanks again.

Tinkster 03-28-2012 07:00 PM

Quote:

Originally Posted by hisunday (Post 4639132)
Thanks for your response.

I tried: date --date="20120228204515" --rfc-3339=seconds

but this returns with "date: invalid date `20120228204515'" error. It looks that input string has to be in a specific format - it can't be in yyyymmddHHMMSS format for this command to work.

Also could you suggest some sample code to use 'awk' as a solution. Thanks again.

Indeed ... you will need to massage your data a little.

Code:

echo $(date -d "$(echo $x2|sed -r 's/(....)(..)(..)(..)(..)(..)/\1-\2-\3 \4:\5:\6/')" "+%s" )"-"$( date -d "$(echo $x1|sed -r 's/(....)(..)(..)(..)(..)(..)/\1-\2-\3 \4:\5:\6/')" "+%s" )|bc -iq
261



All times are GMT -5. The time now is 02:31 PM.