LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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
  Search this Thread
Old 11-28-2013, 12:08 PM   #1
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Rep: Reputation: Disabled
How to Calculate Time Difference?


Hi,

I want to calculate time difference, i tried below steps however getting some error:

let say.. current_time=22:18 and previous_time=22:01 .
i tried as diff=$(($current_time - $previous_time))
error: syntax errorperand expected(error token is "22:18 - 22:01"

could someone please help me to calculate the diff?

Thanks in advance :-)
 
Old 11-28-2013, 01:15 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Have a look at this:
Code:
#!/bin/bash
# usage: time.diff.sh mm:ss mm:ss
# output: difference in seconds

OIFS=$IFS
IFS=":"

startTime=($1)
endTime=($2)

IFS=$OIFS

stInSecs=$(( ${startTime[0]}*60 + ${startTime[1]}  ))
etInSecs=$(( ${endTime[0]}*60   + ${endTime[1]} ))

diffSecs=$(( $stInSecs - $etInSecs ))

echo "$1 - $2 --> $diffSecs seconds."
Example run:
Code:
$ ./time.diff.sh 22:18 22:01
22:18 - 22:01 --> 17 seconds.
Calculating with full dates is easier to do:
Code:
#!/bin/bash

startDate=$( date -d "nov 28 09:10:40" +%s )
endDate=$( date -d "nov 27 23:37:08" +%s )
diffSecs=$(( startDate - endDate ))
echo "Difference: $diffSecs seconds."
Example run:
Code:
$ ./full.date.sh
Difference: 34412 seconds.

Last edited by druuna; 11-28-2013 at 03:39 PM. Reason: Fixed typo
 
1 members found this post helpful.
Old 11-28-2013, 01:25 PM   #3
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,116

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
If you are using "GNU date" (which would be what's installed on most Linux systems these days):

Code:
let DELTA_SECONDS=(`date +%s -d "1/2/2013 00:00:00"`-`date +%s -d "1/1/2013 00:00:00"`)
echo $DELTA_SECONDS
If you don't want to display the difference in seconds as the above does (you want minutes, or hours, or days instead), you can just divide the above result by 60, 3600, 86400, etc. to convert to whatever unit you need.
 
1 members found this post helpful.
Old 11-29-2013, 01:36 AM   #4
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Smile Issue Resolved

Followed the steps provided by you and its working fine..
Thanks a lot for all your help and time :-)
 
Old 11-29-2013, 02:30 AM   #5
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
How to Calculate Time Difference? Reply to Thread

Hi,

as i need a time difference in min, i am doing this:

a=$(date -d "$curr_date $curr_time" +%M )
b=$(date -d "$fps_validated_date $fps_validated_time" +%M )
diffmin=$(( $a - $b ))

however i am getting below errors:
1. a=$(date -d "$curr_date $curr_time" +%M)
date: invalid date `29/11/13 07:15'

2. b=$(date -d "$fps_validated_date $fps_validated_time" +%M )
date: invalid date `29/11/13 06:32'

how to resolve it?
 
Old 11-29-2013, 02:46 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Use a valid date.

This is a valid date: 2013/11/29 07:15 and this is not a valid date: 29/11/2013 07:15

Date should be in this format: YYYY/mm/dd HH:MM:SS

EDIT: The code in post #5 will probably not do what you expect it to do. Have a look at the following example:
Code:
#!/bin/bash

curr_date="2013/11/29"
curr_time="07:30"

fps_validated_date="2013/11/29"
fps_validated_time="06:30"

a=$(date -d "$curr_date $curr_time" +%M )
b=$(date -d "$fps_validated_date $fps_validated_time" +%M )

diffmin=$(( $a - $b ))

echo "Difference: $diffmin"
Example run:
Code:
$ ./foo.sh 
Difference: 0
The trick used by haertig and me is translating the date into seconds from epoch (the %s) and calculate with that. Using %M will only show the minutes part of the date string.

Last edited by druuna; 11-29-2013 at 02:59 AM.
 
Old 11-29-2013, 03:50 AM   #7
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
How to Calculate Time Difference?

Actually there is a file with date as 29/11/13 07:15 and according to this i converted the system time in the similar format.
If i want to read this date 29/11/13 07:15 from a file and convert it into the format 2013/11/29 07:15, is there a way to convert it ? and then we can take a time difference?

Last edited by santosh0782; 11-29-2013 at 04:04 AM.
 
Old 11-29-2013, 04:09 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Converting 29/11/13 07:15 into 2013/11/29 07:15:
Code:
$ foo="29/11/13 07:15"
$ echo $foo | awk -F"[ /]" '{ print "20"$3"/"$2"/"$1" "$4 }'
2013/11/29 07:15
 
Old 02-19-2014, 07:01 AM   #9
hroptatyr
LQ Newbie
 
Registered: Apr 2012
Posts: 6

Rep: Reputation: Disabled
Using ddiff (from dateutils) makes calculating date and time differences easy:

Code:
$ ddiff 22:01 22:18
1020s
$ ddiff 22:01 22:18 -f '%M minutes'
17 minutes
 
2 members found this post helpful.
Old 02-22-2014, 09:57 PM   #10
santosh0782
Member
 
Registered: Nov 2013
Posts: 132

Original Poster
Rep: Reputation: Disabled
Thanks a lot, its really good :-)
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get Kernel space time and compare to userspace to calculate difference? sindhu4sind Programming 1 06-05-2012 01:09 PM
nanosecond calculate difference endof Programming 6 04-11-2011 09:05 AM
bash: time difference and calculate the mid day of the day? frenchn00b Programming 3 05-03-2009 03:03 PM
how to calculate time difference in milliseconds in C/C++ waqasdaar Programming 19 03-17-2009 05:02 PM
calculate time difference between 2 formatted timestamps nickleus Linux - General 3 05-12-2006 11:08 PM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration