Module:MostRecentValue
Yi palo
Documentation for this module may be created at Module:MostRecentValue/doc
local monthNames = {
["january"] = "01",
["february"] = "02",
["march"] = "03",
["april"] = "04",
["may"] = "05",
["june"] = "06",
["july"] = "07",
["august"] = "08",
["september"] = "09",
["october"] = "10",
["november"] = "11",
["december"] = "12",
["jan"] = "01",
["feb"] = "02",
["mar"] = "03",
["apr"] = "04",
["jun"] = "06",
["jul"] = "07",
["aug"] = "08",
["sep"] = "09",
["oct"] = "10",
["nov"] = "11",
["dec"] = "12"
}
local dmyPattern = "^(%d+)%s+(%w+)%s+(%d+)$"
local mdyPattern = "^(%w+)%s+(%d+),?%s+(%d+)$"
local myPattern = "^(%w+)%s+(%d+)$"
function dateToISO(dateString)
local day, month, year = dateString:lower():match(dmyPattern)
if year == nil then
-- failed to parse try again
month, day, year = dateString:lower():match(mdyPattern)
if year == nil then
-- failed to parse try again
month, year = dateString:lower():match(myPattern)
end
end
month = monthNames[month]
if month == nil or year == nil then
return nil
end
if day == nil or day == '' then
day = "01"
elseif tonumber(day) < 10 then
-- pad with a zero
day = "0" .. tonumber(day)
end
return year.."-"..month.."-"..day
end
local p = {}
function p.mostRecentValue( frame )
v1 = frame.args["v1"]
d1 = frame.args["d1"]
v2 = frame.args["v2"]
d2 = frame.args["d2"]
d1Iso = dateToISO(d1)
d2Iso = dateToISO(d2)
if d1Iso == nil and d2Iso == nil then
error("Failed to parse both dates given")
elseif d2Iso == nil then
return v1
elseif d1Iso == nil then
return v2
end
if d1Iso < d2Iso then
return v2
else
return v1
end
end
return p