| View previous topic :: View next topic |
| Author |
Message |
Question_dev
Joined: 24 Aug 2007 Posts: 88
|
Posted: Thu Jun 19, 2008 6:06 am Post subject: Calculate total time with min and sec. |
|
|
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) |
|
| Back to top |
|
 |
Insert_witty_name
Joined: 10 May 2006 Posts: 376
|
Posted: Thu Jun 19, 2008 9:18 am Post subject: |
|
|
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: | 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: | 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. |
|
| Back to top |
|
 |
J.F.
Joined: 22 Feb 2004 Posts: 2906
|
Posted: Thu Jun 19, 2008 11:30 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Question_dev
Joined: 24 Aug 2007 Posts: 88
|
Posted: Thu Jun 19, 2008 3:24 pm Post subject: |
|
|
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 |
|
| Back to top |
|
 |
jean

Joined: 05 Jan 2008 Posts: 489
|
Posted: Thu Jun 19, 2008 6:09 pm Post subject: |
|
|
| No hope is left for us. |
|
| Back to top |
|
 |
Question_dev
Joined: 24 Aug 2007 Posts: 88
|
Posted: Thu Jun 19, 2008 8:18 pm Post subject: |
|
|
I retried and here's the code ! :
(The output is totalmin)
| Code: |
int totalmin = 0;
if(startmin > endmin){
totalmin = (60-startmin)+endmin;
}
else if(startmin < endmin){ //Normal
totalmin = endmin-startmin;
}
int totalsec = startsec+endsec;
if(totalsec >= 60){
int secinmin = totalsec/60;
totalmin += secinmin;
}
|
I only have one prob : The output is sometimes ex . 2.5604245, how can i have only 2 numbers after 2. ? |
|
| Back to top |
|
 |
quadrizo
Joined: 23 Aug 2007 Posts: 21
|
Posted: Thu Jun 19, 2008 9:49 pm Post subject: |
|
|
IMG !!
try again ...
the answer was given before by Insert_witty_name just a "if (startmin <endmin)" to add |
|
| Back to top |
|
 |
jean

Joined: 05 Jan 2008 Posts: 489
|
Posted: Thu Jun 19, 2008 9:49 pm Post subject: |
|
|
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: | | printf("%8.2f", value); |
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. |
|
| Back to top |
|
 |
Question_dev
Joined: 24 Aug 2007 Posts: 88
|
Posted: Thu Jun 19, 2008 9:57 pm Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
quadrizo
Joined: 23 Aug 2007 Posts: 21
|
Posted: Thu Jun 19, 2008 10:17 pm Post subject: |
|
|
you should just invert min and sec and add a if ....
try to understand before to say that is wrong ...
| Code: |
int starttotal = startsec + (startmin * 60);
int endtotal = endsec + (endmin * 60);
if(startmin>endmin)
endtotal+=60*60;
int diffmin = ( endtotal-starttotal)/60;
int diffsec = ( endtotal-starttotal) % 60;
|
|
|
| Back to top |
|
 |
Maxiime
Joined: 19 Jun 2008 Posts: 18
|
Posted: Fri Jun 20, 2008 2:02 am Post subject: |
|
|
Question dev, your code works, but what if there's only a difference of seconds ?
You have to add something like :
| Code: |
if(startmin == endmin){
//calculate your seconds
}
|
Cy |
|
| Back to top |
|
 |
|