Module:Date Stuff: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 56: | Line 56: | ||
if date[2] == nil then | if date[2] == nil then | ||
date = {nil, nil, date[1], "year"} | date = {nil, nil, nil, tonumber(date[1]), "year"} | ||
elseif date[3] == nil then | elseif date[3] == nil then | ||
date = {monthNames[tonumber(date[1])] | date = {monthNames[tonumber(date[1])], tonumber(date[1]), nil, tonumber(date[2]), "month-year"} | ||
else | else | ||
date = {monthNames[tonumber(date[1])] | date = {monthNames[tonumber(date[1])], tonumber(date[1]), tonumber(date[2]), tonumber(date[3]), "month-day-year"} | ||
end | end | ||
return date | return date | ||
end | end | ||
Line 68: | Line 67: | ||
--Writes that date based on what is given. | --Writes that date based on what is given. | ||
function writeDate(date) | function writeDate(date) | ||
if date[ | if date[5] == "year" then | ||
return date[ | return date[4] | ||
elseif date[ | elseif date[5] == "month-year" then | ||
return date[1].." "..date[ | return date[1].." ("..date[2]..") "..date[4] | ||
elseif date[4] == "month-day-year" then | elseif date[4] == "month-day-year" then | ||
return date[1].." "..date[2]..", "..date[ | return date[1].." ("..date[2]..") "..date[3]..", "..date[4] | ||
end | end | ||
end | end | ||
Line 98: | Line 97: | ||
--Checks which math formula to use, based on how much of the date there is. | --Checks which math formula to use, based on how much of the date there is. | ||
if birthDate[ | if birthDate[5] == "year" or secondDate == "year" then | ||
age = math.floor(secondDate[ | age = math.floor(secondDate[4] - birthDate[4]) | ||
elseif birthDate[ | elseif birthDate[5] == "month-year" or secondDate == "month-year" then | ||
if birthDate[2] < secondDate[2] then | if birthDate[2] <= secondDate[2] then | ||
age = math.floor(secondDate[ | age = math.floor(secondDate[4] - birthDate[4]) | ||
elseif birthDate[2] > | elseif birthDate[2] > secondDate[2] then | ||
age = math.floor(secondDate[ | age = math.floor(secondDate[4] - birthDate[4] - 1) | ||
end | end | ||
elseif birthDate[ | elseif birthDate[5] == "month-day-year" or secondDate == "month-day-year" then | ||
if birthDate[2] < secondDate[2] then | if birthDate[2] < secondDate[2] then | ||
age = math.floor(secondDate[ | age = math.floor(secondDate[4] - birthDate[4]) | ||
elseif birthDate[2] | elseif birthDate[2] == secondDate[2] then | ||
if | if birtDate[3] <= secondDate[3] then | ||
age = math.floor(secondDate[ | age = math.floor(secondDate[4] - birthDate[4]) | ||
elseif | elseif birtDate[3] > secondDate[3] then | ||
age = math.floor(secondDate[ | age = math.floor(secondDate[4] - birthDate[4] - 1) | ||
end | end | ||
elseif birthDate[2] > secondDate[2] then | |||
age = math.floor(secondDate[4] - birthDate[4] - 1) | |||
end | end | ||
end | end |
Revision as of 12:08, 29 May 2022
Documentation for this module may be created at Module:Date Stuff/doc
local p = {}
function p.date(frame)
local orgiArgs = frame.args
local args = {}
for k, v in pairs(orgiArgs) do
v = v:match('^%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
return p._date(args)
end
function p.age(frame)
local orgiArgs = frame.args
local args = {}
if orgiArgs[1] == "{{{1}}}" or orgiArgs[2] == "{{{2}}}" or orgiArgs[4] == "{{{3}}}" then
return error("Missing a parameter")
end
for k, v in pairs(orgiArgs) do
v = v:match('^%s*(.-)%s*$')
if v ~= '' then
args[k] = v
end
end
return p._age(args)
end
--Splits a string based on specified delimiter
function Split(s, delimiter)
result = {};
for match in (s..delimiter):gmatch("(.-)"..delimiter) do
table.insert(result, match);
end
return result;
end
--Takes a short form date and converts it to a long form, and marks if it is only a year, or a month and year
function convertShortDate(date, calendar)
--In place of a long if elseif, I've put all the months into a single variable, with each in its numerical place.
--This also allows us to add other calendars here
if calendar == "Solandria" then
monthNames = {"Miry", "Ozdary", "Rydust", "Iptust", "Tosey", "Iverly", "Eohe", "Kasgust", "Andust", "Bepry", "Nindust", "Tymust", "Ekage", "Aphly", "Asust", "Syranust"}
elseif calendar == "Eberron" then
monthNames = {"Zarantyr", "Olarune", "Therendor", "Eyre", "Dravago", "Nymm", "Lharvion", "Barrakas", "Rhann", "Sypheros", "Aryth", "Vult"}
elseif calendar == "Forgotten Realms" then
monthNames = {"Hammer", "Alturiak", "Ches", "Tarsakh", "Mirtul", "Kythorn", "Flamerule", "Eleasis", "Eleint", "Marpenoth", "Uktar", "Nightal"}
elseif calendar == "IRL" then
monthNames = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
end
--Takes the provided variable and seperates it out into a table
--Also marks if it is only a year or a month and year
date = Split(date, "-")
if date[2] == nil then
date = {nil, nil, nil, tonumber(date[1]), "year"}
elseif date[3] == nil then
date = {monthNames[tonumber(date[1])], tonumber(date[1]), nil, tonumber(date[2]), "month-year"}
else
date = {monthNames[tonumber(date[1])], tonumber(date[1]), tonumber(date[2]), tonumber(date[3]), "month-day-year"}
end
return date
end
--Writes that date based on what is given.
function writeDate(date)
if date[5] == "year" then
return date[4]
elseif date[5] == "month-year" then
return date[1].." ("..date[2]..") "..date[4]
elseif date[4] == "month-day-year" then
return date[1].." ("..date[2]..") "..date[3]..", "..date[4]
end
end
--Converts a short from date to a long from date using the specified calendar
function p._date(args)
return writeDate(convertShortDate(args[1], args[2]))
end
--Calculates a persons age using the specified calendar
function p._age(args)
birthDate = args[1]
secondDate = args[2]
status = args[3]
calendar = args[4]
--checks if birthDate is not nil, and if it is returns the second date
if birthDate ~= nil then
birthDate = convertShortDate(birthDate, calendar)
secondDate = convertShortDate(secondDate, calendar)
elseif birthDate == nil then
return writeDate(convertShortDate(secondDate))
end
--Checks which math formula to use, based on how much of the date there is.
if birthDate[5] == "year" or secondDate == "year" then
age = math.floor(secondDate[4] - birthDate[4])
elseif birthDate[5] == "month-year" or secondDate == "month-year" then
if birthDate[2] <= secondDate[2] then
age = math.floor(secondDate[4] - birthDate[4])
elseif birthDate[2] > secondDate[2] then
age = math.floor(secondDate[4] - birthDate[4] - 1)
end
elseif birthDate[5] == "month-day-year" or secondDate == "month-day-year" then
if birthDate[2] < secondDate[2] then
age = math.floor(secondDate[4] - birthDate[4])
elseif birthDate[2] == secondDate[2] then
if birtDate[3] <= secondDate[3] then
age = math.floor(secondDate[4] - birthDate[4])
elseif birtDate[3] > secondDate[3] then
age = math.floor(secondDate[4] - birthDate[4] - 1)
end
elseif birthDate[2] > secondDate[2] then
age = math.floor(secondDate[4] - birthDate[4] - 1)
end
end
--Determines if age or aged is required
if status == "alive" then
return writeDate(birthDate).." (age "..age..")"
elseif status == "dead" then
return writeDate(secondDate).." (aged "..age..")"
end
end
return p