LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 03-28-2012, 12:42 AM   #1
hisunday
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
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.

Last edited by hisunday; 03-28-2012 at 12:52 AM. Reason: further clarification
 
Old 03-28-2012, 01:24 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,001

Rep: Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003Reputation: 1003
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.
 
Old 03-28-2012, 05:48 PM   #3
hisunday
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-28-2012, 06:00 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,950
Blog Entries: 11

Rep: Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860Reputation: 860
Quote:
Originally Posted by hisunday View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
date calculation satish.res Linux - Newbie 3 12-27-2009 05:31 AM
Get elapsed time HarryBoy Programming 2 08-08-2008 06:13 AM
diff. between elapsed time and cpu time madhugp Linux - General 1 01-15-2007 07:57 AM
find elapsed time innuendo_98 Programming 2 11-10-2005 08:03 AM
getting elapsed time.. Mad_C Programming 2 04-01-2003 01:12 AM


All times are GMT -5. The time now is 12:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration