Time difference in minutes

Hi Folks,

I have a text file that has only time in the format HH:MM:SS like seen below.

21:36:17
23:52:08

I need to find the difference in minutes alone from this text file so the result would be 136.

Thanks
Jay

Hello jay,

Assuming your file is in following format. Then following may help.
File:

cat time_diff
21:36:17        23:52:08
awk 'function time_details(A){split(A,X,":"); TOT=X[1] * 3600 + X[2] * 60 + X[3]; return TOT} {print time_details($2) - time_details($1)}'  time_diff

Thanks,
R. Singh

Follow this thread for time being there are many solutions on forum you can easily search

Hi,

Thanks much for the reply.

I have both the times line after line. The code that you gave me will work only if the time is in the same line i believe.

Any way we could find the difference from this 2 lines ?

Thanks
Jay

@RavinderSingh: What about date changes? Also the time should be in minutes.
@Jay, could you post what tried yourself?

Hi,

I can fetch the time and date in the format seen below.

10/15/14 21:36:17
10/15/14 23:52:08

Now i need to find the difference in minutes or seconds.

I get the following error when i try to run the command given by R.Singh.

awk: syntax error near line 1
awk: bailing out near line 1

Thanks
Jay

Sample input line by line start time, endtime, and so on

[akshay@nio tmp]$ cat file
21:36:17
23:52:08
20:26:17
22:32:08

Code executed

[akshay@nio tmp]$ cat s.awk
function dfor(time, a)
{ 
	  split(time, a, /:/); 
	  return 3600*a[1] + 60*a[2] + a[3] 
}
function hms(s, h,m)
{
	  h=int(s/3600);
	  s=s-(h*3600);
	  m=int(s/60);
	  s=s-(m*60);
	  return sprintf("%02d:%02d:%02d", h, m, s);
}


FNR==1{ print "Start","END","Seconds","Minutes","HH:MM:SS" }

{ 
	s[v = (FNR%2)?"s":"e" ] = $1	
	if(v=="e")
	{
		seconds = dfor(s["e"])-dfor(s["s"])
		print s["s"],s["e"],seconds,seconds/60,hms(seconds)
	}
}

Output

[akshay@nio tmp]$ awk -f s.awk file
Start END Seconds Minutes HH:MM:SS
21:36:17 23:52:08 8151 135.85 02:15:51
20:26:17 22:32:08 7551 125.85 02:05:51
1 Like

Try with the original time format in post#1:

awk -F: '{m=$1*60+$2+$3/60; d=(m-p)} d<0{d+=1440} NR>1{printf "%.0f\n", d} {p=m}' file

If the difference is negative it is assumed there is one day difference and 24*60-1440 seconds are added.

----
Better variable names:

awk -F: '{mins=$1*60+$2+$3/60; diff=(mins-prev)} diff<0{diff+=1440} NR>1{printf "%.0f\n", diff} {prev=mins}' file

----
@Akshay what about date changes (around midnight)?
----
@Jay: On Solaris use /usr/xpg4/bin/awk rather than awk

2 Likes

I just noticed that user has posted new data in #6 , which also contains date field, did you noticed ?

When i try this, i am getting answer as 16, but i am supposed to get 136 when i use it with date stamp as well.

$ awk -F: '{mins=$1*60+$2+$3/60; diff=(mins-prev)} diff<0{diff+=1440} NR>1{printf "%.0f\n", diff} {prev=mins}' only_time.txt
16
$ more only_time.txt
10/15/14 21:36:17
10/15/14 23:52:08

and what if the time has elapsed 8 more minutes and had moved to the next day ?

@jay, you changed your date/time format between post #1 and #6. Please don't change your date/time format like that, because it really matters..

The line you tried works with your original format in post #1 and it takes date changes into account as explained in post #8

----

You are right the OP changed it.

OOps.. ok that worked !!!!

Thanks much for the replies... Great learning !!!!

Thanks a ton !!!