Jump to content
  • Advertisement
Sign in to follow this  

formatting time to string

This topic is 3018 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!