Modul:Pendirian
Dokumentasi untuk modul ini dapat dibuat di Modul:Pendirian/doc
local p = {}
-- Nama-nama bulan dalam Bahasa Indonesia
local nama_bulan = {
[1] = "Januari", [2] = "Februari", [3] = "Maret", [4] = "April",
[5] = "Mei", [6] = "Juni", [7] = "Juli", [8] = "Agustus",
[9] = "September", [10] = "Oktober", [11] = "November", [12] = "Desember"
}
-- Mengambil argumen dari template secara aman
local function get_args(frame)
local args = {}
if frame.getParent then
local pargs = frame:getParent().args
if pargs then
for k, v in pairs(pargs) do
args[k] = mw.text.trim(tostring(v))
end
end
end
if frame.args then
for k, v in pairs(frame.args) do
args[k] = mw.text.trim(tostring(v))
end
end
return args
end
-- Menghitung jumlah hari dalam suatu bulan
local function days_in_month(month, year)
local days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
if month == 2 then
if (year % 4 == 0 and year % 100 ~= 0) or (year % 400 == 0) then
return 29
end
end
return days[month] or 30
end
-- Algoritma presisi selisih waktu
local function calculate_age(y1, m1, d1, y2, m2, d2)
local years = y2 - y1
local months = m2 - m1
local days = d2 - d1
if days < 0 then
months = months - 1
local prev_month = m2 - 1
local prev_year = y2
if prev_month < 1 then
prev_month = 12
prev_year = prev_year - 1
end
days = days + days_in_month(prev_month, prev_year)
end
if months < 0 then
years = years - 1
months = months + 12
end
return years, months, days
end
-- Parsing input tanggal
local function parse_date(args)
local y, m, d
local input_str = args[1] or args["tanggal"] or args["date"]
if input_str and input_str ~= "" then
y, m, d = input_str:match("^([0-9]%d%d%d)%-([0-9]%d?)%-([0-9]%d?)$")
if not y then
y, m, d = input_str:match("^([0-9]%d%d%d)/([0-9]%d?)/([0-9]%d?)$")
end
if not y then
y = input_str:match("^([0-9]%d%d%d)$")
if y then m, d = 1, 1 end
end
end
if not y and args[1] and args[1]:match("^%d+$") then
y = tonumber(args[1])
m = tonumber(args[2])
d = tonumber(args[3])
end
if not y and args["tahun"] then y = tonumber(args["tahun"]) end
if not m and args["bulan"] then m = tonumber(args["bulan"]) end
if not d and args["hari"] then d = tonumber(args["hari"]) end
return tonumber(y), tonumber(m) or 1, tonumber(d) or 1
end
function p.utama(frame)
local args = get_args(frame)
local y1, m1, d1 = parse_date(args)
if not y1 then
return '<span class="error" style="color: #d33; font-weight: bold;">[Peringatan: Masukkan tanggal pendirian yang valid]</span>'
end
local now = os.date("!*t")
local y2 = tonumber(args["ref_tahun"]) or now.year
local m2 = tonumber(args["ref_bulan"]) or now.month
local d2 = tonumber(args["ref_hari"]) or now.day
local is_future = false
if y1 > y2 or (y1 == y2 and m1 > m2) or (y1 == y2 and m1 == m2 and d1 > d2) then
is_future = true
end
local years, months, days
if is_future then
years, months, days = calculate_age(y2, m2, d2, y1, m1, d1)
else
years, months, days = calculate_age(y1, m1, d1, y2, m2, d2)
end
local mode = string.lower(args["mode"] or args["format"] or args["opsi"] or "otomatis")
local text_usia = ""
if years == 0 and months == 0 and days == 0 then
text_usia = "hari ini"
else
if mode == "detail" or mode == "presisi" or mode == "lengkap" then
local parts = {}
if years > 0 then table.insert(parts, years .. " tahun") end
if months > 0 then table.insert(parts, months .. " bulan") end
if days > 0 then table.insert(parts, days .. " hari") end
text_usia = table.concat(parts, ", ")
elseif mode == "ringkas" or mode == "sederhana" then
if years > 0 then
text_usia = years .. " tahun"
elseif months > 0 then
text_usia = months .. " bulan"
else
text_usia = days .. " hari"
end
else
if years >= 1 then
if months > 0 then
text_usia = years .. " tahun, " .. months .. " bulan"
else
text_usia = years .. " tahun"
end
elseif months >= 1 then
if days > 0 then
text_usia = months .. " bulan, " .. days .. " hari"
else
text_usia = months .. " bulan"
end
else
text_usia = days .. " hari"
end
end
if is_future then
text_usia = "akan diluncurkan dalam " .. text_usia
else
text_usia = text_usia .. " yang lalu"
end
end
local date_str = d1 .. " " .. (nama_bulan[m1] or "") .. " " .. y1
local show_date = args["tampilkan_tanggal"] or args["show_date"] or "ya"
local kurung = args["kurung"] or "ya"
local output = ""
if mode == "hanya_usia" or mode == "age_only" or show_date == "tidak" or show_date == "no" then
if kurung == "ya" or kurung == "yes" then
output = "(" .. text_usia .. ")"
else
output = text_usia
end
else
if kurung == "ya" or kurung == "yes" then
output = date_str .. ' <small class="explain" style="color: #555;">(' .. text_usia .. ')</small>'
else
output = date_str .. " — " .. text_usia
end
end
local auto_cat = args["kategori"] or args["cat"]
local jenis = string.lower(args["tipe"] or args["jenis"] or "organisasi")
if auto_cat == "ya" or auto_cat == "yes" then
if jenis == "perusahaan" then
output = output .. "[[Kategori:Perusahaan yang didirikan tahun " .. y1 .. "]]"
elseif jenis == "produk" then
output = output .. "[[Kategori:Produk yang diluncurkan tahun " .. y1 .. "]]"
elseif jenis == "papan" or jenis == "dewan" or jenis == "komite" then
output = output .. "[[Kategori:Papan dan dewan yang dibentuk tahun " .. y1 .. "]]"
else
output = output .. "[[Kategori:Organisasi yang didirikan tahun " .. y1 .. "]]"
end
end
return output
end
return p