コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

モジュール:サンドボックス/nanona15dobato/test

モジュールの解説[作成]
local p = {}

local function parseDate(dateStr)
    local len = #dateStr
    if len == 2 then -- mm
        return {month = tonumber(dateStr:sub(1, 2))}
    elseif len == 4 then -- mmdd
        return {
            month = tonumber(dateStr:sub(1, 2)),
            day = tonumber(dateStr:sub(3, 4))
        }
    elseif len == 6 then -- yyyymm
        return {
            year = tonumber(dateStr:sub(1, 4)),
            month = tonumber(dateStr:sub(5, 6))
        }
    elseif len == 8 then -- yyyymmdd
        return {
            year = tonumber(dateStr:sub(1, 4)),
            month = tonumber(dateStr:sub(5, 6)),
            day = tonumber(dateStr:sub(7, 8))
        }
    elseif len == 10 then -- yyyymmddhh
        return {
            year = tonumber(dateStr:sub(1, 4)),
            month = tonumber(dateStr:sub(5, 6)),
            day = tonumber(dateStr:sub(7, 8)),
            hour = tonumber(dateStr:sub(9, 10))
        }
    elseif len == 12 then -- yyyymmddhhmm
        return {
            year = tonumber(dateStr:sub(1, 4)),
            month = tonumber(dateStr:sub(5, 6)),
            day = tonumber(dateStr:sub(7, 8)),
            hour = tonumber(dateStr:sub(9, 10)),
            min = tonumber(dateStr:sub(11, 12))
        }
    elseif len == 14 then -- yyyymmddhhmmss
        return {
            year = tonumber(dateStr:sub(1, 4)),
            month = tonumber(dateStr:sub(5, 6)),
            day = tonumber(dateStr:sub(7, 8)),
            hour = tonumber(dateStr:sub(9, 10)),
            min = tonumber(dateStr:sub(11, 12)),
            sec = tonumber(dateStr:sub(13, 14))
        }
    else
        return nil -- 無効なフォーマット
    end
end

local function toTimestamp(date, defaultNow)
    return os.time {
        year = date.year or defaultNow.year,
        month = date.month or defaultNow.month,
        day = date.day or defaultNow.day,
        hour = date.hour or 0,
        min = date.min or 0,
        sec = date.sec or 0
    }
end

function p.timehanni(frame)
    local args = frame.args
    local startStr = args[1]
    local endStr = args[2]
    

    local now = os.time() + 32400

    local nowTimestamp = os.date("*t",now)

    if not startStr and not endStr then
        return "エラー: 引数が不足しています。"
    elseif startStr and not endStr then
        local startDate = parseDate(startStr)
        if not startDate then
            return "エラー: 第一引数の日付フォーマットが無効。"
        end
        local startTimestamp = toTimestamp(startDate, nowTimestamp)
        return nowTimestamp >= startTimestamp and "true" or "false"
    elseif not startStr and endStr then
        local endDate = parseDate(endStr)
        if not endDate then
            return "エラー: 第二引数の日付フォーマットが無効。"
        end
        local endTimestamp = toTimestamp(endDate, now)
        return nowTimestamp < endTimestamp and "true" or "false"
    else
        local startDate = parseDate(startStr)
        local endDate = parseDate(endStr)
        if not startDate or not endDate then
            return "エラー: 日付フォーマットが無効。"
        end
        local startTimestamp = toTimestamp(startDate, nowTimestamp)
        local endTimestamp = toTimestamp(endDate, nowTimestamp)
        return (startTimestamp <= now and now <= endTimestamp) and "true" or "false"
    end
end
function p.oshougatu(frame)
    local startStr = frame.args[1]
    local endStr = frame.args[2]


    local isInRange = p.timehanni(frame)
    if isInRange == "true" then
        return frame:preprocess('<div style="overflow-x: auto; margin: 5px; padding: 5px; border: 2px solid #f00; border-radius: 10px; line-height: 1.25;"><div style="text-align: center; font-size:large;">あけましておめでとうございます</div><div style="text-align: center;">ここは、なノな15どバとの利用者ページです。</div><div style="text-align: center;">キャッシュ日時(UTC):{{#time:Y年Fj日 (D) H:i}}({{Purge|破棄}})</div></div>')
    else
        return frame:preprocess('<div style="overflow-x: auto; margin: 5px; padding: 5px; border: 1px solid #a7d7f9; border-radius: 10px; line-height: 1.25; background-color: #ffffee"><div style="text-align: center; font-size:large;">{{UserName|n = IP利用者}}さん、ようこそ</div><div style="text-align: center;">ここは、なノな15どバとの利用者ページです。</div><div style="text-align: center;">キャッシュ日時(UTC):{{#time:Y年Fj日 (D) H:i}}({{Purge|破棄}})</div></div>')
    end
end

return p