Sep 7, 2019 at 3:14am Sep 7, 2019 at 3:14am UTC
I have another code I need to write and I have it completed just about, but I need a thorough explanation, please. Why is this calculation as follows?
days = input_seconds / 60 / 60 / 24;
hours = (input_seconds / 60 / 60) % 24;
minutes = (input_seconds / 60) % 60;
seconds = input_seconds % 60;
Can someone help me understand the reasoning behind this formula? This is not mine, I found this on this forum and would like an explanation. Thank you very much!
Sep 7, 2019 at 3:30am Sep 7, 2019 at 3:30am UTC
Well think about a simpler example of say
number = 234;
and you want to extract
hundreds = 2;
tens = 3;
units = 4;
You use a series of / 10 and % 10 operations to do that.
Or replace all those magic numbers with meaningful constants.
days = input_seconds / SECONDS_PER_MINUTE / MINUTES_PER_HOUR / HOURS_PER_DAY;
hours = (input_seconds / SECONDS_PER_MINUTE / MINUTES_PER_HOUR) % HOURS_PER_DAY;
minutes = (input_seconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR;
seconds = input_seconds % SECONDS_PER_MINUTE;
Sep 7, 2019 at 3:34am Sep 7, 2019 at 3:34am UTC
Thank you again Salem, you're a great help!!!!