Sponsored Content
Top Forums Shell Programming and Scripting Unix function to calcuate the difference in time Post 302143621 by aigles on Friday 2nd of November 2007 07:00:01 AM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
difftime(3)						     Library Functions Manual						       difftime(3)

NAME
difftime, difftime64 - Compares time values LIBRARY
Standard C Library (libc.so, libc.a) SYNOPSIS
#include <time.h> double difftime( time_t time1, time_t time2); The following function declaration is a Compaq extension and does not conform to current standards. This function is provided to support the time64_t data type and is accessible only when the _TIME64_T feature macro is defined during compilation. #include <time.h> double difftime64( time64_t time64_1, time64_t time64_2); STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: difftime(): XPG4, XPG4-UNIX Refer to the standards(5) reference page for more information about industry standards and associated tags. PARAMETERS
Specifies a time value of type time_t expressed in seconds. Specifies a time value of type time_t expressed in seconds. Specifies a time value of type time64_t expressed in seconds. Specifies a time value of type time64_t expressed in seconds. DESCRIPTION
The difftime() function returns a signed time value in seconds that is the difference between the values of the time1 and time2 parameters, also expressed in seconds. The difftime64() function works exactly as the difftime() function, but accepts arguments of type time64_t type instead of time_t. This function declaration is only available when the _TIME64_T feature macro is defined during compilation. See the time(3) reference page for additional details. NOTES
The difftime() and difftime64() functions are supported for multithreaded applications. RETURN VALUES
Upon successful completion the difftime() function returns a value, expressed in seconds, that is the difference between the values of parameters time1 and time2. Upon successful completion the difftime64() function returns a value,expressed in seconds, that is the difference between the values of parameters time64_1 and time64_2. RELATED INFORMATION
Functions: ctime(3), time(3), timezone(3) Standards: standards(5) delim off difftime(3)
All times are GMT -4. The time now is 03:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy