「モジュール:Calendar」の版間の差分

提供:エンタメデータベースWiki
移動先:案内検索
2行目: 2行目:


function p.generateCalendar(frame)
function p.generateCalendar(frame)
     local year = tonumber(frame.args.year) or os.date("%Y")
     local year = tonumber(frame.args.year) or tonumber(os.date("%Y"))
     local month = tonumber(frame.args.month) or os.date("%m")
     local month = tonumber(frame.args.month) or tonumber(os.date("%m"))
     local days_in_month = {31, [2] = (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) and 29 or 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
 
    -- 日数を決定
     local days_in_month = {
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    }
 
    -- 閏年の計算
    if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
        days_in_month[2] = 29
    end


     local month_name = os.date("%B", os.time{year=year, month=month, day=1})
     local month_name = os.date("%B", os.time{year=year, month=month, day=1})
     local first_day = os.date("*t", os.time{year=year, month=month, day=1}).wday
     local first_day = os.date("*t", os.time{year=year, month=month, day=1}).wday
     local days = days_in_month[month]
     local days = days_in_month[month]
   
 
     local calendar = {"{| class=\"wikitable\""}
     local calendar = {"{| class=\"wikitable\""}
     table.insert(calendar, "|+ " .. year .. "年 " .. month_name)
     table.insert(calendar, "|+ " .. year .. "年 " .. month_name)
26行目: 35行目:
         local date_link = string.format("[[%d年%02d月%02d日|%02d]]", year, month, day, day)
         local date_link = string.format("[[%d年%02d月%02d日|%02d]]", year, month, day, day)
         table.insert(calendar, "| " .. date_link)
         table.insert(calendar, "| " .. date_link)
       
 
         -- 土曜日の後は次の行に移る
         -- 土曜日の後は次の行に移る
         if (day + first_day - 1) % 7 == 0 then
         if (day + first_day - 1) % 7 == 0 then

2025年1月13日 (月) 09:07時点における版

このモジュールについての説明文ページを モジュール:Calendar/doc に作成できます

local p = {}

function p.generateCalendar(frame)
    local year = tonumber(frame.args.year) or tonumber(os.date("%Y"))
    local month = tonumber(frame.args.month) or tonumber(os.date("%m"))

    -- 日数を決定
    local days_in_month = {
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    }

    -- 閏年の計算
    if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
        days_in_month[2] = 29
    end

    local month_name = os.date("%B", os.time{year=year, month=month, day=1})
    local first_day = os.date("*t", os.time{year=year, month=month, day=1}).wday
    local days = days_in_month[month]

    local calendar = {"{| class=\"wikitable\""}
    table.insert(calendar, "|+ " .. year .. "年 " .. month_name)

    -- 曜日のヘッダー
    table.insert(calendar, "! 日 !! 月 !! 火 !! 水 !! 木 !! 金 !! 土")
    table.insert(calendar, "|-")

    -- 空のセルを追加して、月の最初の日が正しい曜日に来るように調整
    for i = 1, first_day - 1 do
        table.insert(calendar, "| ")
    end

    -- 各日付のセルを生成
    for day = 1, days do
        local date_link = string.format("[[%d年%02d月%02d日|%02d]]", year, month, day, day)
        table.insert(calendar, "| " .. date_link)

        -- 土曜日の後は次の行に移る
        if (day + first_day - 1) % 7 == 0 then
            table.insert(calendar, "|-")
        end
    end

    -- テーブルの閉じタグ
    table.insert(calendar, "|}")
    
    return table.concat(calendar, "\n")
end

return p