Module:Birthday

From NeuroWiki
Jump to navigation Jump to search

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

local p = {}

-- Convert English month names to numbers
local function englishToNumber(month)
    local months = {
        January = 1, February = 2, March = 3,
        April = 4, May = 5, June = 6,
        July = 7, August = 8, September = 9,
        October = 10, November = 11, December = 12,
        Jan = 1, Feb = 2, Mar = 3, Apr = 4,
        Jun = 6, Jul = 7, Aug = 8, Sep = 9,
        Oct = 10, Nov = 11, Dec = 12
    }
    return months[month] or nil
end

function p.calculateAge(frame)
    local birthYear = tonumber(frame.args[1])
    local birthMonthInput = frame.args[2]
    local birthDay = tonumber(frame.args[3])

    -- Check for missing input
    if not birthYear and not birthMonthInput and not birthDay then
        return "Error: The input year, month, and day are all empty."
    end

    -- Convert month input
    local birthMonth
    if tonumber(birthMonthInput) then
        birthMonth = tonumber(birthMonthInput)  -- If it's already a number
    else
        birthMonth = englishToNumber(birthMonthInput)
    end

    -- Generate date string
    local dateString = ""

    if birthYear and birthMonth and birthDay then
        if type(birthMonthInput) == "string" and (birthMonthInput:match("^[A-Za-z]+$")) then
            -- English month format
            dateString = string.format("%s %d, %d", birthMonthInput, birthDay, birthYear)
        else
            -- Numeric format
            dateString = string.format("%d-%02d-%02d", birthYear, birthMonth, birthDay)
        end

        -- Calculate age
        local currentYear = tonumber(os.date("%Y"))
        local currentMonth = tonumber(os.date("%m"))
        local currentDay = tonumber(os.date("%d"))

        local age = currentYear - birthYear

        if currentMonth < birthMonth or (currentMonth == birthMonth and currentDay < birthDay) then
            age = age - 1
        end

        return string.format("%s (Age: %d)", dateString, age)
    else
        -- If the month is in English and year or day is missing
        if type(birthMonthInput) == "string" and (birthMonthInput:match("^[A-Za-z]+$")) then
            if birthYear and not birthDay then
                return string.format("%s, %d", birthMonthInput, birthYear)  -- Only year
            elseif birthDay then
                return string.format("%s %d", birthMonthInput, birthDay)  -- Only day
            else
                return string.format("%s", birthMonthInput)  -- Only month
            end
        end

        -- Handle cases where year, month, or day is missing
        if birthYear and birthMonth then
            return string.format("%d-%02d", birthYear, birthMonth)  -- Year and month
        elseif birthYear and birthDay then
            return string.format("%d-%02d", birthYear, birthDay)  -- Year and day
        elseif birthMonth and birthDay then
            return string.format("%02d-%02d", birthMonth, birthDay)  -- Month and day
        elseif birthYear then
            return string.format("%d", birthYear)  -- Only year
        elseif birthMonth then
            return string.format("%02d", birthMonth)  -- Only month
        elseif birthDay then
            return string.format("%02d", birthDay)  -- Only day
        else
            return "Error: The input year, month, and day are all empty."
        end
    end
end

return p