Module: Date range

From A Wiki of Ice and Fire
Jump to: navigation, search

Documentation for this module may be created at Module:Date range/doc

local getArgs = require('Module:Arguments').getArgs

local p = {}

local function is_empty(s)
    return s == nil or s == ''
end

-- TODO: Add circa (~), after (>) and before (<)

function p.main(frame)
    local args = getArgs(frame) -- arguments passed to this module
    return p._main(args)
end

function p._main(args)
    local from = args.from or args[1]
    local to = args.to or args[2]
    local text = args.text
    
    local from_n = tonumber(from)
    local from_s = (from_n ~= nil) and tostring(math.abs(from_n))
    local to_n = tonumber(to)
    local to_s = (to_n ~= nil) and tostring(math.abs(to_n))

    if is_empty(from) then
        if is_empty(to) then
            return args.text
        elseif to == '-?' then
            return "Until before Aegon's Conquest"
        elseif to == '+?' or to == '?' then
            return "Until after Aegon's Conquest"
        elseif to_n and to_n < 0 then
            return "Until " .. to_s .. " ʙᴄ"
        elseif to_n and to_n > 0 then
            return "Until " .. to_s .. " ᴀᴄ"
        end
    elseif from == '-?' then
        if is_empty(to) then
            return "Since before Aegon's Conquest"
        elseif to == '-?' then
            return "Before Aegon's Conquest"
        elseif to == '+?' or to == '?' then
            return "Before and after Aegon's Conquest"
        elseif to_n and to_n < 0 then
            return "?–" .. to_s .. " ʙᴄ"
        elseif to_n and to_n > 0 then
            return "? ʙᴄ – " .. to_s .. " ᴀᴄ"
        end
    elseif from == '+?' or from == '?' then
        if is_empty(to) then
            return "Since after Aegon's Conquest"
        elseif to == '-?' then
            error("Range impossible.")
        elseif to == '+?' or to == '?' then
            return "After Aegon's Conquest"
        elseif to_n and to_n < 0 then
            error("Range impossible.")
        elseif to_n and to_n > 0 then
            return "?–" .. to_s .. " ᴀᴄ"
        end
    elseif from_n and from_n < 0 then
        if is_empty(to) then
            return "Since " .. from_s .. " ʙᴄ"
        elseif to == '-?' then
            return from_s .. "–? ʙᴄ"
        elseif to == '+?' or to == '?' then
            return from_s .. " ʙᴄ – ? ᴀᴄ"
        elseif to_n and to_n == from_n then
            return "In " .. from_s .. " ʙᴄ"
        elseif to_n and to_n < from_n then
            error("Range impossible.")
        elseif to_n and to_n < 0 then
            return from_s .. "–" .. to_s .. " ʙᴄ"
        elseif to_n and to_n > 0 then
            return from_s .. " ʙᴄ – " .. to_s .. " ᴀᴄ"
        end
    elseif from_n and from_n > 0 then
        if is_empty(to) then
            return "Since " .. from_s .. " ᴀᴄ"
        elseif to == '-?' then
            error("Range impossible.")
        elseif to == '+?' or to == '?' then
            return from_s .. "–? ᴀᴄ"
        elseif to_n and to_n == from_n then
            return "In " .. from_s .. " ᴀᴄ"
        elseif to_n and to_n < from_n then
            error("Range impossible.")
        elseif to_n and to_n > from_n then
            return from_s .. "–" .. to_s .. " ᴀᴄ"
        end
    elseif from_n and from_n == 0 and to_n and to_n == 0 then
        return "During Aegon's Conquest"
    elseif from_n and from_n == 0 then
        return "Since Aegon's Conquest"
    elseif to_n and to_n == 0 then
        return "Until Aegon's Conquest"
    end
end

return p