I've got a function which converts a supplied number of seconds into hour, minute, second format. It's supposed to just convert to the best solution, then return the remainder, so that 300 becomes 5 minutes with 0's elsewhere, and 301 returns 5 minutes, 1 second. This all works great, until I go passed the 1 hour mark. Here's the block of code, and examples of it failing. Anyone with a much better grasp of math mind correcting my function? I basically just copied the formula from the mushclient source for DoAfterSpecial, since I would've been lost otherwise. I'm apparently missing something, however, as I typing DoAfter(5400) doesn't fail the way my function does.
```
function hms (hms_x)
if (tonumber(hms_x) == nil) or (tonumber(hms_x) == 0) then
return error("Invalid argument.")
else
hms_seconds = math.floor(tonumber(hms_x))
if hms_seconds < 1 then hms_seconds = 1 end
if hms_seconds > 86400 then hms_seconds = 86400 end
hms_hours = math.floor(hms_seconds / 3600)
hms_seconds = math.floor(hms_seconds - (hms_hours * 3600))
hms_minutes = math.floor(hms_seconds / 60)
hms_seconds = math.floor(hms_seconds - (hms_minutes * 60))
hms_seconds = math.floor(hms_seconds - (hms_minutes * 60))
return hms_hours, hms_minutes, hms_seconds
end
end
```
h,m,s = hms(5400)
print(h..", ",m..", ",s)
-- 1, 30, -1800 |