Module:CalculateMureDate

-- @module Module:MureData --   This module is for use with a variety of Mure-related --   templates. It is used to calculate dates accounting for Mure's --   unique rotational & orbital periods, among other such uses. -- @see Template:MR (MureDate) -- @author User:Sebolto -- -- local obj = {}

-- Computational pseudo-constants (not actual protected consts) Math = { H_STND_Y = 8832,         -- Hours per Galactic Standard Year H_MURE_Y = 38088.11137,  -- Hours per Mure year D_MURE_Y = 2116.006187,  -- Days in Mure year D_MURE_LY = 264.5007734  -- Days in Mure Little-year }

-- @function calculateDate -- @param frame -- @return string (formatted Mure date) function obj.calculateDate(frame) -- Declarations local index, array, label, elapsedYears, elapsedDays, mureYearRaw, mureYear, mureLittleYear, percentageOfYear -- Initial definitions index = 0; array = {} -- For loops splits input at whitespace, trimming it and moving -- it into the array object. Index is incremented at each pass-thru for component in string.gmatch(frame.args[1], "[^ ]+") do       component = component:gsub("^%s*", "") table.insert(array, component) index = index + 1 end -- Date input should not exceed two parts (ex. 25,000 BBY) if index > 3 then return end -- Replace comma with empty string (25,000 -> 25000) array[1] = string.gsub(array[1], "[,]", "") -- Calculations differ depending on GSD moving forward or back if array[2] == "BBY" then elapsedYears = 25200 - tonumber(array[1]) elseif array[2] == "ABY" then elapsedYears = 25200 + tonumber(array[1]) end -- If the result is negative, date occurred prior to Uprising, -- and thus gets the KNK lable. Otherwise, it happens after the -- Uprising and gets KPK if elapsedYears < 0 then label = "KNK" elapsedYears = math.abs(elapsedYears) elseif elapsedYears >= 0 then label = "KPK" end

-- mureYearRaw is simple multiplication + division with floats. -- mureYear is a rounded approximation (int division) -- percentageOfYear is the remainder (can't use modulo here) mureYearRaw = (elapsedYears * Math.H_STND_Y) / Math.H_MURE_Y mureYear = math.floor(mureYearRaw) percentageOfYear = mureYearRaw - mureYear

-- elapsedDays is the number of estimated days that have passed in   -- the current year so far (the remainder), a value mureLittleYear -- makes use of to determine which of the eight "little years" it is. elapsedDays = percentageOfYear * Math.D_MURE_Y mureLittleYear = math.floor(elapsedDays / Math.D_MURE_LY) + 1 -- format ex: 1:0 KPK -> 25,200 BBY return mureLittleYear .. ":" .. mureYear .. " " .. label end

return obj