Module:TimeGreeting: Difference between revisions

From Empire of Dragons
Jump to navigation Jump to search
(Created page with "-- Module:TimeGreeting local p = {} function p.getGreeting() local now = mw.getCurrentFrame():callParserFunction("#time", "U") local hours = tonumber(now:match("(%d%d)")) if hours >= 6 and hours < 12 then return "Good morning" elseif hours >= 12 and hours < 18 then return "Good afternoon" else return "Good evening" end end function p.displayGreeting(frame) return p.getGreeting() end return p")
 
No edit summary
 
(18 intermediate revisions by the same user not shown)
Line 4: Line 4:


function p.getGreeting()
function p.getGreeting()
     local now = mw.getCurrentFrame():callParserFunction("#time", "U")
    -- Use local time (server's time zone)
     local now = mw.getCurrentFrame():preprocess('{{#time:YmdHis}}')
    local offsetHours = tonumber(mw.getCurrentFrame():callParserFunction("#time", "H"))
    mw.getCurrentFrame():preprocess('{{#time:YmdHis}}')
 
    -- Alternatively, use UTC+1
    -- local now = mw.getCurrentFrame():callParserFunction("#time", "Y-m-d H:i:s", "1")
    offsetHours = offsetHours + 1
 
     local hours = tonumber(now:match("(%d%d)"))
     local hours = tonumber(now:match("(%d%d)"))


     if hours >= 6 and hours < 12 then
     if hours then
        return "Good morning"
        -- Adjust for local time zone
    elseif hours >= 12 and hours < 18 then
        hours = hours + offsetHours
         return "Good afternoon"
 
        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
     else
         return "Good evening"
         return "Error: Unable to determine the time"
     end
     end
end
end

Latest revision as of 10:33, 29 January 2024

Unknown time | Full local time: 08:27:35


-- Module:TimeGreeting

local p = {}

function p.getGreeting()
    -- Use local time (server's time zone)
    local now = mw.getCurrentFrame():preprocess('{{#time:YmdHis}}')
    local offsetHours = tonumber(mw.getCurrentFrame():callParserFunction("#time", "H"))
    mw.getCurrentFrame():preprocess('{{#time:YmdHis}}')

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

    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