Module:TimeGreeting

From Empire of Dragons
Revision as of 10:08, 19 January 2024 by Oberoten (talk | contribs)
Jump to navigation Jump to search

Good night | Full local time: 08:25:44


-- Module:TimeGreeting

local p = {}

function p.getGreeting()
    -- Use local time (server's time zone)
    local now = mw.getCurrentFrame():callParserFunction("#time", "Y-m-d H:i:s")
    --local offsetHours = tonumber(mw.getCurrentFrame():callParserFunction("#time", "O")) / 100

    -- Alternatively, use UTC+1
    --now = mw.getCurrentFrame():callParserFunction("#time", "Y-m-d H:i:s", "1")
    local offsetHours = 2

    local hours = tonumber(now:match("(%d%d)"))

    if hours then
        -- Adjust for local time zone
        hours = hours + offsetHours

        local greeting
        if hours >= 6 and hours < 12 then
            greeting = "Good morning"
        elseif hours >= 12 and hours < 14 then
            greeting = "Good noon"
        elseif hours >= 14 and hours < 18 then
            greeting = "Good afternoon"
        elseif hours >= 18 and hours < 24 then
            greeting = "Good night"
        elseif hours >= 0 and hours < 6 then
            greeting = "Good midnight"
        else
            greeting = "Unknown time"
        end

        local fullTime = mw.getCurrentFrame():callParserFunction("#time", "H:i:s")

        return greeting .. " | Full local time: " .. fullTime
    else
        return "Error: Unable to determine the time"
    end
end

function p.displayGreeting(frame)
    return p.getGreeting()
end

return p