Insert in an array between certain values

6

I have two values in the time format, 20:03:02 and 20:03:35 , which are in a 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 values in the same array one by one, ie 20:03:03, 20:03:04 until the value reaches 20:03:35 (+ 33 elements in the array). 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"}
    
asked by anonymous 06.02.2015 / 23:23

1 answer

5

I do not know if it's the best way or you're totally free of problems 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 running on ideone .

    
07.02.2015 / 00:22