Time difference in minutes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Time difference in minutes
# 8  
Old 10-16-2014
Try with the original time format in post#1:
Code:
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:
Code:
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

Last edited by Scrutinizer; 10-16-2014 at 05:28 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 9  
Old 10-16-2014
Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk -F: '{n=$1*60+$2+$3/60; d=(n-p)} d<0{d+=1440} NR>1{printf "%.0f\n", d} {p=n}' file

----
@Akshay what about date changes (around midnight)?
----
@Jay: On Solaris use /usr/xpg4/bin/awk rather than awk
I just noticed that user has posted new data in #6, which also contains date field, did you noticed ?
# 10  
Old 10-16-2014
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.

Code:
$ 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

Code:
$ 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 ?

Last edited by Scrutinizer; 10-16-2014 at 05:35 AM.. Reason: code tags
# 11  
Old 10-16-2014
@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



----
Quote:
Originally Posted by Akshay Hegde
I just noticed that user has posted new data in #6, which also contains date field, did you noticed ?
You are right the OP changed it.
# 12  
Old 10-16-2014
OOps.. ok that worked !!!!

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

Thanks a ton !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file creation Time minutes and if file older then 5 minutes execute some stuff

Hello all, Info: System RedHat 7.5 I need to create a script that based on the creation time, if the file is older then 5 minutes then execute some stuff, if not exit. I thought to get the creation time and minutes like this. CreationTime=$(stat -c %y /tmp/test.log | awk -F" " '{ print... (3 Replies)
Discussion started by: charli1
3 Replies

2. Shell Programming and Scripting

How to get a time minus 60 minutes?

Hello, date --date '-60 min ago' +'%Y-%m-%d %H:%M:%S,%3N' Above command gives the date and time minus 60 minutes but the problem i am facing is, i do not want to hardcode the value 60 it is stored in a variable var=60 now if i run below command , i get error date --date '-$var min... (3 Replies)
Discussion started by: Ramneekgupta91
3 Replies

3. Shell Programming and Scripting

grep the time within given minutes

Mar 26 15:25:11 : jdoe : TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - Mar 26 15:28:52 : jdoe : 3 incorrect password attempts ; TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; COMMAND=/usr/bin/su - Mar 25 12:23:07 : jdoe : TTY=pts/2 ; PWD=/home/jdoe ; USER=root ; ... (6 Replies)
Discussion started by: Daniel Gate
6 Replies

4. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

5. Shell Programming and Scripting

subtract minutes from time

i have the time 20100421043335 in format (date +%Y%m%d%H%M%S),and i want to be able to get the previous time 2 minutes ago,which is 20100421043135 (9 Replies)
Discussion started by: tomjones
9 Replies

6. Shell Programming and Scripting

Process Time in hours or minutes only

Hi i want to print the time of a process in hours only..(or) in minutes only.Is there anyway to print the process such like that when i give the commnand like following #ps -eo pid,time PID TIME 412 01:49:32 481 00:03 it shows in HH:MM:SS format: Could anyone... (1 Reply)
Discussion started by: srikanthg
1 Replies

7. Shell Programming and Scripting

how to find the time before 30 minutes

Hi All, I want to find out the time before 30 minutes. I am able to do with in hours limit. date Fri Aug 21 06:50:00 BST 2009 TZ=CST+1 date Fri Aug 21 04:50:02 CST 2009 Can any one please help me (6 Replies)
Discussion started by: vikash_k
6 Replies

8. Shell Programming and Scripting

Difference in day-hours-minutes-seconds format

Hi experts, I am reading two log files and passing dates as output to a txt file. Code is given below: echo "Start Time:" >> Report.txt cat start.log | while read LINE1 do echo $DATE1 >> Report.txt done echo "End Time:" >> Report.txt cat end.log | while read LINE2 ... (7 Replies)
Discussion started by: Sreejith_VK
7 Replies

9. UNIX for Advanced & Expert Users

Adding # minutes to current time...

Hi all, Looking for a way to add lets say 10 minutes to the current time output should look like 7:15 AM or 7:15 PM. I know that gdate could do this for me but unfortunately its not available on the system I'm working on. So if any one know any way I can accomplish this using the date command it... (7 Replies)
Discussion started by: gptavares
7 Replies

10. Shell Programming and Scripting

how to display time in minutes n seconds...

Hi all, may i know how to display time in minutes and seconds(may be milliseconds and even smaller that ) in shell scripts.... (1 Reply)
Discussion started by: santy
1 Replies
Login or Register to Ask a Question