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

Started by
15 comments, last by Woodsman 19 years, 6 months ago
I doubt it but I thought I'd ask. I'm having trouble developing pseudocode for this problem.
Advertisement
I don't know of any, but it shouldn't be that hard.

Assuming you mean converting 900 seconds into 15 minutes sort of thing, it's fairly simple:

iTotalSeconds = Number of Total Secondsint iHours = 0, iMinutes = 0, iSeconds = 0;while(iTotalSeconds >= 3600) // 3600 seconds in an hour.{     Add 1 to iHours     Subtract 3600 from iTotalSeconds}while(iTotalSeconds >= 60) // 60 seconds in a minute{    Add 1 to iMinutes    Subtract 60 from iTotalSeconds}


Add the rest to iSeconds and you've got your 3 variables, ready to output however you want.

Make sense?
It's a good start :) thanks.
{seconds an int}
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int seconds = seconds % (60);
strftime?
Okay, I tried translating that and got this so far.

long Secs_To_Time(long TS)								// TS = aka Total Seconds{	long Hours = 0, Minutes = 0, Seconds = 0;	while (TS >= 3600)		// 3600 seconds is an hour	{		Hours += 1;		TS = TS - 3600;	}	while (TS >= 60)		// 60 seconds in a minute	{		Minutes += 1;		TS = TS - 60;	}		while (TS != 0)		// Rest in Seconds	{		Seconds += 1;		TS = TS - 1;	}}
Quote:Original post by CJH
{seconds an int}
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int seconds = seconds % (60);

Nice! Oddly, in all my programming years, I never thought to do it that way. Of course, that's assuming it works, which it looks like it does.
Quote:Original post by philvaira
Okay, I tried translating that and got this so far.

*** Source Snippet Removed ***

That looks great, except that the last while loop is unnecessary. Everything left in TS at that point will go to seconds, so you could replace the whole while loop with

Seconds = TS;
Hmm that's good.
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?

This topic is closed to new replies.

Advertisement