Module:Date Stuff: Difference between revisions

From Stiles Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Manual revert
 
(86 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {};    --All lua modules on Wikipedia must begin by defining a variable
local p = {}
                    --that will hold their externally accessible functions.
 
                    --Such variables can have whatever name you want and may
function p.date(frame)
                    --also contain various data as well as functions.
local orgiArgs = frame.args
p.hello = function( frame )     --Add a function to "p".
local args = {}
                                        --Such functions are callable in Wikipedia
for k, v in pairs(orgiArgs) do
                                        --via the #invoke command.
v = v:match('^%s*(.-)%s*$')
                                        --"frame" will contain the data that Wikipedia
if v ~= '' then
                                        --sends this function when it runs.
args[k] = v
                                -- 'Hello' is a name of your choice. The same name needs to be referred to when the module is used.
end
   
end
    local str = "Hello World!"  --Declare a local variable and set it equal to
return p._date(args)
                                --"Hello World!". 
end
   
 
    return str    --This tells us to quit this function and send the information in
function p.age(frame)
                  --"str" back to Wikipedia.
local orgiArgs = frame.args
   
local args = {}
end  -- end of the function "hello"
if orgiArgs[1] == "{{{1}}}" or orgiArgs[2] == "{{{2}}}" or orgiArgs[4] == "{{{3}}}" then
function p.hello_to(frame) -- Add another function
return error("Missing a parameter")
local name = frame.args[1] -- To access arguments passed to a module, use `frame.args`
end
    -- `frame.args[1]` refers to the first unnamed parameter
for k, v in pairs(orgiArgs) do
    -- given to the module
v = v:match('^%s*(.-)%s*$')
return "Hello, " .. name .. "!"  -- `..` concatenates strings. This will return a customized
if v ~= '' then
-- greeting depending on the name given, such as "Hello, Fred!"
args[k] = v
end
end
return p._age(args)
end
end
function p.count_fruit(frame)
 
local num_bananas = frame.args.bananas -- Named arguments ({{#invoke:Example|count_fruit|foo=bar}}) are likewise
--Splits a string based on specified delimiter
local num_apples = frame.args.apples  -- accessed by indexing `frame.args` by name (`frame.args["bananas"]`, or)
function Split(s, delimiter)
  -- equivalently `frame.args.bananas`.
  result = {};
return 'I have ' .. num_bananas .. ' bananas and ' .. num_apples .. ' apples'
  for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  -- Like above, concatenate a bunch of strings together to produce
    table.insert(result, match);
  -- a sentence based on the arguments given.
  end
  return result;
end
end


return p    --All modules end by returning the variable containing their functions to Wikipedia.
--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
-- Now we can use this module by calling {{#invoke: Example | hello }},
function convertShortDate(shortDate, calendar)
-- {{#invoke: Example | hello_to | foo }}, or {{#invoke:Example|count_fruit|bananas=5|apples=6}}
  --In place of a long if elseif, I've put all the months into a single variable, with each in its numerical place.
-- Note that the first part of the invoke is the name of the Module's wikipage,
  --This also allows us to add other calendars here
-- and the second part is the name of one of the functions attached to the
  if calendar == "Solandria" then
-- variable that you returned.
    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
  local shortDate = Split(shortDate, "-")
 
  if shortDate[2] == nil then
    dateTable = {nil, nil, nil, tonumber(shortDate[1]), "year"}
  elseif shortDate[3] == nil then
    dateTable = {monthNames[tonumber(shortDate[1])], tonumber(shortDate[1]), nil, tonumber(shortDate[2]), "month-year"}
  else
    dateTable = {monthNames[tonumber(shortDate[1])], tonumber(shortDate[1]), tonumber(shortDate[2]), tonumber(shortDate[3]), "month-day-year"}
  end
  return dateTable
end
 
--Writes that date based on what is given.
function writeDate(dateTable)
  if dateTable[5] == "year" then
    return dateTable[4]
  elseif dateTable[5] == "month-year" then
    return dateTable[1].." ("..dateTable[2]..") "..dateTable[4]
  elseif dateTable[5] == "month-day-year" then
    return dateTable[1].." ("..dateTable[2]..") "..dateTable[3]..", "..dateTable[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)
  local birthDate = convertShortDate(args[1], args[4])
  local secondDate = convertShortDate(args[2], args[4])
  local status = args[3]
 
  --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 birthDate[3] <= secondDate[3] then
        age = math.floor(secondDate[4] - birthDate[4])
      elseif birthDate[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
    local longDate = writeDate(birthDate)
    return longDate.." (age "..age..")"
  elseif status == "dead" then
    return writeDate(secondDate).." (aged "..age..")"
  end
end


-- The "print" function is not allowed in Wikipedia.  All output is accomplished
return p
-- via strings "returned" to Wikipedia.

Latest revision as of 21:04, 31 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(shortDate, 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
  local shortDate = Split(shortDate, "-")
  
  if shortDate[2] == nil then
    dateTable = {nil, nil, nil, tonumber(shortDate[1]), "year"}
  elseif shortDate[3] == nil then
    dateTable = {monthNames[tonumber(shortDate[1])], tonumber(shortDate[1]), nil, tonumber(shortDate[2]), "month-year"}
  else
    dateTable = {monthNames[tonumber(shortDate[1])], tonumber(shortDate[1]), tonumber(shortDate[2]), tonumber(shortDate[3]), "month-day-year"}
  end
  return dateTable
end

--Writes that date based on what is given.
function writeDate(dateTable)
  if dateTable[5] == "year" then
    return dateTable[4]
  elseif dateTable[5] == "month-year" then
    return dateTable[1].." ("..dateTable[2]..") "..dateTable[4]
  elseif dateTable[5] == "month-day-year" then
    return dateTable[1].." ("..dateTable[2]..") "..dateTable[3]..", "..dateTable[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)
  local birthDate = convertShortDate(args[1], args[4])
  local secondDate = convertShortDate(args[2], args[4])
  local status = args[3]
  
  --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 birthDate[3] <= secondDate[3] then
        age = math.floor(secondDate[4] - birthDate[4])
      elseif birthDate[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
    local longDate = writeDate(birthDate)
    return longDate.." (age "..age..")"
  elseif status == "dead" then
    return writeDate(secondDate).." (aged "..age..")"
  end
end

return p