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!