Module:Calendar/shaacamthanj: Difference between revisions

From Nguhcraft Wiki
Jump to navigation Jump to search
No edit summary
Tag: Manual revert
No edit summary
Line 1: Line 1:
local calendar = {}
local calendar = {}


local epoch_date = -34000 -- datestamp for Casting Rains, Quiet, 1, 1
local epoch_date = -33600 -- datestamp for Casting Rains, Quiet, 1, 1


local subdigits = {[0] = '', '', '','','','','',''}
local subdigits = {[0] = '', '', '','','','','',''}

Revision as of 15:24, 17 November 2026

Documentation for this module may be created at Module:Calendar/shaacamthanj/doc

local calendar = {}

local epoch_date = -33600 -- datestamp for Casting Rains, Quiet, 1, 1

local subdigits = {[0] = '', '', '','','','','',''}
local natdigits = {[0] = '', '', '', '', '', ''}
local subseparators = {'', '', '', '󰴂'}
local natseparators = {'', '', '', ''}

local daynames = {
	scs = {'', '', '', '', '', '', '', ''}
}

local monthnames = {
	eng = {[0] = 'the Depths', 'the Casting Rains', 'Hunger', 'Hunting', 'Floods', 'Light', 'Storms', 'Forage', 'Bloods', 'Harvest', 'Drought', 'the Dry Season Solstice', 'Decay', 'Lanterns'},
	scs = {[0] = '', '', '', '', '', '', '', '', '', '', '', '', '', ''}
}

local weeknames = {
	eng = {'Quiet', 'Unlucky', 'Lucky', 'Nightly'},
	scs = {'QUIET WEEK', 'UNLUCKY WEEK', 'LUCKY WEEK', 'NIGHTLY WEEK'},
}


local function substrate_num(num)
	--- Digits in the subtrate system
	local place = 0
	local digits = { subdigits[num % 8] }
	num = math.floor(num / 8)
	while num > 0 do
		place = place + 1
		local d = num % 6
		if d ~= 0 then
			digits[#digits+1] = subseparators[place]
			if d ~= 1 then digits[#digits+1] = natdigits[d] end
		end
		num = math.floor(num / 6)
	end
	return '' .. table.concat(digits)
end

local function native_num(num)
	--- Digits in the subtrate system
	local place = 0
	local digits = { natdigits[num % 6] }
	num = math.floor(num / 6)
	while num > 0 do
		place = place + 1
		local d = num % 6
		if d ~= 0 then
			digits[#digits+1] = natseparators[place]
			if d ~= 1 then digits[#digits+1] = natdigits[d] end
		end
		num = math.floor(num / 6)
	end
	return '' .. table.concat(digits)
end

local function has_depths(year) return year % 6 == 0 end
local function has_lanterns_n8(year) return (year % 2 == 0) and not (year % 12 == 4) end

local function year_length(year)
	local len = 409
	if has_lanterns_n8(year) then len = len + 1 end
	if has_depths(year) then len = len + 32 end
	return len
end

local function month_length(year, month)
	if month == 0 and not has_depths(year) then return 0 end
	if month == 13 and has_lanterns_n8(year) then return 32 end
	return 31 + (month % 2)
end
	
calendar.from = function (date_info)
	assert(date_info and date_info.year and date_info.month and date_info.week and date_info.day)
	local tyear, tmonth, tweek, tday = tonumber(date_info.year), tonumber(date_info.month), to_number(date_info.week), to_number(date_info.day)
	assert(tyear >= 1)
	local year, month = 1, 0
	local dt = epoch_date
	while year < tyear do
        	dt = dt + year_length(year)
        	year = year + 1
	end
	while month < tmonth do
		dt = dt + month_length(year, month)
		month = month + 1
	end
	dt = dt + (tweek - 1) * 8
	dt = dt + tday - 1
	return dt
end

calendar.to = function (datestamp)
	assert(datestamp >= epoch_date)
	local dt = datestamp - epoch_date
	local y, m, w, d = 1, 0, 1, 1
	while dt >= year_length(y) do
        	dt = dt - year_length(y)
        	y = y + 1
	end
	while dt >= month_length(y, m) do
		dt = dt - month_length(y, m)
		m = m + 1
	end
	while dt >= 8 do
        	dt = dt - 8
        	w = w + 1
	end
	d = d + dt
	return {year = y, month = m, week = w, day = d}
end

calendar.to_formatted = function(datestamp, opts)
	opts = opts or {}
	opts.lang = opts.lang or "eng"
	local d = calendar.to(datestamp)
	if opts.lang == "eng" then
		return "Month of "
			.. (monthnames.eng[d.month])
			.. ", "
			.. (weeknames.eng[d.week])
			.. " Week, Day "
			.. tostring(d.day)
			.. ", "
			.. tostring(d.year)
	end
end

return calendar