# Convert Lua UINT to hh:mm:ss

I have found the convert_to function in the lua-user wiki, and I created the convert_to_time function which does what I want it to do. Which is, basically: Having a number, return its value in a Time format.

The values range is: [0..863999] as we want the data in 0..24 hours format by step of 0.1 second.

The code from the lua-user-wiki, had a step of 1 second so I had to change the base to fit my specifications, but because of that I've had to create too many conditions to be able to always print out the value I want. I had issues with printing 01:00:00 for example.

If someone of you can give it a view and suggest me a simpler way to achieve what I want in ALL situations I would appreciate it.

intervals={
{"seconds",1}, --the "1" should never really get used but
{"minutes",60},
{"hours",60},
{"days",24},
}

positions={}
for i=1,4 do
positions[intervals[i][1]]=i
end

function convert_to(value, sourceunits, targetunits)

local sourcei, targeti = positions[sourceunits], positions[targetunits]
assert(sourcei and targeti)

if sourcei<targeti then

local base=10
for i=sourcei+1,targeti do
base=base*intervals[i][2]
end

return value/base

elseif sourcei>targeti then

local base=10
for i=targeti+1,sourcei do
base=base*intervals[i][2]
end

return value*base
else return value end
end

function convert_to_time(value)
local returnString = "", horas, minutos, segundos

horas = tostring(convert_to(value,"seconds","hours")) -- return : 12.888888889
if(tonumber(string.match(horas, "%.(%w+)"))==nil) then return (horas ..":00:00") end
minutos = tostring(convert_to("0."..string.match(horas, "%.(%w+)"),"hours","minutes")/10) -- returns : 53.33333334
if(tonumber(string.match(minutos, "%.(%w+)"))==nil) then return (string.match(horas, "(%w+)%.") .. minutos ..":00") end
segundos = tostring(convert_to("0."..string.match(minutos, "%.(%w+)"),"minutes","seconds")/10) -- returns : 20.0000000004

horasStr = string.match(horas, "(%w+)%.")
minutosStr = string.match(minutos, "(%w+)%.")
if string.match(segundos, "%.") then
segundosStr = string.match(segundos, "(%w+)%.")
else
segundosStr = segundos
end

-- Ñapa para que los numeros se vean en formato hh:mm:ss
if (tonumber(horasStr) < 10) then if (tonumber(horasStr)==nil) then horasStr = "00" end horasStr = "0"..horasStr end
if (tonumber(minutosStr) < 10) then if (tonumber(minutosStr)==nil) then minutosStr = "00" end minutosStr = "0"..minutosStr end
if (tonumber(segundosStr) < 10) then if (tonumber(segundosStr)==nil) then segundosStr = "00" end segundosStr = "0"..segundosStr end

returnString = horasStr .. ":" .. minutosStr .. ":" .. segundosStr

return returnString

end


You can test the code for this example:

print(convert_to_time(36000))


A much simpler way is:

function convert_to_time(tenths)
local hh = (tenths // (60 * 60 * 10)) % 24
local mm = (tenths // (60 * 10)) % 60
local ss = (tenths // 10) % 60
local t = tenths % 10

return string.format("%02d:%02d:%02d.%01d", hh, mm, ss, t)
end

• This is indeed a much simpler way, but it would be good to actually review the code in the question as well. Aug 19 '17 at 15:46
• Very good! Simple and effective! Just want I wanted! Aug 24 '17 at 7:15
• @MartinRouterKing: I reverted your change to my answer. The // operator is valid since Lua 5.3, which is now almost 2.5 years old. Furthermore, your remark about millisecond precision was wrong. Neither the precision nor the accuracy of the output are milliseconds, both are only deciseconds. Aug 24 '17 at 19:46

Try also

function convert_to_time(tenths)
return os.date("%H:%M:%S",os.time({sec=tenths//10, hour=0, min=0, day=1, month=1, year=2000}))
end


This code exploits the time and date arithmetic performed by os.time and the string formatting performed by os.date, both standard Lua functions.

• You have presented an alternative solution, but haven't reviewed the code. Please edit it to explain your reasoning (how your solution works and how it improves upon the original) so that everyone can learn from your thought process. Feb 23 '18 at 14:40
• @TobySpeight, see my edited answer. Thanks for the nudge.
– lhf
Feb 23 '18 at 14:48
• Thank you for your answer, but I will stick into the last solution as this one seems to not work for me. Whilst it does convert UINT to date format, I can't trust the result as it says for example that GMT Time Difference is : 10:00:00, when it actually is 1:00:00 (With the other solution it does it well) Feb 26 '18 at 7:40
• @MartinRouterKing, I had misinterpreted what tenthsmeant. See my edited answer.
– lhf
Feb 26 '18 at 10:56
• Alright! It works well now! I appreciate your willingness to help and give alternative solutions to my problem! +1 for you =) Feb 27 '18 at 7:24