Module:Sandbox/Cyborg Coder/Dates

.mw-parser-output .documentation,.mw-parser-output .documentation-metadata{border:1px solid var(--border-color-base,#a2a9b1);background-color:#ecfcf4;color:inhe

Module:Sandbox/Cyborg Coder/Dates
--Cyborg Coder Google Code-in, Lua Task 7 - Date formatting
local p = {}

local function leapd(y)
	if y % 1000 == 0 then return 29 end
	if y % 100 == 0 then return 28 end
	if y% 4 == 0 then return 29 end
	return 28
end

months = { "jan", "feb", "mar", "apr", "may", "jun",
	"jul", "aug", "sep", "oct", "nov", "dec" }
local days_in_month = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
days_in_month[0] = 0
local month_idx = {}
for i, v in ipairs(months) do
	month_idx[v] = i
end

local function day_try(d, m, y)
	days_in_month[2] = leapd(y)
	if d < 1 or d > days_in_month[m] then
		return "Invalid"
	end
	return "Valid"
end

function p.date(frame)
	local date = frame.args.text or mw.text.trim(frame.args[1] or "")
	date = " " .. date
	local dformat = frame.args.format or "none"
	if date == "" then date = "No date" end
	--local d, y = date:match("(%d+)%D+(%d+)")
	local d = date:match("%D(%d%d)%D")
	local y = date:match("(%d%d%d+)")
	local mnth = date:match("%a+") or ""
	mnth = string.upper(string.sub(mnth, 1, 1)) .. string.sub(mnth, 2)
	if not months[string.lower(string.sub(mnth, 1, 3))] ~= nil then mnth = date:match("%u%a+") or "" end
	d, y = tonumber(d) or 1, tonumber(y) or 0
	local m = month_idx[mnth:sub(1,3):lower()] or tonumber(date:match("%D0(%d)")) or 0
	local v = day_try(d, m, y)
	local out = date .. " = " .. d .. " " .. m .. " " .. y .. " " .. mnth
	out = out .. " = " .. v
	if v == "Invalid" then return out .. " -> Invalid Entry" end
	out = out .. " -> ".. d .. " " .. mnth .. " " .. y
	return out .. "<br>"
end

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.