Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating an time based system-hours minutes seconds

Im currently tring arduino C and trying to figure out what the best way to convert the given added values into the correct seconds minutes and hours.Im aware this needs division and modulo its just im not sure where. how would i go about doing this?
this is the code

int swimhours=0;
int swimmins=50;
int swimsecs=59;
int bikehours=2;
int bikemins=50;
int bikesecs=59;
int runhours=1;
int runmins=50;
int runsecs=59;

int totalhours=swimhours+bikehours+runhours;
int totalmins=swimmins+bikemins+runmins;
int totalsecs=swimsecs+runsecs+bikesecs;`

i need the values of the above to be added(which is the total) and then i need the conversion in the complete variables

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The "trick" is to work in seconds.


These are durations, so we don’t have to worry about time zones or Daylight-Savings Time, etc. Good.

First, convert everything to seconds.

swimmins += swimhours * 60;
swimsecs += swimmins * 60;

bikemins += bikehours * 60;
bikesecs += bikemins * 60;

runmins += runhours * 60;
runsecs += runmins * 60;

Then, find the total number of seconds.

int totalsecs = swimsecs + bikesecs + runsecs;

Finally, find convert back to hours, minutes and seconds.

int totalmins = totalsecs / 60;
totalsecs %= 60;

int totalhours = totalmins / 60;
totalmins %= 60;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading