关于lua:时间戳记模式

Timestamp pattern

假设我具有以下提醒时间戳记

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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

与上面提到的提醒时间戳相匹配的上方模式应该是什么?


这是答案,该功能实际上正常工作

1
2
3
4
5
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


您需要使用Lua的字符串解析功能。尝试以下提到的一些技术,如果仍然有问题,请专门张贴不起作用的内容:

  • 有关拆分字符串并保存多个变量的问题
  • 有关从字符串中提取数据的问题,与您的非常相似(尽管问题域是GPS坐标而不是日期/时间)
  • 有关如何在Lua中进行模式匹配的问题,几个很好的示例以及文档链接