我需要从毫秒到表示相同时间量的(小时,分钟,秒,毫秒)元组。例如:从毫秒到小时,分钟,秒和毫秒
10799999ms = 2H59米59S 999ms
下面的伪代码是我能想出的唯一的事:
# The division operator below returns the result as a rounded down integer
function to_tuple(x):
h = x/(60*60*1000)
x = x - h*(60*60*1000)
m = x/(60*1000)
x = x - m*(60*1000)
s = x/1000
x = x - s*1000
return (h,m,s,x)
我敢肯定,它必须能够做到这一点更聪明/更优雅/更快/更紧凑。
你可以用C和朋友模运算符(% )稍微简化x的计算(例如,x = x%(60 * 60 * 1000)) – fvu
确保您在使用的标准语言库中没有此类功能。 –