Os.time returns weird values and errors

What do I want to achieve?
I am trying to make a ISO 8601 parser in Roblox.

What is the issue?
os.time is returning an incorrect response, and also the error field 'day' missing in date table.

Here is the function i’m using:

function TimeArray(timestring)
    local offset
    local sst = string.split(timestring, " ") -- Split the date from the time
    local day = sst[1]
    local dt = sst[2] -- time and offset
    if string.find(dt, "+") then -- if offset is greater
	    local sst2 = string.split(dt, "+") -- split the time and the offset
	    dt = sst2[1] -- DateTime becomes the time ex. 12:00
	    offset = sst2[2] -- Becomes the offset number ex. 9
    else
	    local sst2 = string.split(dt, "-") -- split the time and the offset
	    dt = sst2[1] -- DateTime becomes the time ex. 12:00
	    offset = tonumber(-sst2[2]) -- becomes the opposite of the offset number ex. -9
    end
    local tab = {} -- define table

    for i, v in pairs(string.split(day, "-")) do
    	table.insert(tab, v) -- add each date (year, month, day)
    end
    for i, v in pairs(string.split(dt, ":")) do
	    table.insert(tab, v) -- add each time (hours, seconds)
    end
    table.insert(tab, tonumber(offset)) -- insert the offset at the end
    return tab
end

-- Running tests

year, month, day, hour, min, offset = TimeArray("2020-10-25 09:20-02")

os.time({year=year, month=month, day=day, hour=hour, min=min})

What have I tried?
I’ve checked lua’s website on os.time. Their webpage says:

The time function, when called without arguments, returns the current date and time, coded as a number. (In most systems, that number is the number of seconds since some epoch.) When called with a table, it returns the number representing the date and time described by the table.

From pasting their example given print(os.time{year=1970, month=1, day=1, hour=0, sec=1}), I get the result of 1, where as their website says it should return 10801.

Also, when running my function above, I get the error field 'day' missing in date table.

Help would be appreciated, Thanks! :smile:

1 Like

print(os.time{year=1970, month=1, day=1, hour=0, sec=1}) should give you 1 because it’s 1 second away from epoch as stated here:

Could you add some comments in the TimeArray function so I (and you in the future) could more easily understand what’s happening.

On what line is the error coming from?

I’ve updated the post with comments.

The error is coming from the final line of the code:
os.time({year=year, month=month, day=day, hour=hour, min=min}).

Also, how come Lua’s site says it should return 10801, if Lua also uses epoch?

I found the problem by seeing what values were returned from the function and how you captured them. Your returned a table from the function and because of this the changes that have to be made should be:

local newTime = TimeArray("2020-10-25 09:20-02")
os.time({year=newTime[1], month=newTime[2], day=newTime[3], hour=newTime[4], min=newTime[5]})

instead of:

year, month, day, hour, min, offset = TimeArray("2020-10-25 09:20-02")
os.time({year=year, month=month, day=day, hour=hour, min=min})
1 Like

Why would my way not work? It’s just putting all the values of the table into specific variables.

Thanks for the solution though!

I think the correct syntax in that case is like this:

return value1, value2, value3...
1 Like