LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 05-11-2006, 07:36 AM   #1
nickleus
Member
 
Registered: Nov 2004
Location: Noreg
Distribution: ubuntu
Posts: 107

Rep: Reputation: 15
calculate time difference between 2 formatted timestamps


Hi yall,

I have 2 numbers which i've formatted myself:
200605110035
200605102332

here is how they're formatted:
yyyymmddhhmm

how do i find the difference in minutes between the two using bash?
Thanks in advance =)
Nick

Last edited by nickleus; 05-11-2006 at 07:38 AM.
 
Old 05-11-2006, 04:45 PM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
Bash is the wrong tool for the job. This is a bit beyond the type of coding most people would attempt with a shell. Perl would be a better tool for this.

If you absolutely won't use anything else but bash, at least loosen the shackles you've put on yourself and opt for a different date format. I assume you came up with your date format something like this:
Code:
date +'%Y%m%d%H%M'
Try this instead
Code:
date +'%s'
This will give you date/time represented as "number of seconds since Jan-1-1970". You can take two dates in this representation, subtract one from the other, divide by 60, and there's your time difference expressed in minutes. This calculation can be done in bash much easier than a calculation using your current date format. Although I still think a perl program would be better...
 
Old 05-11-2006, 05:15 PM   #3
berbae
Member
 
Registered: Jul 2005
Location: France
Distribution: Arch Linux
Posts: 540

Rep: Reputation: Disabled
Hi nickleus
I suggest you write the 2 dates in a <file>, one per line, and run
Code:
awk 'BEGIN {FIELDWIDTHS = "4 2 2 2 2"}
{strdate= $1 " " $2 " " $3 " " $4 " " $5 " 00 0"
date[NR]=mktime(strdate)}
END {print (date[1] - date[2]) /60}' <file>

Last edited by berbae; 05-11-2006 at 05:21 PM.
 
Old 05-12-2006, 10:08 PM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Alternatively, you may strptime(3) and operate on time_t variables
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
FTP 4 hour time difference... Santinelli Linux - Enterprise 1 09-08-2005 11:04 AM
Mysql time difference textomat Programming 1 06-04-2005 01:05 PM
why do we have the 22 second time difference? masand LQ Suggestions & Feedback 7 03-30-2005 10:40 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 11:40 AM
Time difference when sending out mail. chuck77 Linux - General 2 05-30-2002 02:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration