LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Closed Thread
 
LinkBack Search this Thread
Old 03-28-2012, 05:52 PM   #1
hisunday
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: Disabled
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.
 
Old 03-28-2012, 06:00 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,539
Blog Entries: 51

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

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-28-2012, 08:12 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,539
Blog Entries: 51

Rep: Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604Reputation: 2604
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')"
 
Old 03-28-2012, 10:26 PM   #5
hisunday
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
That's great. It resolved the issue. Thanks a lot.
 
Old 03-29-2012, 07:11 AM   #6
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 10,893
Blog Entries: 1

Rep: Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308Reputation: 1308
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/
 
  


Closed Thread

Tags
command, date, shell script


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script shell - Date / Time doint Programming 4 04-12-2012 09:09 AM
elapsed time calculation from two date strings hisunday Linux - Newbie 3 03-28-2012 06:00 PM
Using sed in a shell script to add date and time seefor Programming 3 02-25-2009 01:21 PM
Shell Script - Date/Time runnerpaul Programming 9 08-20-2008 07:45 AM
Howto Monitor elapsed time in a shell script rkrijger Linux - Software 2 03-11-2004 08:46 AM


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

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