Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Help with subtracting time values?
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 12-27-2003   #1 (permalink)
cyberkid
Just Joined!
 
Join Date: Dec 2003
Posts: 2
Help with subtracting time values?

Hi All,

Can some one help.
How do I go about writing a script to subtract 1 time value from another?
ie:

16:41:39 -
15:23:35
-----------
result =?


Thanks

Newbie.
cyberkid is offline   Reply With Quote
Old 12-28-2003   #2 (permalink)
Jman
Just Joined!
 
Join Date: Dec 2003
Posts: 14
I'm not experienced in script programming, but the standard date program may help. Give it the date, like this:
Code:
date -d 16:41:39 +%s
and it will output the date in seconds. In other words, it outputs the current date at that time in seconds.

Hope this gets you started.
Jman is offline   Reply With Quote
Old 12-29-2003   #3 (permalink)
zyrotec
Just Joined!
 
Join Date: Dec 2003
Posts: 1
Help subtracting time

This is an example, use it however you want. I am not sure how much you know so I will not bore you explaining what I did, unless you ask.

William

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`
zyrotec is offline   Reply With Quote
Old 12-29-2003   #4 (permalink)
cyberkid
Just Joined!
 
Join Date: Dec 2003
Posts: 2
@Jman

Thanks, but I'm using a minimal linux distro & it doesn't have this option.
Plus converting back, when finished with the calcs is a bit of a worry.

@zyrotec

Many thanks,
This is great saves me a lot of heart-ache & head scratching. It'll get me well on my way.


CK. :wink:
cyberkid is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 05:41 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0