[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Seconds To Time Convert Returning Bad Results
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Seconds To Time Convert Returning Bad Results

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Xvordan   (25 posts)  [Biography] bio
Date Thu 03 Jul 2014 12:33 PM (UTC)
Message
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
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #1 on Thu 03 Jul 2014 02:51 PM (UTC)
Message
Did you intend to subtract twice at the hms_seconds level?

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] bio   Global Moderator
Date Reply #2 on Thu 03 Jul 2014 02:52 PM (UTC)
Message

hms_seconds = math.floor(hms_seconds - (hms_minutes * 60))
hms_seconds = math.floor(hms_seconds - (hms_minutes * 60))

You're reducing seconds twice in a row.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Xvordan   (25 posts)  [Biography] bio
Date Reply #3 on Thu 03 Jul 2014 03:44 PM (UTC)
Message
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
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #4 on Thu 03 Jul 2014 03:51 PM (UTC)
Message
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.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Xvordan   (25 posts)  [Biography] bio
Date Reply #5 on Thu 03 Jul 2014 04:32 PM (UTC)
Message
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


-- h,m,s = hms(5400)
-- Note(h..":"..m..":"..s)
-- Result: 02:-30:00
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #6 on Thu 03 Jul 2014 04:41 PM (UTC)

Amended on Thu 03 Jul 2014 04:55 PM (UTC) by Meerclar

Message
All you should have needed to do to have a properly functioning script was take the last subtraction of minutes out of the original code you posted.

Actually, you could clean up the logic considerably by using something like

seconds = input
hours = floor(seconds/3600) - only returns whole numbers
seconds = seconds%3600 - returns remainder of previous division
minutes = floor(seconds/60) - only whole numbers again
seconds = seconds%60 - final remainder
return h,m,s

instead of having to go back and subtract the previous operation from the running seconds total

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Fiendish   USA  (1,641 posts)  [Biography] bio   Global Moderator
Date Reply #7 on Thu 03 Jul 2014 08:52 PM (UTC)
Message
Xvordan said:

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.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Xvordan   (25 posts)  [Biography] bio
Date Reply #8 on Thu 03 Jul 2014 09:58 PM (UTC)

Amended on Thu 03 Jul 2014 10:13 PM (UTC) by Xvordan

Message
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.
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #9 on Fri 04 Jul 2014 04:20 AM (UTC)
Message
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.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


6,433 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]