LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   time difference calculation (https://www.linuxquestions.org/questions/linux-newbie-8/time-difference-calculation-4175459414/)

rakrr786 04-24-2013 05:02 AM

time difference calculation
 
Hi i am trying to calculate the difference between 2 time stamps but unable to sort out hw to do it.

my time format is like 05:36:11 (24 hr clock) can anyone suggest a method or utility so i can calculate a time diff in shell script.

kareempharmacist 04-24-2013 05:27 AM

more details please.. is it a bash script?
did you read about zsh which is more powerful?
I think your problem is about data types ..may be converting it to another format will be helpful..
asking this question in the right forum will help you a lot which is the "Programming forum"..

rakrr786 04-24-2013 05:35 AM

sorry for posting here

yes it is a bash script
no i havenot looked in zsh

yup i m converting the format to seconds.

TenTenths 04-24-2013 05:51 AM

The following should give you some clues:

Code:

#!/bin/bash

# Time Arithmetic

TIME1=05:36:11
TIME2=06:46:00

# Convert the times to seconds from the Epoch
SEC1=`date +%s -d ${TIME1}`
SEC2=`date +%s -d ${TIME2}`

# Use expr to do the math, let's say TIME1 was the start and TIME2 was the finish
DIFFSEC=`expr ${SEC2} - ${SEC1}`

echo Start ${TIME1}
echo Finish ${TIME2}

echo Took ${DIFFSEC} seconds.

# And use date to convert the seconds back to something more meaningful
echo Took `date +%H:%M:%S -ud @${DIFFSEC}`

This gives the output:
Code:

Start 05:36:11
Finish 06:46:00
Took 4189 seconds.
Took 01:09:49

Enjoy!

rakrr786 04-24-2013 07:08 AM

thanks karim and tentenths

was stuck at converting seconds to hh:mm:ss format

David the H. 04-25-2013 12:56 PM

You might be interested in these advanced date processing utilities, including ddiff, which does just what you're asking.

http://www.fresse.org/dateutils/

rakrr786 04-25-2013 11:49 PM

I dont think i will get those utilities even though it does exactly what i need

any how i figured out the arithmetics as below

Quote:


hrs1=3600
hrs2=60

SEC1=`date -d "${starttime}" +%s` ##in hh:mm:ss formant to convert in seconds from epoch time
SEC2=`date -d "${endtime}" +%s`
difftemp=`expr "${SEC2}" - "${SEC1}"`
let hh=$difftemp/$hrs1
let mm=$difftemp/$hrs2
let ss=$difftemp-$mm\*$hrs2
totaldiff=`echo "$hh":"$mm":"$ss"`

thanks for ur help and i dont know hw to give reputation and cant see a white scale either

TenTenths 04-26-2013 02:21 AM

Quote:

Originally Posted by rakrr786 (Post 4939057)
any how i figured out the arithmetics as below

Just curious, was there any particular reason for doing all the arithmetic, multiplications and divisions instead of just:

Code:

SEC1=`date -d "${starttime}" +%s` ##in hh:mm:ss formant to convert in seconds from epoch time
SEC2=`date -d "${endtime}" +%s`
difftemp=`expr "${SEC2}" - "${SEC1}"`
totaldiff=`date +%H:%M:%S -ud @${difftemp}`


rakrr786 04-26-2013 03:29 AM

yup

totaldiff=`date +%H:%M:%S -ud @${difftemp}`

was giving blank data in totaldiff variable and i can't find why

rakrr786 04-26-2013 03:32 AM

also i have one more thing
tempapp.txt
06:32:33

awk -F : '{ hours = 3600*$1 }{ minutes = 60*$2 }{ seconds = $3 }{SEC1 = hours+minutes+seconds} END { print SEC1 }' tempapp.txt
23553


gives proper output but i cannot use SEC1 once i come out of awk and also cannot assign the output to any other variable i assigned it gives null value

TenTenths 04-26-2013 03:32 AM

Quote:

Originally Posted by rakrr786 (Post 4939147)
yup

totaldiff=`date +%H:%M:%S -ud @${difftemp}`

was giving blank data in totaldiff variable and i can't find why

AH, ok, worked fine on my servers: GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)

Anyways, was just curious, good you got sorted.

rakrr786 04-26-2013 03:36 AM

any idea about the awk command

TenTenths 04-26-2013 03:45 AM

Variables created inside AWK programs aren't exposed to the bash shell, you can however assign the output of AWK to a variable:

Code:

#!/bin/bash

SEC1=`awk -F : '{ hours = 3600*$1 }{ minutes = 60*$2 }{ seconds = $3 }{SEC1 = hours+minutes+seconds} END { print SEC1 }' tempapp.txt`

echo ${SEC1}


mimeini 05-27-2021 09:36 AM

Thanks to TenTenth
 
Thanks to TenTenth for this replay. I think that is a cool solution and just what i need.

#!/bin/bash

# Time Arithmetic

TIME1=05:36:11
TIME2=06:46:00

# Convert the times to seconds from the Epoch
SEC1=`date +%s -d ${TIME1}`
SEC2=`date +%s -d ${TIME2}`

# Use expr to do the math, let's say TIME1 was the start and TIME2 was the finish
DIFFSEC=`expr ${SEC2} - ${SEC1}`

echo Start ${TIME1}
echo Finish ${TIME2}

echo Took ${DIFFSEC} seconds.

# And use date to convert the seconds back to something more meaningful
echo Took `date +%H:%M:%S -ud @${DIFFSEC}`

teckk 05-27-2021 10:11 AM

Code:

#!usr/bin/env/bash

clear

echo -e "Video segment calculator. Calculates run times of segments.
Enter time with format 01:23:34\n"

read -p "Enter Start time: " start_time
read -p "Enter Stop time:  " stop_time

StartDate=$(date -d "$start_time" +"%s")
EndDate=$(date -d "$stop_time" +"%s")
Diff=$(date -d "0 $EndDate sec - $StartDate sec" +"%H:%M:%S")

echo "                ----------"
echo "Difference is:    "$Diff""
echo "---------------------------"



All times are GMT -5. The time now is 08:10 AM.