Time Stap Diff using Bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Time Stap Diff using Bash
# 1  
Old 06-19-2013
Time Stap Diff using Bash

hi all

kindly i want to subtract to time stamps

like:
Code:
t1=08:35:20
t2=08:36:58
 
diff=00:01:38  <<<<
or
diff=01:38   <<<<

using bash
thanks alot
# 2  
Old 06-19-2013
something like this
Code:
$ diffdate() { echo $(($(date -j -f "%H:%M:%S" "$2" "+%s") - $(date -j -f "%H:%M:%S" "$1" "+%s"))); }
$ diffdate "08:35:20" "08:36:58" | awk '{print int($1/60) ":" $1%60}'
1:38

Since you are not specifying the date, this will give incorrect results if you are trying to subtract timestamps rolling over to next date
# 3  
Old 06-19-2013
Code:
 
#!/bin/ksh
t1=08:35:20
t2=13:36:58
today=$(date '+%Y-%m-%d')
EndSec=$(date  -d"$today $t2" '+%s')
StartSec=$(date  -d"$today $t1" '+%s')
Diff=$(expr $EndSec - $StartSec )
Sec=$(expr $Diff % 60 );Rem=$(expr $Diff / 60 );
min=$(expr $Rem % 60 ) ; Hrs=$(expr $Rem / 60 )
[ -z "$Hrs" ] && Hrs=0; [ -z "$Sec" ] && Sec=0; [ -z "$min" ] && min=0

echo "Diff is $Hrs:$min:$Sec"

# 4  
Old 06-19-2013
Quote:
Originally Posted by rajamadhavan
something like this
Code:
$ diffdate() { echo $(($(date -j -f "%H:%M:%S" "$2" "+%s") - $(date -j -f "%H:%M:%S" "$1" "+%s"))); }
$ diffdate "08:35:20" "08:36:58" | awk '{print int($1/60) ":" $1%60}'
1:38

Since you are not specifying the date, this will give incorrect results if you are trying to subtract timestamps rolling over to next date

