I need to go from milliseconds to a tuple of (hour, minutes, seconds, milliseconds) representing the same amount of time. E.g.:
10799999ms = 2h 59m 59s 999ms
The following pseudo-code is the only thing I could come up with:
# 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)
I'm sure it must be possible to do it smarter/more elegant/faster/more compact.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…