Convert Seconds variable into Hours Minutes Seconds

Hey

Can someone help me come up with a clever way to convert a Long variable which contains a Seconds value into Hours Minutes Seconds variables?

For example, if I have a variable called Seconds which has a value of 135 whats the best way to break this down into the 3 variables?

135 Seconds should equal
Seconds = 15
Minutes = 2
Hours = 0

Thanks for your suggestions!

Basically, use modulos and divisions

Here is a quick function that could help you

void secondsToHMS( const uint32_t seconds, uint16_t &h, uint8_t &m, uint8_t &s )
{
    uint32_t t = seconds;

    s = t % 60;

    t = (t - s)/60;
    m = t % 60;

    t = (t - m)/60;
    h = t;
}
6 Likes