this is the output kindly help
Code:
A: syntax error at line 1: `(' unexpected

---------- Post updated at 08:19 AM ---------- Previous update was at 08:19 AM ----------

Quote:
Originally Posted by pravin27
Code:
 
#!/bin/ksh
t1=08:35:20
t2=13:36:58
today=$(date '+%Y-%m-%d')
EndSec=$(date  -d"$today $t2" '+%s')
StartSec=$(date  -d"$today $t1" '+%s')
Diff=$(expr $EndSec - $StartSec )
Sec=$(expr $Diff % 60 );Rem=$(expr $Diff / 60 );
min=$(expr $Rem % 60 ) ; Hrs=$(expr $Rem / 60 )
[ -z "$Hrs" ] && Hrs=0; [ -z "$Sec" ] && Sec=0; [ -z "$min" ] && min=0
 
echo "Diff is $Hrs:$min:$Sec"


this is the output kindly help
Code:
date: illegal option -- d
date: illegal option -- 2
date: illegal option -- 0
date: illegal option -- 1
date: illegal option -- 3
date: illegal option -- -
date: illegal option -- 0
date: illegal option -- 6
date: illegal option -- -
date: illegal option -- 1
date: illegal option -- 9
date: illegal option --  
date: illegal option -- 1
date: illegal option -- 3
date: illegal option -- :
date: illegal option -- 3
date: illegal option -- 6
date: illegal option -- :
date: illegal option -- 5
date: illegal option -- 8
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
date: illegal option -- d
date: illegal option -- 2
date: illegal option -- 0
date: illegal option -- 1
date: illegal option -- 3
date: illegal option -- -
date: illegal option -- 0
date: illegal option -- 6
date: illegal option -- -
date: illegal option -- 1
date: illegal option -- 9
date: illegal option --  
date: illegal option -- 0
date: illegal option -- 8
date: illegal option -- :
date: illegal option -- 3
date: illegal option -- 5
date: illegal option -- :
date: illegal option -- 2
date: illegal option -- 0
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error
expr: syntax error

# 5  
Old 06-19-2013
Code:
#!/bin/bash
#
#
#

# define times to compare
t1="08:35:20"
t2="08:36:58"

# convert to seconds since epoch
t1_c=$(date --date=$t1 +%s)
t2_c=$(date --date=$t2 +%s)

# subtract the seconds
diff=$(echo "scale=2; $t2_c - $t1_c" | bc)

# convert the diff back into normal time
# showing the difference between the two
# times
the_diff=$(date -d @+$diff +%M:%S)
echo "Time difference for $t1 and $t2 is $the_diff."

# done
exit 0

./subDATE.sh
Time difference for 08:35:20 and 08:36:58 is 01:38.

# 6  
Old 06-19-2013
Going forward please provide details regarding which system that you are working on:
Code:
uname -a

The solution posted by pravin27 & in2nix4life is assuming you have GNU date.

I would recommend referring this thread in FAQ section.
# 7  
Old 06-19-2013
Quote:
Originally Posted by Yoda
Going forward please provide details regarding which system that you are working on:
Code:
uname -a

The solution posted by pravin27 & in2nix4life is assuming you have GNU date.

I would recommend referring this thread in FAQ section.


its
Code:
sun 11

---------- Post updated at 01:06 PM ---------- Previous update was at 01:03 PM ----------

Quote:
Originally Posted by in2nix4life
Code:
#!/bin/bash
#
#
#
 
# define times to compare
t1="08:35:20"
t2="08:36:58"
 
# convert to seconds since epoch
t1_c=$(date --date=$t1 +%s)
t2_c=$(date --date=$t2 +%s)
 
# subtract the seconds
diff=$(echo "scale=2; $t2_c - $t1_c" | bc)
 
# convert the diff back into normal time
# showing the difference between the two
# times
the_diff=$(date -d @+$diff +%M:%S)
echo "Time difference for $t1 and $t2 is $the_diff."
 
# done
exit 0
 
./subDATE.sh
Time difference for 08:35:20 and 08:36:58 is 01:38.


Code:
date: illegal option -- date=08:35:20
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
date: illegal option -- date=08:36:58
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
syntax error on line 1, teletype
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
        date [-u] [+format]
        date -a [-]sss[.fff]
Time difference for 08:35:20 and 08:36:58 is .

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

2. Shell Programming and Scripting

Bash script to diff two arrays

Hi, I am quite scripting illiterate and have been trying to write a bash script to compare to two files which i have populated in two seperate arrays as below and confirmed that all the files are loaded into the array. IFS=$'\n' filea=($(find /var/tmp/dir1 -type f -follow -print))... (12 Replies)
Discussion started by: jlykke
12 Replies

3. Shell Programming and Scripting

Diff between two time in hours in last column

Dear All I want to diff between two time(FIRST 4 COLUMN) in hours in last column. Kindly help me for same. 2013-11-23 15:51:23 2013-11-23 12:20:06 BRC023 CG 2013-11-23 15:51:23 2013-11-23 12:20:08 BRC064CG 2013-11-23 15:51:23 2013-11-22 13:17:49 BLM003 NG 2013-11-23 15:51:23 2013-11-22... (9 Replies)
Discussion started by: jaydeep_sadaria
9 Replies

4. Shell Programming and Scripting

How to make diff show differences one line at a time and not group them?

Is there a way to tell diff to show differences one line at a time and not to group them? For example, I have two files: file1: line 1 line 2 line 3 diff line 4 diff line 5 diff line 6 line 7 file2: line 1 line 2 line 3 diff. line 4 diff. line 5 diff. line 6 line 7 (13 Replies)
Discussion started by: mmr11408
13 Replies

5. Shell Programming and Scripting

Time Diff in shell script

Hi all , i am trying to calculate time difference btw the script execution I am using solaris start_time=`date +%s` sleep 2 end_time=`date +%s` duration=`expr $end_time - $start_time` when i try to subtract i get the error line 13: %s - -time : syntax error: operand expected... (3 Replies)
Discussion started by: posner
3 Replies

6. Shell Programming and Scripting

time diff help

Input file: Tue Oct 21 12:56:35 2008 Started Tue Oct 21 12:56:39 2008 Completed Tue Oct 21 12:57:25 2008 Started Tue Oct 21 12:57:32 2008 Completed Tue Oct 21 12:58:12 2008 Started Tue Oct 21 12:58:50 2008 Completed Output required: Tue Oct 21 12:56:35 2008 Started Tue Oct 21... (2 Replies)
Discussion started by: uwork72
2 Replies

7. Shell Programming and Scripting

Bash uniq/ diff/ and other I cant figure it out

First off thank you for any help. Here is the problem. I have two text files that fit the same format. The first I created using an ls -d command and then with the help of the forums ran awk resulting in the fallowing output. W00CHZ0103345-I1CZ44 W00E6S1016722-I01JW159... (8 Replies)
Discussion started by: Movomito
8 Replies

8. Shell Programming and Scripting

diff in date/time ?

the o/p is : Start Time: 2008-02-19 00:13:16,338 End Time: 2008-02-19 23:43:17,974 How to find the diff in time..........? (1 Reply)
Discussion started by: ali560045
1 Replies

9. UNIX for Advanced & Expert Users

Diff b/w modification & updated time

Hi All, What is the difference b/w last modification time and last updated time. Thanks Sweta (1 Reply)
Discussion started by: sweta
1 Replies

10. UNIX for Dummies Questions & Answers

Help with date and time diff

Hi , Can somebody please help me with this. The date format is in MM/DD/YY HH24:MI:SS suppose I have a start_date=09/03/02 04:15:02 & Finish date= 09/04/02 07:13:51 I want to find out the difference between the two ie finish_time-start time. How can I do this. Morever I want the... (2 Replies)
Discussion started by: rooh
2 Replies
Login or Register to Ask a Question