Module:Calendar/shaacamthanj: Difference between revisions

From Nguhcraft Wiki
Jump to navigation Jump to search
m Annwan moved page Module:Calendar/śácamþań to Module:Calendar/shaacamthanj without leaving a redirect: Lua doesn't like non-ascii
No edit summary
Line 1: Line 1:
local calendar = {}
local calendar = {}


local epoch_date = 0 -- TO FIX, datestamp for Casting Rains  
local epoch_date = -33855 -- TO FIX, datestamp for Casting Rains, Quiet, Day 1, 1


local subdigits = {[0] = '', '', '','','','','','',}
local subdigits = {[0] = '', '', '','','','','',''}
local natdigits = {[0] = '', '', '', '', '', ''}
local natdigits = {[0] = '', '', '', '', '', ''}
local subseparators = {'', '', '', '󰴂'}
local subseparators = {'', '', '', '󰴂'}
Line 9: Line 9:


local daynames = {
local daynames = {
eng = {'1', '2', '3', '4', '5', '6', '7', '8'},
eng = {'1', '2', '3', '4', '5', '6', '7', '8'},
scs = {'', '', '', '', '', '', '', ''}
scs = {'', '', '', '', '', '', '', ''}
}
}


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


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


Line 56: Line 56:
end
end
return '' .. table.concat(digits)
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 date = epoch_date
while year < tyear do
        date = date + year_length(year)
        year = year + 1
end
while month < tmonth do
date = date + month_length(year, month)
month = month + 1
end
date = date + (tweek - 1) * 8
date = date + tday - 1
return date
end
calendar.to = function (datestamp)
local date = datestamp + epoch_date
local y, m, w, d = 1, 0, 1, 1
while date >= year_length(y) do
        date = date - year_length(y)
        y = y + 1
end
while date >= month_length(y, m) do
date = date - month_length(y, m)
m = m + 1
end
while date >= 8 do
        date = date - 8
        w = w + 1
end
d = d + date
return {year = y, month = m, week = w, day = d}
end
calendar.to_formated = 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
end


return calendar
return calendar

Revision as of 14:46, 17 November 2026

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

local calendar = {}

local epoch_date = -33855 -- TO FIX, datestamp for Casting Rains, Quiet, Day 1, 1

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

local daynames = {
	eng = {'1', '2', '3', '4', '5', '6', '7', '8'},
	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 date = epoch_date
	while year < tyear do
        	date = date + year_length(year)
        	year = year + 1
	end
	while month < tmonth do
		date = date + month_length(year, month)
		month = month + 1
	end
	date = date + (tweek - 1) * 8
	date = date + tday - 1
	return date
end

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

calendar.to_formated = 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