时间戳格式模式

让我们假设我有以下提醒时间戳:

local reminder_timestamp = "2013-12-13T00:00:00+01:00"

我正在使用以下函数返回UTC时间:

local function makeTimeStamp(dateString)
    local pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%p])(%d%d)%:?(%d%d)"
    local year, month, day, hour, minute, seconds, tzoffset, offsethour, offsetmin = dateString:match(pattern)
    local timestamp = os.time( {year=year, month=month, day=day, hour=hour, min=minute, sec=seconds} )
    local offset = 0
    if ( tzoffset ) then
      if ( tzoffset == "+" or tzoffset == "-" ) then  -- we have a timezone!
        offset = offsethour * 60 + offsetmin
        if ( tzoffset == "-" ) then
          offset = offset * -1
        end
        timestamp = timestamp + offset

      end
    end
  return timestamp
end

以上的模式应该是什么才能与我先前提到的提醒时间戳相匹配?

点赞
用户869951
用户869951

你需要使用 Lua 的字符串解析能力。尝试使用以下提到的技术,如果你仍然遇到问题,请具体说明哪些地方不起作用:

2013-12-22 13:33:04
用户841531
用户841531

以下是答案,这个函数实际上可以正常工作

pattern = "(%d+)%-(%d+)%-(%d+)%a(%d+)%:(%d+)%:([%d%.]+)([Z%p])(%d%d)%:?(%d%d)"

reminder_timestamp = "2013-12-23T08:00:00+01:00"

local year, month, day, hour, minute, seconds, tzoffset, offsethour, offsetmin = reminder_timestamp:match(pattern)

资源: http://www.lua.org/manual/5.1/manual.html#5.4.1

2013-12-23 04:26:01