模組:沙盒/StevenK234/period

文档图示 模块文档[创建]
--此為Module:Chronology category template/period之暫存區
--未來主模組完成後將移動至Module:Chronology category template/period

local format = mw.ustring.format --導入文字格式處理
local period = {}

local function BeforeChrist(year)
    --公元前
	if (string.match(year,"-") ~= nil) then return "前"
    else return ""
	end
end

local function SwitchTrueFalse(x,TrueReturn,FalseReturn)
    --x為true與false之行為
    if (x == true) then
        return TrueReturn
    elseif (x == false) then
        return FalseReturn
    end
end

local function ZeroDecadeFix(x)
    --0年代/前0年代計算修正
    if (x == "0") then
        return tonumber(x)+1
    elseif (x == "-0") then
        return tonumber(x)-1
    else
        return tonumber(x)
    end    
end

function period.MILLENNIUM(year,text)
    --{{MILLENNIUM}}
    local val = ZeroDecadeFix(year)
    local bc = BeforeChrist(year)
    local func = math.floor((math.abs(val)-1)/1000+1)
    local TrueReturn = format("%s".."%.0f".."千紀", bc, func)
    local FalseReturn = function(x) if (x < 0) then return format("-%.0f", func) else return format("%.0f", func) end end
    return SwitchTrueFalse(text,TrueReturn,FalseReturn(val))
end

function period.CENTURY(year,text)
    --{{CENTURY}}
    local val = ZeroDecadeFix(year)
    local bc = BeforeChrist(year)
    local func = math.floor((math.abs(val)-1)/100+1)
    local TrueReturn = format("%s".."%.0f".."世紀", bc, func)
    local FalseReturn = function(x) if (x < 0) then return format("-%.0f", func) else return format("%.0f", func) end end
    return SwitchTrueFalse(text,TrueReturn,FalseReturn(val))        
end

function period.DECADE(year,text)
    --{{DECADE}}
    local val = ZeroDecadeFix(year)
    local bc = BeforeChrist(year)
    local func = format("%.0f", math.floor(math.abs(year/10))*10)
    local TrueReturn = format("%s".."%.0f".."年代", bc, func)
    local FalseReturn = function(x) if (x < 0) then return format("-%.0f", func) else return format("%.0f", func) end end
    return SwitchTrueFalse(text,TrueReturn,FalseReturn(val))
end

function period.YEAR(year,text)
    --{{YEAR}}
    local val = tonumber(year)
    local bc = BeforeChrist(year)
    local TrueReturn = format("%s".."%d".."年", bc, math.abs(val))
    local FalseReturn = format("%d", val)
    return SwitchTrueFalse(text,TrueReturn,FalseReturn)
end

return period