Module:Date old

From Greyhawk Wiki
Jump to navigation Jump to search

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

-- A script for Greyhawk calendar conversion.
-- Usage: {{#invoke:Date|Convert|year=6106|from=SD|to=CY}}
-- The simplest way to use this in an article is Template:Date.

local p = {}
local cal = {
  ["CY"] = { ahead=0,    name="Common Year"},
  ["OL"] = { ahead=804,  name="Olman Lunar"},
  ["OR"] = { ahead=644,  name="Oeridian Record"},
  ["TC"] = { ahead=1407, name="Touv Calendar"},
  ["FT"] = { ahead=2150, name="Flan Tracking"},
  ["BH"] = { ahead=2659, name="Baklunish Hegira"},
  ["OC"] = { ahead=4462, name="Olven Calendar"},
  ["SD"] = { ahead=5515, name="Suloise Dating"}
}

-- Convert between two Greyhawk calendars.
-- Usage: {{#invoke:Date|Convert|year=6106|from=SD|to=CY}}
function p.Convert ( frame )
  local year = tonumber(frame.args["year"]) or 1
  local fr   = frame.args["from"] or "CY"
  local to   = frame.args["to"]   or "CY"

  if year == 0 then year = 1 end -- no year zero in Greyhawk calendars

  local outdate = year + (cal[to]["ahead"]-cal[fr]["ahead"])

-- Special handling of crossing zero line due to lack of year zero
  if year <= 0 and outdate >= 0 then
    outdate = outdate + 1
  elseif year >= 0 and outdate <= 0 then
    outdate = outdate - 1
  end

  if to == "CY" then
    return ( "[[" .. outdate .. " CY]]" )
  else
    return ( outdate .. " [[" .. cal[to]["name"] .. "|" .. to .. "]]" )
  end
end

-- Add or subtract a number to a year, accounting for lack of year zero
-- Usage (adding): {{#invoke:Date|Add|year=591|delta=5}}
-- Usage (subtract): {{#invoke:Date|Add|year=591|delta=-1}}
function p.Add ( frame )
  local year = tonumber(frame.args["year"]) or 1
  local delta = tonumber(frame.args["delta"]) or 0
  local outdate = year + delta

-- Special handling of crossing zero line due to lack of year zero
  if year <= 0 and outdate >= 0 then
    outdate = outdate + 1
  elseif year >= 0 and outdate <= 0 then
    outdate = outdate - 1
  end

  return outdate
end

return p