Module:Date Stuff: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local args = {}


--Takes a string and splits it based on a specified delimiter
--Splits a string based on specified delimiter
function Split(s, delimiter)
function Split(s, delimiter)
   result = {};
   result = {};
Line 11: Line 10:
end
end


function convertDate(date)
--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
   --Splits the date
  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, "-")
  --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
   if date[2] == nil then
  month = monthNames[month]
    date = {nil, nil, date[1], 1, nil}
    
   elseif date[3] == nil then
  --Checks to see if the month is a valid number
     date = {monthNames[tonumber(date[1])], nil, date[2], nil, 1}
  if month == nil then
  else
     return
    date = {monthNames[tonumber(date[1])], date[2], date[3], nil, nil}
   end
   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
   return date
end
end
 


--Takes a short date and converts it to a long date (12-18-392 to Tymust 18, 392)
--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)
  --Sets date variable to the first frame argument
   date = convertShortDate(frame.args[1], "solandria")
   date = frame.args[1]
 
  return writeDate(date)


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




--Calculates an age based on two given dates
--Calculates a persons age based on which calendar to use.
function p.solandriaBirthDateAge(frame)
function p.age(frame)
   --Sets the date variables based on passed through arguments
   --Takes our varibales from the frame and names them
   birthDate = frame.args[1]
   firstDate = frame.args[1]
   currentDate = frame.args[2]
   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
   --Converts the given dates to long dates
   if firstDate ~= nil then
   convertedBirthDate = convertDate(birthDate)
    firstDate = convertShortDate(firstDate, calendar)
  convertedCurrentDate = convertDate(currentDate)
    secondDate = convertShortDate(secondDate, calendar)
 
   elseif firstDate == nil then
  --Splits the first date given
    return writeDate(convertShortDate(secondDate))
  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]
 
  if birthYear == nil then
  dateAge = convertedCurrentDate
   end
   end
 
 
   --Compares the second and first date month to determine which age calculation to use
   --Checks which math formula to use, based on how much of the date there is.
   if currentMonth > birthMonth then
   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(currentYear - birthYear)
     age = math.floor(secondDate[3] - firstDate[3])
  elseif currentMonth == birthMonth then
     age = age - 1 .." - ".. age
     if currentDay >= birthDay then
   elseif firstDate[2] >= secondDate[2] then
      age = math.floor(currentYear - birthYear)
     age = math.floor(secondDate[3] - firstDate[3] - 1)
     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
   end
 
 
   --Takes the first date and adds the age to the end
   --Determins if age or aged is required
   if status == "alive" then
   if status == "alive" then
     dateAge = convertedBirthDate.." (age "..age..")"
     return writeDate(firstDate).." (age "..age..")"
   elseif status == "dead" then
   elseif status == "dead" then
     dateAge = convertedCurrentDate.." (aged "..age..")"
     return writeDate(secondDate).." (aged "..age..")"
  else
    error("Invalid Status")
   end
   end
  return dateAge
end
end


return p
return p