Module:Date Stuff: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- | --Splits a string based on specified delimiter | ||
function Split(s, delimiter) | function Split(s, delimiter) | ||
result = {}; | result = {}; | ||
Line 11: | Line 10: | ||
end | end | ||
function | --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. | --In place of a long if elseif, I've put all the months into a single variable, with each in its numerical place. | ||
monthNames = {"Miry", "Ozdary", "Rydust", "Iptust", "Tosey", "Iverly", "Eohe", "Kasgust", "Andust", "Bepry", "Nindust", "Tymust", "Ekage", "Aphly", "Asust", "Syranust"} | --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"} | |||
end | |||
--Takes the provided variable and seperates it out into a table | |||
--Also makrs if it is only a year or a montha nd year | |||
date = Split(date, "-") | date = Split(date, "-") | ||
if date[2] == nil then | |||
date = {nil, nil, date[1], 1, nil} | |||
elseif date[3] == nil then | |||
date = {monthNames[tonumber(date[1])], nil, date[2], nil, 1} | |||
else | |||
date = {monthNames[tonumber(date[1])], date[2], date[3], nil, nil} | |||
end | end | ||
return date | return date | ||
end | end | ||
-- | --Writes that date based on what is given. | ||
function writeDate(date) | |||
if date[4] == 1 then | |||
return date[3] | |||
elseif date[5] == 1 then | |||
return date[1].." "..date[3] | |||
else | |||
return date[1].." "..date[2]..", "..date[3] | |||
end | |||
end | |||
--Converts a Solandria short from dat to a Solandira long from date | |||
function p.solandria(frame) | function p.solandria(frame) | ||
date = convertShortDate(frame.args[1], "solandria") | |||
date = frame.args[1] | |||
return writeDate(date) | |||
end | end | ||
--Calculates | --Calculates a persons age based on which calendar to use. | ||
function p. | function p.age(frame) | ||
-- | --Takes our varibales from the frame and names them | ||
firstDate = frame.args[1] | |||
secondDate = frame.args[2] | |||
status = frame.args[3] | status = frame.args[3] | ||
calendar = frame.args[4] | |||
--checks if firstDate is not nil, and if it is returns the second date | |||
-- | if firstDate ~= nil then | ||
firstDate = convertShortDate(firstDate, calendar) | |||
secondDate = convertShortDate(secondDate, calendar) | |||
elseif firstDate == nil then | |||
return writeDate(convertShortDate(secondDate)) | |||
end | end | ||
-- | --Checks which math formula to use, based on how much of the date there is. | ||
if | if firstDate[4] == 1 or secondDate[4] == 1 or firstDate[5] == 1 or secondDate[5] == 1 or firstDate[2] <= secondDate[2] then | ||
age = math.floor(secondDate[3] - firstDate[3]) | |||
age = age - 1 .." - ".. age | |||
elseif firstDate[2] >= secondDate[2] then | |||
age = math.floor(secondDate[3] - firstDate[3] - 1) | |||
elseif | |||
age = math.floor( | |||
end | end | ||
-- | --Determins if age or aged is required | ||
if status == "alive" then | if status == "alive" then | ||
return writeDate(firstDate).." (age "..age..")" | |||
elseif status == "dead" then | elseif status == "dead" then | ||
return writeDate(secondDate).." (aged "..age..")" | |||
end | end | ||
end | end | ||
return p | return p |
Revision as of 18:28, 5 July 2021
Documentation for this module may be created at Module:Date Stuff/doc
local p = {}
--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"}
end
--Takes the provided variable and seperates it out into a table
--Also makrs if it is only a year or a montha nd year
date = Split(date, "-")
if date[2] == nil then
date = {nil, nil, date[1], 1, nil}
elseif date[3] == nil then
date = {monthNames[tonumber(date[1])], nil, date[2], nil, 1}
else
date = {monthNames[tonumber(date[1])], date[2], date[3], nil, nil}
end
return date
end
--Writes that date based on what is given.
function writeDate(date)
if date[4] == 1 then
return date[3]
elseif date[5] == 1 then
return date[1].." "..date[3]
else
return date[1].." "..date[2]..", "..date[3]
end
end
--Converts a Solandria short from dat to a Solandira long from date
function p.solandria(frame)
date = convertShortDate(frame.args[1], "solandria")
return writeDate(date)
end
--Calculates a persons age based on which calendar to use.
function p.age(frame)
--Takes our varibales from the frame and names them
firstDate = frame.args[1]
secondDate = frame.args[2]
status = frame.args[3]
calendar = frame.args[4]
--checks if firstDate is not nil, and if it is returns the second date
if firstDate ~= nil then
firstDate = convertShortDate(firstDate, calendar)
secondDate = convertShortDate(secondDate, calendar)
elseif firstDate == nil then
return writeDate(convertShortDate(secondDate))
end
--Checks which math formula to use, based on how much of the date there is.
if firstDate[4] == 1 or secondDate[4] == 1 or firstDate[5] == 1 or secondDate[5] == 1 or firstDate[2] <= secondDate[2] then
age = math.floor(secondDate[3] - firstDate[3])
age = age - 1 .." - ".. age
elseif firstDate[2] >= secondDate[2] then
age = math.floor(secondDate[3] - firstDate[3] - 1)
end
--Determins if age or aged is required
if status == "alive" then
return writeDate(firstDate).." (age "..age..")"
elseif status == "dead" then
return writeDate(secondDate).." (aged "..age..")"
end
end
return p