Module:Sandbox/Cyborg Coder/Dates: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(20 intermediate revisions by the same user not shown)
Line 1:
--Cyborg Coder Google Code-in, Lua Task 7 - Date formatting
local p = {}
 
--local function
 
--function p.date( frame )
-- local dtext = frame.args.text
-- if (dtext=="" or dtext==nil) then return "No date given" end
-- local dformat = frame.args.format or default
-- return dtext
--end
 
local function leapd(y)
Line 18 ⟶ 9:
end
 
local 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 }
Line 37 ⟶ 28:
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 default
-- local dtextdformat = frame.args.textformat or "none"
if date == "" then date = "No date" end
--local d, y = date:match("(%d+)%D+(%d+)")
local d = date:match("%D([1-3]%d%d)%D")
local y = date:match("(%d%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
Line 49 ⟶ 42:
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