lua5.3 将时间字符串转成时间戳

将 2018-08-10 17:38:17 格式的时间转成时间戳

--[[ 字符串时间转成时间戳
@timeString: 字符串时间 ,时间格式必须为 2018-08-07 10:43:33
@return: 返回时间戳(int)
    例:  print (string2time('2018-08-07 10:43:33'))
--]]

string2time = function(timeString)
    if type(timeString) ~= 'string' then error('string2time: timeString is not a string') return 0 end
    local fun = string.gmatch( timeString, "%d+")
    local y = fun() or 0
    if y == 0 then error('timeString is a invalid time string') return 0 end
    local m = fun() or 0
    if m == 0 then error('timeString is a invalid time string') return 0 end
    local d = fun() or 0
    if d == 0 then error('timeString is a invalid time string') return 0 end
    local H = fun() or 0
    if H == 0 then error('timeString is a invalid time string') return 0 end
    local M = fun() or 0
    if M == 0 then error('timeString is a invalid time string') return 0 end
    local S = fun() or 0
    if S == 0 then error('timeString is a invalid time string') return 0 end
    return os.time({year=y, month=m, day=d, hour=H,min=M,sec=S})
end

 

你可能感兴趣的:(lua5.3)