模組:文白相雜

可在模組:文白相雜/doc建立此模組的說明文件

local f = {}

f.DATEPATTERN = '([〇一二三四五六七八九]-)年([〇一二三四五六七八九]-)月([〇一二三四五六七八九]-)日 ([一二三四五六日]-) ([〇一二三四五六七八九]-)時([〇一二三四五六七八九]-)分 %(UTC%)'

local function chinese2num(s)
    local str = s
    str = string.gsub(str, '〇', '0')
    str = string.gsub(str, '零', '0')
    str = string.gsub(str, '一', '1')
    str = string.gsub(str, '二', '2')
    str = string.gsub(str, '三', '3')
    str = string.gsub(str, '四', '4')
    str = string.gsub(str, '五', '5')
    str = string.gsub(str, '六', '6')
    str = string.gsub(str, '七', '7')
    str = string.gsub(str, '八', '8')
    str = string.gsub(str, '九', '9')
    return tonumber(str)
end

local function date2num(date)
    local y,m,d,h,min = string.gmatch(date, f.DATEPATTERN)()
    if y and m and d and h and min then
        return chinese2num(y)*100000000+chinese2num(m)*1000000+chinese2num(d)*10000+chinese2num(h)*100+chinese2num(min)
    else
        return 0
    end
end

local function curdate()
    local t = os.date('!*t')
    return t.year*100000000+t.month*1000000+t.day*10000+t.hour*100+t.min
end

local function extractdate(text)
    local x, y = text.find(text, f.DATEPATTERN)
    if not x then
        return ''
    end

    local date = string.sub(text, x, y)
    return date
end

function f.getdate(frame)
    local str = frame.args[1] or ''
    return extractdate(str)
end

function f.checkdate(frame)
    local str = frame.args[1] or ''
    local past = date2num(extractdate(str))
    local now = curdate()
    if past ~= 0 and now - past >= 1000000 then
        return '1'
    else
        return '0'
    end
end

return f