模量返回时间分量 [英] Returning Time Components with Modulus
问题描述
某人做了20小时42分钟&一秒16秒,总计74536秒. 我如何从该人完成该班次的秒数中得到小时数?
Someone does 20 Hours 42 Minutes & 16 Seconds in one shift totaling 74536 seconds. How do I get the hours from number of seconds the person has done for that shift?
20 * 60 * 60 = 72000
42 * 60 = 2520
16 = 16
+ -----
Total = 74536
____________________________
Total % 60 = Seconds (16)
Total % ? = Minutes (42)
Total % ? = Hours (20)
已经尝试了84600;事实证明,当数字低于模数时,它确实不是很有用,如果某人仅登录几秒钟,我将必须抓住一些问题...
Tried 84600 already; turns out when a number is lower the modulus, it really is not very helpful, and something I am going to have to catch should someone only sign in for a few seconds ...
推荐答案
您需要同时使用模数和除法:
You need to use both modulus and division:
t = seconds_in_shift;
secs = t % 60;
t /= 60;
mins = t % 60;
t /= 60;
hour = t;
或者:
secs = ttime % 60;
mins = (ttime / 60) % 60;
hour = ttime / 3600;
另一个选项使用Standard中的 div()
函数C(<stdlib.h>
):
One other option uses the div()
function from Standard C (<stdlib.h>
):
div_t v1 = div(ttime, 60);
div_t v2 = div(v1.quot, 60);
此后,v1.rem
包含秒; v2.rem
包含分钟,而v2.quot
包含小时,其基于ttime
中最初的秒数.
After that, v1.rem
contains the seconds; v2.rem
contains the minutes, and v2.quot
contains the hours based on the number of seconds originally in ttime
.
这篇关于模量返回时间分量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!