Module:Date Stuff

From Stiles Wiki
Revision as of 15:38, 5 July 2021 by Maxwells (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:Date Stuff/doc

local p = {}
local args = {}

--Takes a string and splits it based on a specified delimiter
function Split(s, delimiter)
  result = {};
  for match in (s..delimiter):gmatch("(.-)"..delimiter) do
    table.insert(result, match);
  end
  return result;
end

function convertDate(date)
  --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"}
  --Splits the date
  date = Split(date, "-")
  --Saves the month part of the variable into a seperate month variable
  month = tonumber(date[1])
  
  --Takes the month variable and converts it to its text counterpart
  month = monthNames[month]
  
  --Checks to see if the month is a valid number
  if month == nil then
    return
  end
  
  --Takes the text month variable and adds it to the day and year variable with a comma
  date = month.." "..date[2]..", "..date[3]
  
  return date
end
  

--Takes a short date and converts it to a long date (12-18-392 to Tymust 18, 392)
function p.solandria(frame)
  --Sets date variable to the first frame argument
  date = frame.args[1]

  --Calls the convert date function to convert short date to long date
  date = convertDate(date)

  return date
end


--Calculates an age based on two given dates
function p.solandriaBirthDateAge(frame)
  --Sets the date variables based on passed through arguments
  birthDate = frame.args[1]
  currentDate = frame.args[2]
  status = frame.args[3]


  --Converts the given dates to long dates
  convertedBirthDate = convertDate(birthDate)
  convertedCurrentDate = convertDate(currentDate)

  --Splits the first date given
  birthDate = Split(birthDate, "-")
  --Saves the parts of the first date given into seperate month day year variables
  birthMonth = birthDate[1]
  birthDay = birthDate[2]
  birthYear = birthDate[3]

  --Splits the second date given
  currentDate = Split(currentDate, "-")
  --Saves the parts of the second date given into seperate month day year variables
  currentMonth = currentDate[1]
  currentDay = currentDate[2]
  currentYear = currentDate[3]

  --Compares the second and first date month to determine which age calculation to use
  if currentMonth > birthMonth then
    age = math.floor(currentYear - birthYear)
  elseif currentMonth == birthMonth then
    if currentDay >= birthDay then
      age = math.floor(currentYear - birthYear)
    elseif currentDay < birthDay then
      age = math.floor(currentYear - birthYear - 1)
    else
      error("Something went wrong")
    end
  elseif currentMonth < birthMonth then
    age = math.floor(currentYear - birthYear - 1)
  else
    error("Something went wrong")
  end

  --Takes the first date and adds the age to the end
  if status == "alive" then
    dateAge = convertedBirthDate.." (age "..age..")"
  elseif status == "dead" then
    dateAge = convertedCurrentDate.." (aged "..age..")"
  else
    error("Invalid Status")
  end
  return dateAge

end

return p