gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > How to bring milliseconds into format?

#177306 - Yogie.de - Tue Apr 03, 2012 9:19 pm

Hi,

I'm a starter, so please don't shout at me ;-)

I have a value that contains milliseconds and I would like to display these milliseconds into a readable time format on the screen...

ExposureTime is in Milliseconds...

So far I have this:
Code:

            TimeEstimate = ExposureTime;
            TimeEstimateDays=
            TimeEstimateHours=
            TimeEstimateMinutes=(TimeEstimate/1000)/60;
            TimeEstimateSeconds=(TimeEstimate/1000)-(TimeEstimateMinutes*60);
            TimeEstimateMilliseconds=

            // minutes
             dummy=TimeEstimateMinutes-(int(TimeEstimateMinutes/10)*10);
            PA_OutputText (1,18,5,"%d:",dummy);
            dummy=int((TimeEstimateMinutes-dummy)/10);
            PA_OutputText (1,17,5,"%d",dummy);
            // seconds
            dummy=TimeEstimateSeconds-(int(TimeEstimateSeconds/10)*10);
            PA_OutputText (1,21,5,"%d",dummy);
            dummy=int((TimeEstimateSeconds-dummy)/10);
            PA_OutputText (1,20,5,"%d",dummy);


I'm too stupid to find out how I could reach that I get days, hours and milliseconds to be displayed in the format like above...?!

Anyone to help me please?

Many thanks
Yogie

#177396 - Ruben - Sun May 27, 2012 6:06 am

It depends on what you want to do, exactly.

If you want to display it as a sort of 'time taken':
x milliseconds, y seconds, z minutes, u hours, v days

Then you would do
Code:
// get exposure time
u32 time = ExposureTime;

// calculate data
// 1000 milliseconds per second
// 60 seconds per minute
// 60 minutes per hour
// 24 hours per day
u32 timeMs   = (time              ) % 1000; // wrap to millisecond range
u32 timeSecs = (time/1000         ) %   60; // wrap to second range
u32 timeMins = (time/1000/60      ) %   60; // wrap to minute range
u32 timeHour = (time/1000/60/60   ) %   24; // wrap to hour range
u32 timeDays = (time/1000/60/60/24);        // the remaining data is days

If you want to display it as showing a total count for each variable, then it changes slightly:
Code:
// get exposure time
u32 time = ExposureTime;

// calculate data
// 1000 milliseconds per second
// 60 seconds per minute
// 60 minutes per hour
// 24 hours per day
u32 timeMs   = time              ;
u32 timeSecs = time/1000         ;
u32 timeMins = time/1000/60      ;
u32 timeHour = time/1000/60/60   ;
u32 timeDays = time/1000/60/60/24;

Note that the only thing I changed was remove the modulo/wrap.