lua中时间字符串转时间 其他时区时间转本地时区时间

--字符串转时间戳 字符串格式:2019-12-24 15:53:23
function lxb.Common:getTimeStampByStirng(timeString)
    if type(timeString) ~= 'string' then print("string2time: timeString is not a string") return 0 end
    local fun = string.gmatch(timeString, "%d+")
    local y = fun() or 0
    if y == 0 then print("year == 0") return 0 end
    local m = fun() or 0
    if m == 0 then print("month == 0") return 0 end
    local d = fun() or 0
    if d == 0 then print("day == 0") return 0 end
    local H = fun() or 0
    if H == 0 then print("hour == 0") return 0 end
    local M = fun() or 0
    if M == 0 then print("minute == 0") return 0 end
    local S = fun() or 0
    if S == 0 then print("second == 0") return 0 end
    return os.time( { year = y, month = m, day = d, hour = H, min = M, sec = S })
end

--转换到本地时间 字符串格式
function lxb.Common:getLocalTimeWithString(timeString)
    local now = os.time()
    local dif =  os.difftime(now, os.time(os.date("!*t", now)))
    local time = os.date("*t", self:getTimeStampByStirng(timeString) + dif);
    local date = os.date("%Y-%m-%d %H:%M:%S", os.time(time));
    return date;
end

--转换到本地时间 table格式
function lxb.Common:getLocalTimeWithTab(timeString)
    local now = os.time()
    local dif =  os.difftime(now, os.time(os.date("!*t", now)))
    local date = os.date("*t", self:getTimeStampByStirng(timeString) + dif);
    return date;
end

posted on 2019-12-25 16:25  kk波波  阅读(1392)  评论(0)    收藏  举报

导航