Question:
I have two values in time format, 20:03:02
and 20:03:35
, which are in an array in Lua.
hora_array={"20:03:02", "20:03:35"}
The difference between the two values is 33 seconds (20:03:02 – 20:03:35 = 33).
I want to insert in this same array the values one at a second, that is 20:03:03, 20:03:04 until reaching the value 20:03:35 (+ 33 elements in the array ).
How can I add a number to a string ? The end result of the array would be this:
hora_array={"20:03:02","20:03:03","20:03:04","..." "20:03:35"}
Answer:
I don't know if it's the best way or if it's totally hassle free but I think this is what you need:
function str2time(hora)
return tonumber(string.sub(hora, 1, 2)) * 3600 + tonumber(string.sub(hora, 4, 5)) * 60 + tonumber(string.sub(hora, 7, 8))
end
hora_array = {"20:03:02", "20:03:35"}
horaInicial = str2time(hora_array[1])
horaFinal = str2time(hora_array[2])
hora_array = {}
for i = horaInicial, horaFinal do
hora = math.floor(i / 3600)
minuto = math.floor((i - hora * 3600) / 60)
segundo = math.floor(i - hora * 3600 - minuto * 60)
table.insert(hora_array, string.format("%02d:%02d:%02d", hora, minuto, segundo))
end
for i, v in ipairs(hora_array) do print(v) end
See working on ideone . And on repl.it. Also posted on GitHub for future reference .