Sign in to follow this  
Storyyeller

formatting time to string

Recommended Posts

Suppose I have a number in ms and want to change it to HH:MM:SS:MS format in a string. What is the best way to do this? One problem I have with Boost Lexical Cast is that I can't figure out how to force it to pad the numbers. Should I just manually check the length of each part and prepend a 0 if necessary? Is there some way to do this with streams?

Share this post


Link to post
Share on other sites
Quote:
Original post by Storyyeller
Suppose I have a number in ms and want to change it to HH:MM:SS:MS format in a string.

What is the best way to do this?

One problem I have with Boost Lexical Cast is that I can't figure out how to force it to pad the numbers. Should I just manually check the length of each part and prepend a 0 if necessary? Is there some way to do this with streams?
I don't have a complete answer for you, but what I would recommend trying is creating a 'time' type, and then overloading the << operator (and perhaps >> as well) for this type. If I'm not mistaken, lexical_cast() will then handle the conversion(s) naturally and correctly. (Not that you would have to use lexical_cast(), but you could if you wanted to.)

The code might look something like this (not compiled or tested):

struct Time ( unsigned int ms; };

std::ostream& operator<<(std::ostream& s, const Time& time)
{
unsigned int seconds = time.ms / 1000;
unsigned int minutes = seconds / 60;
unsigned int hours = minutes / 60;

unsigned int ms = time.ms % 1000;
seconds %= 60;
minutes %= 60;

// Set up stream to pad to 2 digits (can't remember syntax off the
// top of my head):
// ...

// Note that this assumes 'hours' is < 100:

return s << hours << ":" << minutes << ":" << seconds << ":" << ms;
}

That's off the top of my head, so no guarantees that I got all the details right, but maybe it'll at least help get you pointed in the right direction.

Share this post


Link to post
Share on other sites
Here's what I came up with using streams. When I looked at Boost DateTime, I could find the functions to do what I wanted.


std::string FormatPlayTime(Uint32 frames)
{
const Uint32 FRAMESPERHOUR = 3600000/MSPERFRAME;
const Uint32 FRAMESPERMINUTE = 60000/MSPERFRAME;

Uint32 hours = frames / FRAMESPERHOUR;
frames -= hours * FRAMESPERHOUR;

Uint32 minutes = frames / FRAMESPERMINUTE;
frames -= minutes * FRAMESPERMINUTE;

Uint32 millisecs = frames * MSPERFRAME;
Uint32 seconds = millisecs / 1000;
Uint32 hundreths = (millisecs % 1000) / 10;

assert(minutes < 60);
assert(seconds < 60);
assert(hundreths < 100);

std::stringstream mystream("Time: ", std::stringstream::in | std::stringstream::app | std::stringstream::out);
mystream << std::setfill('0');
mystream << hours << ':' << std::setw(2) << minutes << ':' << std::setw(2) << seconds << '.' << std::setw(2) << hundreths;
return mystream.str();
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this