C++: Is there a function that converts seconds into time? (12:59:59)

Started by
15 comments, last by Woodsman 19 years, 7 months ago
Quote:Original post by GroZZleR
Quote:Original post by CJH
{seconds an int}
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int seconds = seconds % (60);


This would work too, but you never actually modify the seconds variable, which means your calculation of int seconds = (seconds) will be off. No?

I don't quite understand. He is modifying the seconds variable. On the last line. seconds = seconds % (60); That would effectively chop it off anywhere from 0 to 59.

Edit: Perhaps you mean that he is redeclaring the seconds variable which was used to hold the initial number of seconds. That could easily be fixed by changing variable names or dropping the int before the seconds on the last line.
Advertisement
The RHS seconds and LHS seconds are different variables; I should have named them something different, I guess.
// total second ==> tseconds
hours = tseconds / 3600;
minutes = (tseconds % 3600) / 60;
seconds = tseconds % 60;
I'm having a problem with adding two times together. It works fine until the sum of the seconds come in.

What it should do... (output)
1:12:03
11:24:59
=======
12:37:02

Currently: (output)
1:13:3
11:24:59
======
12:36:62

long Secs_To_Time(long TS, Time t)								// TS = aka Total Seconds{	t.Hours = 0, t.Minutes = 0, t.Seconds = 0;	while (TS >= 3600)		// 3600 seconds is an hour	{		t.Hours += 1;		TS = TS - 3600;	}	while (TS >= 60)		// 60 seconds in a minute	{		t.Minutes += 1;		TS = TS - 60;	}		TS -= 1;		t.Seconds += 1;	std::cout << t.Hours << ":" << t.Minutes << ":" << t.Seconds << std::endl;	return 1; // who cares right now}



Adding it all together:
		// SUM OF BOTH TIMES		T3Sum.Hours = T1.Hours + T2.Hours;		T3Sum.Minutes = T1.Minutes + T2.Minutes;		T3Sum.Seconds = T1.Seconds + T2.Seconds;		std::cout << T3Sum.Hours << ":" << T3Sum.Minutes << ":" << T3Sum.Seconds << std::endl;
Quote:Original post by philvaira
I'm having a problem with adding two times together. It works fine until the sum of the seconds come in.

What it should do... (output)
1:12:03
11:24:59
=======
12:37:02

Currently: (output)
1:13:0
11:24:0
======
12:36:62

*** Source Snippet Removed ***


Adding it all together:
*** Source Snippet Removed ***

The problem here is that when you add the times together, you could overflow one the seconds, minutes, or hours. If you will be doing time arithmetic, perhaps a function to "fix" the time to an acceptable format would be better.

void Fix_Time(Time &t)						{	while (t.Seconds >= 60)	{		t.Minutes++;		t.Seconds -= 3600;	}	while (t.Minutes >= 60)	{		t.Hours++;		t.Minutes -= 60;	}        t.Hours %= 12; // Format to 12 hour time                       // change to 24 for 24 hour clock}


Better yet, you could program built in functionality to the Time class to handle addition of times and formatted output. But I'll leave that to you. :)
I updated my message before you posted that, sorry! Thanks though. I got them adding, but it's kinda funky if you want to check it out.

Currently I'm working on this idea..

if (T3Sum.Seconds > 59)
{
T3Sum.Minutes += 1;
}

[Edited by - philvaira on October 14, 2004 2:07:42 AM]
w00t! I got it! To fix up the time overflow, if you want to call it that, I did this...

if (T3Sum.Seconds > 59)
{
T3Sum.Minutes += 1;
T3Sum.Seconds = T3Sum.Seconds - 60;
}

Thanks for the fantastic help. I learned a lot today here.
Quote:Original post by philvaira
w00t! I got it! To fix up the time overflow, if you want to call it that, I did this...

if (T3Sum.Seconds > 59)
{
T3Sum.Minutes += 1;
T3Sum.Seconds = T3Sum.Seconds - 60;
}

Thanks for the fantastic help. I learned a lot today here.
Don't forget hours and minutes either.
If a plant cannot live according to its nature, it dies; so a man.

This topic is closed to new replies.

Advertisement