Lua ISO 8601 datetime parsing pattern
23:24 26 Oct 2011

I'm trying to parse a full ISO8601 datetime from JSON data in Lua. I'm having trouble with the match pattern.

So far, this is what I have:

-- Example datetime string 2011-10-25T00:29:55.503-04:00
local datetime = "2011-10-25T00:29:55.503-04:00"
local pattern = "(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)%.(%d+)"
local xyear, xmonth, xday, xhour, xminute, 
        xseconds, xmillies, xoffset = datetime:match(pattern)

local convertedTimestamp = os.time({year = xyear, month = xmonth, 
        day = xday, hour = xhour, min = xminute, sec = xseconds})

I'm stuck at how to deal with the timezone on the pattern because there is no logical or that will handle the - or + or none. Although I know lua doesn't support the timezone in the os.time function, at least I would know how it needed to be adjusted.

I've considered stripping off everything after the "." (milliseconds and timezone), but then i really wouldn't have a valid datetime. Milliseconds is not all that important and i wouldn't mind losing it, but the timezone changes things.

Note: Somebody may have some much better code for doing this and I'm not married to it, I just need to get something useful out of the datetime string :)

parsing datetime date lua