「モジュール:Calendar」の版間の差分
提供:エンタメデータベースWiki
![]() H1G_yusaku@ヘイグ (トーク | 投稿記録) |
![]() H1G_yusaku@ヘイグ (トーク | 投稿記録) |
||
1行目: | 1行目: | ||
local p = {} | local p = {} | ||
-- 閏年の計算 | |||
local function isLeapYear(year) | |||
return (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) | |||
end | |||
function p.generateCalendar(frame) | function p.generateCalendar(frame) | ||
-- 引数から年と月を取得、無ければ現在の日付を使用 | |||
local year = tonumber(frame.args.year) or tonumber(os.date("%Y")) | local year = tonumber(frame.args.year) or tonumber(os.date("%Y")) | ||
local month = tonumber(frame.args.month) or tonumber(os.date("%m")) | local month = tonumber(frame.args.month) or tonumber(os.date("%m")) | ||
10行目: | 16行目: | ||
} | } | ||
-- | -- 閏年の場合、2月の長さを29日に変更 | ||
if (year | if isLeapYear(year) then | ||
days_in_month[2] = 29 | days_in_month[2] = 29 | ||
end | end | ||
22行目: | 28行目: | ||
table.insert(calendar, "|+ " .. year .. "年 " .. month_name) | table.insert(calendar, "|+ " .. year .. "年 " .. month_name) | ||
-- 前月と翌月へのリンク | |||
local prev_year = year | |||
local prev_month = month - 1 | |||
if prev_month < 1 then | |||
prev_month = 12 | |||
prev_year = prev_year - 1 | |||
end | |||
local next_year = year | |||
local next_month = month + 1 | |||
if next_month > 12 then | |||
next_month = 1 | |||
next_year = next_year + 1 | |||
end | |||
table.insert(calendar, string.format("! [[%d年%02d月|<< 前月]] !! [[%d年%02d月|翌月 >>]]", prev_year, prev_month, next_year, next_month)) | |||
table.insert(calendar, "|-") | |||
-- 曜日のヘッダー | -- 曜日のヘッダー | ||
table.insert(calendar, "! 日 !! 月 !! 火 !! 水 !! 木 !! 金 !! 土") | table.insert(calendar, "! 日 !! 月 !! 火 !! 水 !! 木 !! 金 !! 土") |
2025年1月13日 (月) 09:08時点における版
このモジュールについての説明文ページを モジュール:Calendar/doc に作成できます
local p = {}
-- 閏年の計算
local function isLeapYear(year)
return (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0)
end
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
}
-- 閏年の場合、2月の長さを29日に変更
if isLeapYear(year) 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)
-- 前月と翌月へのリンク
local prev_year = year
local prev_month = month - 1
if prev_month < 1 then
prev_month = 12
prev_year = prev_year - 1
end
local next_year = year
local next_month = month + 1
if next_month > 12 then
next_month = 1
next_year = next_year + 1
end
table.insert(calendar, string.format("! [[%d年%02d月|<< 前月]] !! [[%d年%02d月|翌月 >>]]", prev_year, prev_month, next_year, next_month))
table.insert(calendar, "|-")
-- 曜日のヘッダー
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