Calculate total time with min and sec.

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Question_dev
Posts: 88
Joined: Fri Aug 24, 2007 8:23 pm

Calculate total time with min and sec.

Post by Question_dev »

Hii,

I tried to calculate the total time using the start time (min and sec are saved in chars) and the end time (min and sec stored in chars).

Now i want to calculate the time difference.
I thought about something like :

1. check if startmin > endmin :
- if 1 then : (60 - startmin) + endmin
- if 0 then : endmin - startmin
2. for seconds : i really don't know lol

Hope someone can help !
Tnx

(the max time should be 3 or 2 minutes)
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

I won't question what you're actually doing because I don't think I want to know...

Convert it all to seconds to get the difference.

Code: Select all

int starttotal = startmin + (startsec * 60);
int endtotal = endmin + (endsec * 60);
int diffsec = endtotal - starttotal;
If you want to break it down to difference in minutes and seconds, then:

Code: Select all

int starttotal = startmin + (startsec * 60);
int endtotal = endmin + (endsec * 60);
int diffmin = (starttotal - endtotal)/60;
int diffsec = (startotal - endtotal) % 60;
Personally I wouldn't do any of the above, I always store it as seconds then convert it to minutes & seconds only when you need to display it.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

That won't work if it wraps on an hour. You need to take into account the hour as well... unless it wraps on a day. Then you need to take into account the day as well... unless it wraps on a month. Etc.

See why you don't use a CLOCK for timing? Use a TIMER for timing. That's what they're for.
Question_dev
Posts: 88
Joined: Fri Aug 24, 2007 8:23 pm

Post by Question_dev »

I know the max time is 3 or 2 minutes. So that should work

And if i use a timer, then i need a background timer.
I only know timers with a loop(while), and i can't add it to my function.

So if anyone has a background timer, plz post.

@IWN : What if the starttime is (17):59:59 and endtime (18):02:59 ?
Then your code doesn't work..

Tnx
Cyaaa
User avatar
jean
Posts: 489
Joined: Sat Jan 05, 2008 2:44 am

Post by jean »

No hope is left for us.
Question_dev
Posts: 88
Joined: Fri Aug 24, 2007 8:23 pm

Post by Question_dev »

I retried and here's the code ! :
(The output is totalmin)

Code: Select all

	int totalmin = 0;

	if(startmin > endmin){
		totalmin = (60-startmin)+endmin;
	}
	else if&#40;startmin < endmin&#41;&#123;  //Normal
		totalmin = endmin-startmin;
	&#125;
	
	int totalsec = startsec+endsec;
		
	if&#40;totalsec >= 60&#41;&#123;
		int secinmin = totalsec/60;
		totalmin += secinmin;
	&#125;
I only have one prob : The output is sometimes ex . 2.5604245, how can i have only 2 numbers after 2. ?
quadrizo
Posts: 21
Joined: Thu Aug 23, 2007 10:21 pm

Post by quadrizo »

IMG !!
try again ...
the answer was given before by Insert_witty_name just a "if (startmin <endmin)" to add
User avatar
jean
Posts: 489
Joined: Sat Jan 05, 2008 2:44 am

Post by jean »

OH, MY LORD, WHY DID YOU LEFT US?? I sometimes wonder if it's too hard for you to understand that this isn't elementary school, or if you make it on purpose to laugh while reading upset people's answers...
Even if i already know the "solution" to your HUGE problem, i tried search it on google, just to see. 7 seconds, solution found in 2nd search result for "printf formatting" ...what the @#*?^|!

Code: Select all

printf&#40;"%8.2f", value&#41;;
This doesn't round up your value, just diplay it as you want...if you don't understand what's the difference between a binary storage and it's representations, please don't come back asking.
Question_dev
Posts: 88
Joined: Fri Aug 24, 2007 8:23 pm

Post by Question_dev »

quadrizo wrote:IMG !!
try again ...
the answer was given before by Insert_witty_name just a "if (startmin <endmin)" to add
I tried it and it doesn't work. If u looked better, u should see the code is wrong.

@jean: I think you didn't understand my post. But its ok, i also found the solution, 1 min after my post.

Cyaa
quadrizo
Posts: 21
Joined: Thu Aug 23, 2007 10:21 pm

Post by quadrizo »

you should just invert min and sec and add a if ....
try to understand before to say that is wrong ...

Code: Select all

int starttotal = startsec + &#40;startmin * 60&#41;;
int endtotal = endsec + &#40;endmin * 60&#41;;
if&#40;startmin>endmin&#41;
    endtotal+=60*60;
int diffmin = &#40; endtotal-starttotal&#41;/60;
int diffsec = &#40; endtotal-starttotal&#41; % 60;
Maxiime
Posts: 18
Joined: Thu Jun 19, 2008 11:34 pm

Post by Maxiime »

Question dev, your code works, but what if there's only a difference of seconds ?

You have to add something like :

Code: Select all

if&#40;startmin == endmin&#41;&#123;
 //calculate your seconds
&#125; 
Cy
Post Reply