「Module:MemberStats」の版間の差分
表示
ページの作成:「local p = {} function p.averageAge(frame) local args = frame.args local totalAge = 0 local count = 0 -- 今日の日付を取得 (YYYYMMDD) local today = tonumber(os.date("%Y%m%d")) -- 引数から生年月日をスキャン for _, birthStr in ipairs(args) do local birth = tonumber(birthStr) if birth then -- 年齢計算ロジック: floor((今日 - 誕生日) / 10000) local age = math.fl…」 |
編集の要約なし |
||
| 1行目: | 1行目: | ||
local p = {} | local p = {} | ||
-- 今日の日付をMediaWikiマジックワードで取得 | |||
local function getTodayYmd() | |||
local frame = mw.getCurrentFrame() | |||
local todayStr = frame:preprocess("{{CURRENTYEAR}}{{CURRENTMONTH}}{{CURRENTDAY}}") -- 例: 20260220 | |||
return tonumber(todayStr) | |||
end | |||
function p.averageAge(frame) | function p.averageAge(frame) | ||
| 5行目: | 12行目: | ||
local totalAge = 0 | local totalAge = 0 | ||
local count = 0 | local count = 0 | ||
local today = getTodayYmd() -- os.dateの代わり | |||
local today = | if not today then | ||
return "日付取得エラー" | |||
end | |||
for _, birthStr in ipairs(args) do | for _, birthStr in ipairs(args) do | ||
local birth = tonumber(birthStr) | local birth = tonumber(birthStr) | ||
if birth then | if birth and birth >= 19000101 and birth <= today then | ||
-- | -- 正確な年齢計算(誕生日が過ぎているかを考慮) | ||
local | local birthYear = math.floor(birth / 10000) | ||
local birthMonth = math.floor((birth % 10000) / 100) | |||
local birthDay = birth % 100 | |||
local todayYear = math.floor(today / 10000) | |||
local todayMonth = math.floor((today % 10000) / 100) | |||
local todayDay = today % 100 | |||
local age = todayYear - birthYear | |||
if todayMonth < birthMonth or (todayMonth == birthMonth and todayDay < birthDay) then | |||
age = age - 1 | |||
end | |||
totalAge = totalAge + age | totalAge = totalAge + age | ||
count = count + 1 | count = count + 1 | ||
end | end | ||
end | end | ||
if count == 0 then return "0" end | if count == 0 then return "0" end | ||
local avg = totalAge / count | local avg = totalAge / count | ||
return string.format("%.2f", avg) | return string.format("%.2f", avg) | ||
| 29行目: | 48行目: | ||
function p.count(frame) | function p.count(frame) | ||
local count = 0 | local count = 0 | ||
for | for _ in pairs(frame.args) do -- ipairsではなくpairsで安全 | ||
count = count + 1 | count = count + 1 | ||
end | end | ||
2026年2月20日 (金) 10:43時点における版
local p = {}
-- 今日の日付をMediaWikiマジックワードで取得 local function getTodayYmd()
local frame = mw.getCurrentFrame()
local todayStr = frame:preprocess("2026041") -- 例: 20260220
return tonumber(todayStr)
end
function p.averageAge(frame)
local args = frame.args local totalAge = 0 local count = 0 local today = getTodayYmd() -- os.dateの代わり
if not today then
return "日付取得エラー"
end
for _, birthStr in ipairs(args) do
local birth = tonumber(birthStr)
if birth and birth >= 19000101 and birth <= today then
-- 正確な年齢計算(誕生日が過ぎているかを考慮)
local birthYear = math.floor(birth / 10000)
local birthMonth = math.floor((birth % 10000) / 100)
local birthDay = birth % 100
local todayYear = math.floor(today / 10000)
local todayMonth = math.floor((today % 10000) / 100)
local todayDay = today % 100
local age = todayYear - birthYear
if todayMonth < birthMonth or (todayMonth == birthMonth and todayDay < birthDay) then
age = age - 1
end
totalAge = totalAge + age
count = count + 1
end
end
if count == 0 then return "0" end
local avg = totalAge / count
return string.format("%.2f", avg)
end
function p.count(frame)
local count = 0
for _ in pairs(frame.args) do -- ipairsではなくpairsで安全
count = count + 1
end
return count
end
return p