【问题标题】:Timestamp pattern时间戳模式
【发布时间】:2013-12-23 12:26:01
【问题描述】:

假设我有以下提醒时间戳

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

上面的模式应该是什么来匹配我之前提到的提醒时间戳?

【问题讨论】:

    标签: lua timestamp coronasdk


    【解决方案1】:

    您需要使用 Lua 的字符串解析功能。尝试下面提到的一些技术,如果您仍然有问题,请具体发布哪些不起作用:

    【讨论】:

      【解决方案2】:

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

      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

      【讨论】:

        猜你喜欢
        • 2017-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-14
        • 2011-12-04
        • 1970-01-01
        • 2019-10-03
        相关资源
        最近更新 更多