Module:Birth date and age

From Porn Base Central, the free encyclopedia of gay porn
Jump to navigation Jump to search

Documentation for this module may be created at Module:Birth date and age/doc

local p = {}

local function pad2digit(num)
    return string.format("%02d", num)
end

local function getZodiacSign(day, month)
    local zodiacSigns = {
        { name = "Capricorn", start = { day = 22, month = 12 }, end_ = { day = 19, month = 1 } },
        { name = "Aquarius", start = { day = 20, month = 1 }, end_ = { day = 18, month = 2 } },
        { name = "Pisces", start = { day = 19, month = 2 }, end_ = { day = 20, month = 3 } },
        { name = "Aries", start = { day = 21, month = 3 }, end_ = { day = 19, month = 4 } },
        { name = "Taurus", start = { day = 20, month = 4 }, end_ = { day = 20, month = 5 } },
        { name = "Gemini", start = { day = 21, month = 5 }, end_ = { day = 20, month = 6 } },
        { name = "Cancer", start = { day = 21, month = 6 }, end_ = { day = 22, month = 7 } },
        { name = "Leo", start = { day = 23, month = 7 }, end_ = { day = 22, month = 8 } },
        { name = "Virgo", start = { day = 23, month = 8 }, end_ = { day = 22, month = 9 } },
        { name = "Libra", start = { day = 23, month = 9 }, end_ = { day = 22, month = 10 } },
        { name = "Scorpio", start = { day = 23, month = 10 }, end_ = { day = 21, month = 11 } },
        { name = "Sagittarius", start = { day = 22, month = 11 }, end_ = { day = 21, month = 12 } },
    }

    for _, sign in ipairs(zodiacSigns) do
        if (month == sign.start.month and day >= sign.start.day) or (month == sign.end_.month and day <= sign.end_.day) then
            return sign.name
        end
    end

    return nil
end

local function calculateAge(year, month, day)
    local currentYear, currentMonth, currentDay = os.date('%Y'), os.date('%m'), os.date('%d')
    currentYear, currentMonth, currentDay = tonumber(currentYear), tonumber(currentMonth), tonumber(currentDay)

    local age = currentYear - year

    if currentMonth < month or (currentMonth == month and currentDay < day) then
        age = age - 1
    end

    return age
end

function p.main(frame)
    local args = require('Module:Arguments').getArgs(frame, {wrappers = 'Template:Birth date and age'})

    local year = tonumber(args[1] or args.year)
    local month = tonumber(args[2] or args.month)
    local day = tonumber(args[3] or args.day)
    local df = args.df

    if not year or not month or not day then
        return ''
    end

    local birthDate = string.format("[[:Category:%s %s birthdays|%s %s]] [[:Category:%s births|%s]]", os.date('%B', os.time({year = year, month = month, day = day})), day, day, os.date('%B', os.time({year = year, month = month, day = day})), year, year)
    local age = calculateAge(year, month, day)
    local ageString = string.format(" (age&nbsp;%s)", age)
    local zodiacSign = getZodiacSign(day, month)
    local output = string.format('%s%s', birthDate, ageString)

    if zodiacSign then
        local zodiacCategory = mw.title.getCurrentTitle().namespace == 0 and string.format('[[Category:%s]]', zodiacSign) or ''
        output = output .. string.format('<br>Astrology: [[File:%s.svg|13px|link=Category:%s|%s|class=noviewer]]%s', zodiacSign, zodiacSign, zodiacSign, zodiacCategory)
    end

    return output
end

return p