Unix function to calcuate the difference in time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix function to calcuate the difference in time
# 1  
Old 11-02-2007
Unix function to calcuate the difference in time

HI ,

I need to get the timedifference between two values... which funcation will help

eg: difference betweem 19:22 and 19:43 should give 21 mins
# 2  
Old 11-02-2007
You must write your own function.
You can do something like that (bash or ksh solution) :
Code:
shopt -s extglob # Bash Only

diff_times()
{
   local time1=$1
   local time2=$2
   typeset -i t1 t2 t

   if [[ "$time1" != ?([0-9])[0-9]:?([0-9])[0-9] ]]
   then
      echo "Invalid time '${time1}'" 1>&2
      return 1
   fi

   if [[ "$time2" != ?([0-9])[0-9]:?([0-9])[0-9] ]]
   then
      echo "Invalid time '${time2}'" 1>&2
      return 1
   fi

   (( t1 = ${time1%:*} * 60 + ${time1#*:} ))
   (( t2 = ${time2%:*} * 60 + ${time2#*:} ))
   (( t = t2 - t1 ))
   if (( t <0 ))
   then
      (( t = -t ))
   fi

   echo $t
}

start="19:22"
end="19:43"
echo "Difference between $start and $end is $(diff_times "$start" "$end")"

Output :
Code:
Difference between 19:22 and 19:43 is 21

Jean-Pierre.
# 3  
Old 11-02-2007
i don't know which platform you are in , assuming you have GNU date
Code:
# date +%s -d "19:22"
1194002520
# date +%s -d "19:43"
1194003780
# echo "1194003780 - 1194002520" | bc
1260
# echo "(1194003780 - 1194002520)/60" | bc
21

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date time difference in UNIX shell script

There are 2 dates, Tue Oct 1 13:40:19 2013 Sun Sept 30 10:26:23 2013 I have multiple dates like the above one. How do I calculate the date time difference and display in another column in Shell script. Please help. (3 Replies)
Discussion started by: tanmoysays
3 Replies

2. Shell Programming and Scripting

Get the time difference between two consecutive line in UNIX perl script

Hi All :o, I have some log files which contains these informations: 2013-04-24 09:11:34.018 INFO XXXXXXXXXXXX 2013-04-24 09:11:34.029 INFO YYYYYYYYYYYY 2013-04-24 09:11:34.039 INFO ZZZZZZZZZZZZZZZ 2013-04-24 09:12:21.295 INFO TTTTTTTTTTTTTTT 2013-04-24 09:12:21.489 INFO... (3 Replies)
Discussion started by: shariquehabib
3 Replies

3. AIX

How to calcuate total number of weeks?

Hi anyone can help? How to calculate total number of weeks from a specify date, for example, 01 Jan 2012. Thx! https://www.unix.com/images/misc/progress.gif (1 Reply)
Discussion started by: rayray2013
1 Replies

4. Shell Programming and Scripting

Time difference between two time stamps

Hi Friends, I have 2 varaibles which contain START=`date '+ %m/%d/%y %H:%M:%S'` END=`date '+ %m/%d/%y %H:%M:%S'` i want the time difference between the two variables in Seconds. Plz help. (2 Replies)
Discussion started by: i150371485
2 Replies

5. UNIX for Advanced & Expert Users

Help with Calculating time difference between many directories in UNIX

A report needs to come some what similar to this No of elements Stream Batch No Load time A B C D A,B,C im able to get quite easily wc -l /usr/local/intranet/areas/prod/output/SRGW_0?/*/MESSAGE_T.dat O/P of above command. A B C ... (1 Reply)
Discussion started by: peckenson
1 Replies

6. Shell Programming and Scripting

Difference Time Unix Shell

I have 2 variables MTIME="Jan_2_2012_23:55:49" SCH_TIME="Jan_03_2012_00:32:28" I want to find the time taken (in seconds) between MTIME and SCH_TIME. Is there any way by which this can be done in Unix Shell Script? (4 Replies)
Discussion started by: ankitncr
4 Replies

7. 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

8. Shell Programming and Scripting

Find Time difference in Unix

Hi, START_TIME :- "10-NOV-2009 00:00:04" STOP_TIME :- "10-NOV-2009 00:05:47" Please help to find difference between these two. Searched for the same topic but did not find an answer for the same time format :( Regards, Robin (3 Replies)
Discussion started by: robinbannis
3 Replies

9. Shell Programming and Scripting

Time difference in Minute in UNIX

is there any ways to get the time difference between 2 dates in UNIX? for example, For below date the outut should come 22 minutes startdate enddate ========= ======= 06/17/2008 13:25 06/17/2008 13:47 For, below date, the output should come 1462 minutes ... (5 Replies)
Discussion started by: Amit.Sagpariya
5 Replies

10. Shell Programming and Scripting

calcuate the week number of a given date

Hi All, can any one help me fix the error in this - i am still a novice in shell programming. I got this code after some googling now the code works with all the dates( as much as i know) except for 08 th and 09th of every month. can any one of you please help me fix this issue? Thanks in... (11 Replies)
Discussion started by: ahmedwaseem2000
11 Replies
Login or Register to Ask a Question