Lua添加字符串行为

时间:2013-12-24 14:43:51

标签: scripting lua timezone

我一直在努力找到这个问题很长一段时间。在我获得Month变量的两行下,string.find(TimeR,“T”)返回11,但是当我使用TimeR:sub(Ser + 1)时返回24T14:19:59 + 00:00。它的表现就好像TimeR从未改变过,因为原来的字符串“T”是11个字符。

local TimeServer = "http://www.timeapi.org/utc/now"

local TimeR = http.get(TimeServer).readAll() -- Pretend that this gives the string "2013-12-24T14:19:59+00:00"

local function TimeThread()
  local Last = 1
  local Ser = string.find(TimeR, "-")
  local Year = TimeR:sub(Last, Ser - 1)
  TimeR = TimeR:sub(Ser + 1)
  Ser = string.find(TimeR, "-")

  local Month = TimeR:sub(Last, Ser - 1)
  TimeR = TimeR:sub(Ser + 1)
  Ser = string.find(TimeR, "T")

  local Day = TimeR:sub(Last, Ser - 1)
  TimeR = TimeR:sub(Ser + 1)
  Ser = string.find(TimeR, ":")

  local Hour = TimeR:sub(Last, Ser - 1)
  TimeR = TimeR:sub(Ser + 1)
  Ser = string.find(TimeR, ":")

  local Minute = TimeR:sub(Last, Ser - 1)
  TimeR = TimeR:sub(Ser + 1)
  Ser = string.find(TimeR, "+")

  local Second = TimeR:sub(Last, Ser - 1)
  print(Year .. ", " .. Month .. ", " .. Day .. ", " .. Hour .. ", " .. Minute .. ", " .. Second)
end 

TimeThread()

1 个答案:

答案 0 :(得分:0)

试试这个单行:

Year, Month, Day, Hour, Minute, Second = TimeR:match("(.-)%-(.-)%-(.-)T(.-):(.-):(.-)%+")