モジュール:Calendar
提供:エンタメデータベースWiki
このモジュールについての説明文ページを モジュール:Calendar/doc に作成できます
local p = {}
-- タイムゾーンオフセットの設定(日本時間 = UTC+9)
local JST_OFFSET = 9 * 60 * 60
-- 閏年の計算
local function isLeapYear(year)
return (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0)
end
-- 日本時間での現在時刻を取得
local function getJSTTime()
local utc = os.time()
return utc + JST_OFFSET
end
function p.generateCalendar(frame)
-- 引数から年と月を取得、無ければ現在の日付を使用
local current = os.date("*t", getJSTTime())
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_names = {
"1月", "2月", "3月", "4月", "5月", "6月",
"7月", "8月", "9月", "10月", "11月", "12月"
}
-- 月初めの曜日を計算(日本時間で)
local first_day_time = os.time({year=year, month=month, day=1}) + JST_OFFSET
local first_day = os.date("*t", first_day_time).wday
local days = days_in_month[month]
local calendar = {"{| class=\"wikitable\""}
table.insert(calendar, "|+ " .. year .. "年 " .. month_names[month])
-- 曜日のヘッダー
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, "|}")
-- 前月と翌月へのリンクをテーブル外に配置
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
-- 前月と翌月リンク
local prev_next_links = string.format(
"<div style=\"text-align: center;\">[[%d年%02d月|<< 前月]] | [[%d年%02d月|翌月 >>]]</div>",
prev_year, prev_month, next_year, next_month
)
-- カレンダーと前月・翌月のリンクを返す
return prev_next_links .. "\n" .. table.concat(calendar, "\n")
end
return p