Module:Years

-- This module loads a big hashtable for translating a Dalereckoning year -- to the name of the year as given to us by Augathra the Mad and Alaundo. local RoY = {} --- imported libraries -- Parses invocation and template parameters, trims whitespace, and removes blanks. local getArgs = require('Dev:Arguments').getArgs

local yearNames = mw.loadData( 'Module:Years/data' ) local offset = 701 --The offset needed to convert a calendar year to a sequence number.

function RoY._yearName( year ) if yearNames[year + offset] == nil then return '' else return yearNames[year + offset][2] end end

function RoY.yearName( frame ) local args = getArgs(frame) local year = tonumber(args[1] or 0) return RoY._yearName(year) end

function RoY.randomYear math.randomseed(mw.language.new( 'en' ):formatDate('U')) local randomYearNum = math.random(-700,1600)

return {randomYearNum, RoY._yearName(randomYearNum) or ''} end

function RoY._relatedYear( year ) local yearsSinceLeap = ((year % 4) + 4) % 4 local randomYr = RoY.randomYear[1]

local relatedYr = randomYr - (randomYr % 4) + yearsSinceLeap if relatedYr > 1600 then relatedYr = relatedYr - 4 end return {relatedYr, RoY._yearName(relatedYr) or ''} end

function RoY.RelatedYear( frame ) local args = getArgs(frame) local relatedYear = RoY._relatedYear( args[1] ) return ..relatedYear[2].. end

function RoY.yearlinkName( frame ) local args = getArgs(frame) local year = tonumber(args[1] or 0) local formattedYear = string.gsub(year,'-','−')..' DR' local output =    if RoY._yearName(year) ==  then        output = output..formattedYear..'' else output = output..RoY._yearName(year)..', '..formattedYear..']]' end return output end

function RoY.yearsByName local output = mw.html.create('table') :addClass('wikitable'):addClass('sortable') :tag('tr') :tag('th'):attr('data-sort-type','number'):attr('scope','col'):wikitext('Year'):done :tag('th'):wikitext('Name of Year'):done :done local sortable = {} for i,v in ipairs(yearNames) do       sortable[i] = {v[1], v[2]} end table.sort(sortable, function(a,b) return a[2] < b[2] end) for i,v in ipairs(sortable) do       output:tag('tr') :tag('td'):attr('data-sort-value',tonumber(v[1])) :wikitext(''..string.gsub(v[1],'-','−')..' DR') :done :tag('td'):wikitext(v[2]):done :done end output:allDone return tostring(output) end

local convertToDR = { dr = function(year) return year       end, -- Dalereckoning cr = function(year) return year +  25 end, -- Cormyr Reckoning tr = function(year) return year - 212 end, -- Tethyr Reckoning nr = function(year) return year + 1032 end, -- North Reckoning ny = function(year) return year - 3859 end, -- Netheril Year ts = function(year) return year -  70 end, -- Timesong of Serôs mc = function(year) return year - 2136 end, -- Mulhorand Calendar pr = function(year) return year + 1358 end, -- Present Reckoning sy = function(year) return year - 1250 end, -- Shou Year ky = function(year) return year -  74 end, -- Kozakuran Calendar wy = function(year) return year - 418 end, -- Wa Year uy = function(year) return year - 2550 end -- Ulutiun Calendar }

local calendarFormat = { dr = function(year) return year..' DR'            end, -- Dalereckoning cr = function(year) return year..' CR'            end, -- Cormyr Reckoning tr = function(year) return year..' TR'            end, -- Tethyr Reckoning nr = function(year) return year..' NR'            end, -- North Reckoning ny = function(year) return year..' NY'            end, -- Netheril Year ts = function(year) return year..' TS'            end, -- Timesong of Serôs mc = function(year) return year..' MC'            end, -- Mulhorand Calendar pr = function(year) return year..' PR'            end, -- Present Reckoning sy = function(year) return 'Shou Year '..year     end, -- Shou Year ky = function(year) return 'Kozakuran Year '..year end, -- Kozakuran Calendar wy = function(year) return 'Wa Year '..year       end, -- Wa Year uy = function(year) return 'Ulutiun Year '..year  end  -- Ulutiun Calendar }

function formatYear(year, cal) local yStr = tostring(year) if math.abs(year) > 9999 then yStr = string.sub(yStr, 1, string.len(yStr)-3)..','..string.sub(yStr, -3) end local repl, matches = string.gsub(yStr, '-', '−') return calendarFormat[cal](repl) or '' end

function buildLink(year, yearDR, cal) local output =    output = output..formatYear(year, cal)..' '    output = output..'('..formatYear(yearDR, 'dr')..')'.. return output end

function RoY.yearlinkConvert( frame ) local args = getArgs(frame) local year = tonumber(args[1] or 0) local convertedYear = 0 local calendar = string.lower(tostring(args[2] or 'DR')) local conversion = convertToDR[calendar] local output = ' Invalid calendar code ' if conversion ~= nil then convertedYear = conversion(year) output = buildLink(year, convertedYear, calendar) end return output end return RoY