It looks like you're new here. If you want to get involved, click one of these buttons!
--last updated: 2015-06-05 15:23 --author: holonist clock = {} --[[ _time:int _params:int[] _digits:int[] _seperator:String ]] clock.to = function (_time, _params, _digitVals, _seperator) local time = _time local params = _params local digitVals = _digitVals local seperator = _seperator if params == nil then params = {1, 60, 60, 24} end if digitVals == nil then digitVals = {3, 2, 2, 3} end if seperator == nil then seperator = ":" end --the string we are going to return in the end. local clockString = "" --save all sub-time-units (ie seconds, minutes, ...) in subStrings local subStrings = {} for i=1, #params do if i<#params then subStrings[i] = tostring(math.floor(math.fmod(time/clock.getmod(params[i], params, i),params[i+1]))) else subStrings[i] = tostring(math.floor(math.fmod(time/clock.getmod(params[i], params, i),100000000))) end end for i=1, #subStrings do --if the substrings contain less chars then designated in digitVals, add zeros subStrings[i] = clock.fillup(subStrings[i], digitVals[i]) end for i=#subStrings, 1, -1 do clockString = clockString..subStrings[i] if i > 1 then clockString = clockString..seperator end end return clockString end clock.fillup = function(str, di) if str:len() < di then return clock.fillup("0"..str, di) else return str end end clock.getmod = function(mod, params, index) if index > 1 then mod = mod*params[index-1] return clock.getmod(mod, params, index-1) else return mod end end |
Comments
I didn't look over it enough, however if "first unit" must always have value 1 you could set it by default inside the function to save up the 1 when calling it.
For standard time units you can also use os.date()
http://www.lua.org/pil/22.1.html
Likes: Holonist
Also, I'm calling clock.getmod(paramlist[i], paramlist, i) two times. Now it seems obvious to me that the function should work without the first parameter.
Still some work to do.
Can you hand an arbitrary value to os.date() to format it, or does it always give back the actual time?
Likes: talis
As an example: os.date on 906000490 and its output "back in time values"
you were already doing a for loop, but with ipairs you don't need to read the data again (plist and sstr instead of paramlist[i] and subStrings[i] ) - maybe this could help getting rid of the 1 ? -I'm guessing, I didn't check it.
Notice that you can use ipairs because you don't have ["string-indices"]. (if you had them you could have used "pairs").
Also I think that you can "localize" clock.fillup and clock.mod, placing them inside clock.to() - which then could simply become: function clockTo(pTime, pParams, pDigits) .
Without changing too much code I would write:
As far as I understood: the most you can keep local, the faster your app would perform.
Well that os.date() thing makes my work pretty obsolete. The only advantage my function has , it's not bound to certain time units.
For the for-loops, I simply like them. I am familiar with pairs and ipairs, but imo they aren't needed here. I also think they are a bit slower than straight for-loops, but could be wrong.
Anyway thanks for the insights. I'll keep changing that thing until it's perfect, seems like a good exercise
Ok I'm retarded, I forgot to remove print("59s: "..clock.to(59, {1, 60, 60, 24}, 2)) from another file, where it still uses the old function.