【发布时间】:2012-06-08 02:20:53
【问题描述】:
我需要从毫秒到代表相同时间量的(小时、分钟、秒、毫秒)的元组。例如:
10799999ms = 2h 59m 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) )
-
确保您使用的标准语言库中没有此类功能。
标签: algorithm date pseudocode