C++ convert decimal hours into hours, minutes, and seconds

13,169

Solution 1

I don't know c++ functions off the top of my head, however this "psuedocode" should work.

double time = distance / speed;
int hours = time;
double minutesRemainder = (time - hours) * 60;
int minutes = minutesRemainder;
double secondsRemainder = (minutesRemainder - minutes) * 60;
int seconds = secondsRemainder;

Corrected not needing floor.

As far as the comment about it not working for negative times, you can't have a negative distance in physics. I'd say that would be user input error, not coder error!

Solution 2

I don't know if this is better...actually I don't know for sure that it is right as I haven't tested it, but I would first convert the hours to the total number of seconds, then convert that back into hours/minutes/seconds. It would look something like:

int totalseconds = time * 3600.0;

// divide by number of seconds in an hour, then round down by casting to an integer.
int hours = totalseconds/3600;

// divide by 60 to get minutes, then mod by 60 to get the number minutes that aren't full hours
int minutes = (totalseconds/60) % 60;  

// use mod 60 to to get number of seconds that aren't full minutes
int seconds = totalseconds % 60;  
Share:
13,169
Jared
Author by

Jared

Web developer with extremely broad interests (technology, psychology, philosophy, religion, ethics, physics, astronomy, biology, business, marketing, design, etc.)

Updated on June 05, 2022

Comments

  • Jared
    Jared almost 2 years

    I have some number of miles and a speed in MPH that I have converted into the number of hours it takes to travel that distance at that speed. Now I need to convert this decimal number into hours, minutes, and seconds. How do I do this? My best guess right now is:

    double time = distance / speed;
    int hours = time; // double to integer conversion chops off decimal
    int minutes = (time - hours) * 60;
    int seconds = (((time - hours) * 60) - minutes) * 60;
    

    Is this right? Is there a better way to do this? Thanks!

  • avakar
    avakar over 14 years
    For positive time, floor is completely superfluous there. For negative time, it is incorrect.
  • Brett Allen
    Brett Allen over 14 years
    Time is in hours, so total seconds would be time * 3600.0 and hours would be floor(totalseconds / 3600) You're way off...
  • Brett Allen
    Brett Allen over 14 years
    Ah, I do that to be safe with Double to Integer conversion, never sure if it rounds or drops the decimal. The minutes remainder is a number less than 1, which represents a base 10 fraction of an hour (.25 = 15 minutes). Multiplying by 60 turns it into proper minutes (.25 * 60 = 15)
  • Brett Allen
    Brett Allen over 14 years
    avakar, you can't have negative time in speed measurements unless you are disobeying the laws of physics.
  • avakar
    avakar over 14 years
    Actually, negative times crop up in physics calculations all the time.
  • avakar
    avakar over 14 years
    ... as well as negative speeds.
  • avakar
    avakar over 14 years
    Even if you use floor, you're still putting a double into an int.
  • Al Crowley
    Al Crowley over 14 years
    I just fixed the code to multiply by 3600 as noted by Aequitarum. Unless my C-foo is out of wack, converting the hours should be right now as the cast to integer will automatically take the floor. Perhaps it would be more readably obvious with an explicit floor() call. If this was production code destine for long term maintenance, it would probably be a good idea to be as explicit as possible.