Module:Date Stuff: Difference between revisions

From Stiles Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Tag: Manual revert
 
(82 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local args = {}


--Takes a string and splits it based on a specified delimiter
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)
function Split(s, delimiter)
   result = {};
   result = {};
Line 11: Line 37:
end
end


--Takes a short date and converts it to a long date (12-18-392 to Tymust 18, 392)
--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 p.solandria(frame)
function convertShortDate(shortDate, calendar)
   --Sets date variable to the first frame argument
   --In place of a long if elseif, I've put all the months into a single variable, with each in its numerical place.
   date = frame.args[1]
  --This also allows us to add other calendars here
   --Splits the date
  if calendar == "Solandria" then
   date = Split(date, "-")
    monthNames = {"Miry", "Ozdary", "Rydust", "Iptust", "Tosey", "Iverly", "Eohe", "Kasgust", "Andust", "Bepry", "Nindust", "Tymust", "Ekage", "Aphly", "Asust", "Syranust"}
   --Saves the parts of the date into month day year variables
   elseif calendar == "Eberron" then
   month = tonumber(date[1])
  monthNames = {"Zarantyr", "Olarune", "Therendor", "Eyre", "Dravago", "Nymm", "Lharvion", "Barrakas", "Rhann", "Sypheros", "Aryth", "Vult"}
  day = date[2]
   elseif calendar == "Forgotten Realms" then
  year = date[3]
  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, "-")
    
    
  --Takes the month variable and converts it to its text counterpart
   if shortDate[2] == nil then
   if month == 1 then
     dateTable = {nil, nil, nil, tonumber(shortDate[1]), "year"}
     month = "Miry"
   elseif shortDate[3] == nil then
   elseif month == 2 then
     dateTable = {monthNames[tonumber(shortDate[1])], tonumber(shortDate[1]), nil, tonumber(shortDate[2]), "month-year"}
    month = "Ozdary"
  elseif month == 3 then
    month = "Rydust"
  elseif month == 4 then
     month = "Iptust"
  elseif month == 5 then
    month = "Tosey"
  elseif month == 6 then
    month = "Iverly"
  elseif month == 7 then
    month = "Eohe"
  elseif month == 8 then
    month = "Kasgust"
  elseif month == 9 then
    month = "Andust"
  elseif month == 10 then
    month = "Bepry"
  elseif month == 11 then
    month = "Nindust"
  elseif month == 12 then
    month = "Tymust"
  elseif month == 13 then
    month = "Ekage"
  elseif month == 14 then
    month = "Aphly"
  elseif month == 15 then
    month = "Asust"
  elseif month == 16 then
    month = "Syranust"
   else
   else
     error("invalid opperation")
     dateTable = {monthNames[tonumber(shortDate[1])], tonumber(shortDate[1]), tonumber(shortDate[2]), tonumber(shortDate[3]), "month-day-year"}
   end
   end
  return dateTable
end


  --Takes the text month variable and adds it to the day and year variable with a comma
--Writes that date based on what is given.
   date = month.." "..day..", "..year
function writeDate(dateTable)
   return date
  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
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 an age based on two given dates
function p.solandriaBirthDateAge(birthDate, currentDate)
  --Converts the first given date to a long date
  convertedDate = solandria(birthDate)


  --Splits the first date given
--Calculates a persons age using the specified calendar
  birthDate = Split(birthDate, "-")
function p._age(args)
   --Saves the parts of the first date given into seperate month day year variables
   local birthDate = convertShortDate(args[1], args[4])
  birthMonth = birthDate[1]
   local secondDate = convertShortDate(args[2], args[4])
   birthDay = birthDate[2]
   local status = args[3]
   birthYear = birthDate[3]
 
 
   --Checks which math formula to use, based on how much of the date there is.
   --Splits the second date given
   if birthDate[5] == "year" or secondDate == "year" then
   currentDate = Split(currentDate, "-")
    age = math.floor(secondDate[4] - birthDate[4])
   --Saves the parts of the second date given into seperate month day year variables
   elseif birthDate[5] == "month-year" or secondDate == "month-year" then
  currentMonth = currentDate[1]
    if birthDate[2] <= secondDate[2] then
  currentDay = currentDate[2]
      age = math.floor(secondDate[4] - birthDate[4])
  currentYear = currentDate[3]
    elseif birthDate[2] > secondDate[2] then
 
      age = math.floor(secondDate[4] - birthDate[4] - 1)
   --Compares the second and first date month to determine which age calculation to use
    end
  if currentMonth > birthMonth then
   elseif birthDate[5] == "month-day-year" or secondDate == "month-day-year" then
    age = math.floor(currentYear - birthYear)
    if birthDate[2] < secondDate[2] then
  elseif currentMonth == birthMonth then
      age = math.floor(secondDate[4] - birthDate[4])
    if currentDay >= birthDay then
    elseif birthDate[2] == secondDate[2] then
      age = math.floor(currentYear - birthYear)
      if birthDate[3] <= secondDate[3] then
    elseif currentDay < birthDay then
        age = math.floor(secondDate[4] - birthDate[4])
      age = math.floor(currentYear - birthYear - 1)
      elseif birthDate[3] > secondDate[3] then
     else
        age = math.floor(secondDate[4] - birthDate[4] - 1)
       error("Something went wrong")
      end
     elseif birthDate[2] > secondDate[2] then
       age = math.floor(secondDate[4] - birthDate[4] - 1)
     end
     end
  elseif currentMonth < birthMonth then
    age = math.floor(currentYear - birthYear - 1)
  else
    error("Something went wrong")
   end
   end
 
 
   --Takes the first date and adds the age to the end
   --Determines if age or aged is required
   dateAge = convertedDate.." (age "..age..")"
   if status == "alive" then
   return dateAge
    local longDate = writeDate(birthDate)
    return longDate.." (age "..age..")"
   elseif status == "dead" then
    return writeDate(secondDate).." (aged "..age..")"
  end
end
end


return p
return p
--print(solandria("1-2-3"))
--print(solandriaBirthDateAge("12-18-392", "11-22-400"))

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