Find the answer to your Linux question:
Results 1 to 3 of 3
Hello All , The below code will give the time difference between two times as defined . and it will give as shown below . this code is written by ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    45

    How to take the difference in minutes and seconds

    Hello All ,

    The below code will give the time difference between two times as defined . and it will give as shown below .
    this code is written by some one else ... this is working fine but my requirement as follows

    13:00:00
    14:00:00

    the present code will give the output 01:00:00 differnce but i need it has difference of 60 minutes and also second of 3600

    how i have to modify this code ? or How i have to write code ?

    please help me out ....

    Code:
    #variables
    #ft% are from the first time
    #st% are from the second time
    #dt% difference time
    
    #cut out the hours minutes seconds
    
    fth=`echo "$ft" | cut -c1-2`
    ftm=`echo "$ft" | cut -c4-5`
    fts=`echo "$ft" | cut -c7-8`
    sth=`echo "$st" | cut -c1-2`
    stm=`echo "$st" | cut -c4-5`
    sts=`echo "$st" | cut -c7-8`
    
    
    
    # do the math for seconds
    
    dts=`expr $sts - $fts`
    if [ $dts -lt 0 ]
    then
    	dts=`expr $dts + 60`
    	ftm=`expr $ftm + 1`
    fi
    for val in $dts
    do
    	if [ $val -lt 10 ]
    	then
    		dts=`echo "0$val"`
    	else
    		dts=`echo $val`
    	fi
    done
    
    
    
    # do the math for minutes
    
    dtm=`expr $stm - $ftm`
    if [ $dtm -lt 0 ]
    then
    	dtm=`expr $dtm + 60`
    	fth=`expr $fth + 1`
    fi
    for val in $dtm
    do
    	if [ $val -lt 10 ]
    	then
    		dtm=`echo "0$val"`
    	else
    		dtm=`echo $val`
    	fi
    done
    
    
    
    # do the math for hours
    
    dth=`expr $sth - $fth`
    
    
    
    # put dtime back together
    
    difftime=`echo $dth:$dtm:$dts`

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Are you serious? The variables $dth, $dtm, $dts already contain all you need to know. The difference in seconds is $dth * 3600 + $dtm * 60 + $dts, for example.

  3. #3
    Just Joined!
    Join Date
    Feb 2008
    Posts
    45

    yes

    sorry ... I just forgotten to convert ...!!! thanks for remembering..!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...