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
Oops. I actually did notice that earlier in my script editing. I also figured that flooring all the results wasn't doing me any good either.
However, now when I remove all the flooring and fixed that dual subtraction, I get even stranger results. See below:
function hms (hms_x)
if (tonumber(hms_x) == nil) or (tonumber(hms_x) == 0) then
return error("Invalid argument.")
else
hms_seconds = tonumber(hms_x)
if hms_seconds < 1 then hms_seconds = 1 end
if hms_seconds > 86400 then hms_seconds = 86400 end
hms_hours = hms_seconds / 3600
hms_seconds = hms_seconds - (hms_hours * 3600)
hms_minutes = hms_seconds / 60
hms_seconds = hms_seconds - (hms_minutes * 60)
if string.len(hms_seconds) < 2 then hms_seconds = "0"..hms_seconds end
if string.len(hms_minutes) < 2 then hms_minutes = "0"..hms_minutes end
if string.len(hms_hours) < 2 then hms_hours = "0"..hms_hours end
return hms_hours, hms_minutes, hms_seconds
end
end
-- h,m,s = hms(300)
-- Note(h..":"..m..":"..s)
-- Results: 0.083333333333333:00:00
Even more baffled now, since that version is I believe closer to the c version in the source, and yet it breaks even harder. Lol
That's what flooring the math was keeping from happening. The math is actually dead on, it just isn't what you expected to see because you're used to seeing "intelligent" math for this operation.
I'd say either put if checks in for minimum values at day and hour or go back to using floor results.
Highly amused. I got it to work this way, but of course, I still can't work with seconds higher than 3600, at which point the system goes into negatives. Ah well. Limit input at 3600 seconds and have at it. Thanks for the input guys!
require "commas"
function hms (x)
if (tonumber(x) == nil) or (tonumber(x) == 0) then
return error("Invalid argument.")
else
x = tonumber(x)
if x < 1 then x = 1 end
if x > 86400 then x = 86400 end
hours = round(x / 3600)
minutes = round((x - (hours * 3600))/ 60)
seconds = x - ((hours * 3600) + (minutes * 60))
if string.len(seconds) < 2 then seconds = "0"..seconds end
if string.len(minutes) < 2 then minutes = "0"..minutes end
if string.len(hours) < 2 then hours = "0"..hours end
return hours, minutes, seconds
end
end
I also figured that flooring all the results wasn't doing me any good either.
However, now when I remove all the flooring and fixed that dual subtraction, I get even stranger results.
You're flailing. That's bad.
If you ask for help and get it, implement the suggestion and see if it works before doing anything else. If you want to know why a suggestion was made, ask. Don't make changes that you don't understand.
Put effort into understanding what each line of your code is doing.
I do like the version you posted much better, Meerclar. Will try that in my function to see if things turn out better, though will I still have issues with values higher than 3600?
Flailing? Maybe. I'd been searching for tons of examples on Google, reading over them, and also trying them. For now, the last block I posted works well enough, if albeit with a caveat that I can't input rangers higher than an hour, so if this approach doesn't work out for me, I'll revert to that.
Going to switch around the function now with the new suggestions and see how it goes, and thanks again for the help.
Update: Meerclar, you are my hero. Thanks for helping out an admitted math subnormal -- equasions hurt my head. What's more, this version works great for values over 1 hour, too!
Thanks a lot. I know I never would've been able to figure that out on my own. If it was pure scripting syntax, yes, but when it comes to mathematical stuff, I'm out of the loop.
I'm only as good as I am with math operations because of Eve Online - it's not called spreadsheets in space for giggles. I've had to cook up some ridiculously complicated resource trackers over the years to be certain I wasn't about to shortchange myself on a supply run.