LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   elapsed time calculation from two date strings - shell script (https://www.linuxquestions.org/questions/linux-general-1/elapsed-time-calculation-from-two-date-strings-shell-script-936965/)

hisunday 03-28-2012 05:52 PM

elapsed time calculation from two date strings - shell script
 
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.

unSpawn 03-28-2012 06:00 PM

Easiest way IMHO would be to not use time stamps but epoch instead as that's easier to work with. Note the 'date' command can convert human readable dates to epoch and back in about every format you can think of.

hisunday 03-28-2012 06:25 PM

Thanks for your response. Actually in our case the timestamp values (x1 and x2) are already stored in flat files for thousands of records and I need to calculate the elapsed time for these records. We get this flat file from another interface system where we don't have much control. In my original post I gave a simplified requirement by defining two variables x1 and x2. Thanks.

unSpawn 03-28-2012 08:12 PM

This may explain on stdout what I mean:
Code:

x1="20120228204515"
INITDATE="${x1:0:4}-${x1:4:2}-${x1:6:2} ${x1:8:2}:${x1:10:2}:${x1:12:2}"
echo x1="$(eval echo env TZ=UTC date --date=\"${INITDATE}\" +'%s')"


hisunday 03-28-2012 10:26 PM

That's great. It resolved the issue. Thanks a lot.

onebuck 03-29-2012 07:11 AM

Moderator response
 
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate of ; http://www.linuxquestions.org/questi...trings-936808/


All times are GMT -5. The time now is 03:11 AM.