Prijeđi na sadržaj

Modul:Datum i dob

Izvor: Wikipedija


local p = {}
local formatirajDatum = require('Module:Formatiraj datum')

local function getMinMaxIso(isoDatum)
    local y, m, d = isoDatum:match('(%d+)-(%d+)-(%d+)')
    if y then
        return isoDatum, isoDatum
    end

    y, m = isoDatum:match('(%d+)-(%d+)')
    if y and m then
        local maxDan = formatirajDatum.getMaxDan(y, m)
        local earliest = string.format("%s-%s-01", y, m)
        local latest = string.format("%s-%s-%02d", y, m, maxDan)
        return earliest, latest
    end

    y = isoDatum:match('(%d+)')
    if y then
        return y .. "-01-01", y .. "-12-31"
    end

    return nil, nil
end

local function CalculateAge(isoPocetak, isoKraj)
    if not isoPocetak or not isoKraj then return nil end

    local p_min, p_max = getMinMaxIso(isoPocetak)
    local k_min, k_max = getMinMaxIso(isoKraj)

    if not p_min or not k_max then return nil end

    local y1_min, m1_min, d1_min = p_max:match('(%d+)-(%d+)-(%d+)')
    local y2_min, m2_min, d2_min = k_min:match('(%d+)-(%d+)-(%d+)')
    y1_min, m1_min, d1_min = tonumber(y1_min), tonumber(m1_min), tonumber(d1_min)
    y2_min, m2_min, d2_min = tonumber(y2_min), tonumber(m2_min), tonumber(d2_min)
    local minGodine = y2_min - y1_min
    if (m2_min < m1_min) or (m2_min == m1_min and d2_min < d1_min) then
        minGodine = minGodine - 1
    end

    local y1_max, m1_max, d1_max = p_min:match('(%d+)-(%d+)-(%d+)')
    local y2_max, m2_max, d2_max = k_max:match('(%d+)-(%d+)-(%d+)')
    y1_max, m1_max, d1_max = tonumber(y1_max), tonumber(m1_max), tonumber(d1_max)
    y2_max, m2_max, d2_max = tonumber(y2_max), tonumber(m2_max), tonumber(d2_max)
    local maxGodine = y2_max - y1_max
    if (m2_max < m1_max) or (m2_max == m1_max and d2_max < d1_max) then
        maxGodine = maxGodine - 1
    end

    if minGodine < 0 or maxGodine > 122 then
        return nil
    end

    if minGodine == maxGodine then
        if minGodine > 0 then
            return string.format("%d god.", minGodine)
        else
            local mjeseci = (y2_max - y1_max) * 12 + (m2_max - m1_max)
            if d2_max < d1_max then mjeseci = mjeseci - 1 end
            if mjeseci < 0 then mjeseci = 0 end
            return string.format("%d mj.", mjeseci)
        end
    else
        return string.format("%d–%d god.", minGodine, maxGodine)
    end
end

function p.birth(frame)
    local args = frame.args
    local birthDate = mw.text.trim(args[1] or "")
    local deathDate = mw.text.trim(args[2] or "")

    if birthDate == "" then return "" end

    if string.find(birthDate, 'god', 1, true) or string.find(birthDate, 'godina', 1, true) or string.find(birthDate, 'mj', 1, true) or string.find(birthDate, 'ForceAgeToShow', 1, true) or string.find(birthDate, 'dob', 1, true) then
        return birthDate
    end

    local bdaySpan = formatirajDatum.formatDateWithClass{ args = { [1] = birthDate, [2] = 'bday' } }
    local ISObirth = formatirajDatum.getISO{ args = { [1] = birthDate } }

    local visiblePart = birthDate
    local category = ""

    if deathDate == "" then
        if ISObirth then
            local today = mw.getContentLanguage():formatDate('Y-m-d')
            local age = CalculateAge(ISObirth, today)
            if age then
                visiblePart = visiblePart .. " (" .. age .. ")"
                if mw.title.getCurrentTitle().namespace == 0 then
                    category = '[[Kategorija:Stranice s automatskim izračunom dobi osobe]]'
                end
            else
                if mw.title.getCurrentTitle().namespace == 0 then
                    category = '[[Kategorija:Stranice bez izračuna dobi osobe]]'
                end
            end
        else
            if mw.title.getCurrentTitle().namespace == 0 then
                category = '[[Kategorija:Stranice bez izračuna dobi osobe]]'
            end
        end
    end

    if bdaySpan ~= "" then
        return bdaySpan .. " " .. visiblePart .. category
    else
        return visiblePart .. category
    end
end

function p.death(frame)
    local args = frame.args
    local birthDate = mw.text.trim(args[1] or "")
    local deathDate = mw.text.trim(args[2] or "")

    if deathDate == "" then
        return ""
    end

    if string.find(deathDate, 'god', 1, true) or string.find(deathDate, 'godina', 1, true) or string.find(deathDate, 'mj', 1, true) or string.find(deathDate, 'ForceAgeToShow', 1, true) or string.find(deathDate, 'dob', 1, true) then
        return deathDate
    end
    if string.find(birthDate, 'god', 1, true) or string.find(birthDate, 'godina', 1, true) or string.find(birthDate, 'mj', 1, true) or string.find(birthDate, 'ForceAgeToShow', 1, true) or string.find(birthDate, 'dob', 1, true) then
        return deathDate
    end

    local ISObirth = formatirajDatum.getISO{ args = { [1] = birthDate } }
    local ISOdeath = formatirajDatum.getISO{ args = { [1] = deathDate } }

    local display = deathDate
    local category = ""

    if ISObirth and ISOdeath then
        local age = CalculateAge(ISObirth, ISOdeath)
        if age then
            display = display .. " (" .. age .. ")"
            if mw.title.getCurrentTitle().namespace == 0 then
                category = '[[Kategorija:Stranice s automatskim izračunom dobi osobe]]'
            end
        else
            if mw.title.getCurrentTitle().namespace == 0 then
                category = '[[Kategorija:Stranice bez izračuna dobi osobe]]'
            end
        end
    else
        if mw.title.getCurrentTitle().namespace == 0 then
            category = '[[Kategorija:Stranice bez izračuna dobi osobe]]'
        end
    end

    return display .. category
end

return p