LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-24-2013, 05:02 AM   #1
rakrr786
Member
 
Registered: May 2012
Posts: 34

Rep: Reputation: Disabled
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.
 
Old 04-24-2013, 05:27 AM   #2
kareempharmacist
Member
 
Registered: Jan 2012
Location: Cairo, Egypt
Distribution: MX Linux, Ubuntu 18.04
Posts: 336

Rep: Reputation: 42
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"..
 
1 members found this post helpful.
Old 04-24-2013, 05:35 AM   #3
rakrr786
Member
 
Registered: May 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
sorry for posting here

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

yup i m converting the format to seconds.

Last edited by rakrr786; 04-24-2013 at 05:48 AM.
 
Old 04-24-2013, 05:51 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,467

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
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!
 
3 members found this post helpful.
Old 04-24-2013, 07:08 AM   #5
rakrr786
Member
 
Registered: May 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
thanks karim and tentenths

was stuck at converting seconds to hh:mm:ss format
 
Old 04-25-2013, 12:56 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

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

http://www.fresse.org/dateutils/
 
1 members found this post helpful.
Old 04-25-2013, 11:49 PM   #7
rakrr786
Member
 
Registered: May 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
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

Last edited by rakrr786; 04-25-2013 at 11:53 PM.
 
Old 04-26-2013, 02:21 AM   #8
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,467

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by rakrr786 View Post
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}`
 
Old 04-26-2013, 03:29 AM   #9
rakrr786
Member
 
Registered: May 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
yup

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

was giving blank data in totaldiff variable and i can't find why
 
Old 04-26-2013, 03:32 AM   #10
rakrr786
Member
 
Registered: May 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
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

Last edited by rakrr786; 04-26-2013 at 03:37 AM.
 
Old 04-26-2013, 03:32 AM   #11
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,467

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Quote:
Originally Posted by rakrr786 View Post
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.
 
Old 04-26-2013, 03:36 AM   #12
rakrr786
Member
 
Registered: May 2012
Posts: 34

Original Poster
Rep: Reputation: Disabled
any idea about the awk command
 
Old 04-26-2013, 03:45 AM   #13
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,467

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
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}
 
1 members found this post helpful.
Old 05-27-2021, 09:36 AM   #14
mimeini
LQ Newbie
 
Registered: May 2021
Posts: 1

Rep: Reputation: Disabled
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}`
 
Old 05-27-2021, 10:11 AM   #15
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,131
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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 "---------------------------"
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
elapsed time calculation from two date strings - shell script hisunday Linux - General 5 03-29-2012 07:11 AM
elapsed time calculation from two date strings hisunday Linux - Newbie 3 03-28-2012 06:00 PM
problem with process time calculation aloktiagi Linux - Newbie 3 08-11-2009 12:05 PM
Adaptive ping round-trip time calculation vino87 Linux - Networking 0 07-01-2009 02:58 AM
Time source for fps calculation addy86 Programming 1 09-05-2005 04:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration