Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
You must create an account or log in to edit.

Module:Keppel date: Difference between revisions

From Amaranth Legacy, available at amaranth-legacy.community
Content deleted Content added
No edit summary
Tags: Manual revert Reverted
No edit summary
Tag: Reverted
Line 131: Line 131:


-- stretch day of year to the Keppel year
-- stretch day of year to the Keppel year
keppelYday = (keppelYday - 1) / date.yearLength * keppelYearLength % keppelYearLength + 1
keppelYday = (keppelYday / date.yearLength - 1) * keppelYearLength % keppelYearLength + 1


-- get the lythryd
-- get the lythryd

Revision as of 18:39, March 20, 2026

Converts the current (or provided) date to a Keppel date. Used by Template:Keppel date.

Formula

\begin{aligned} y_\oplus &= \text{Earth year} \\ d_\oplus &= \text{Earth day of year (fractional)} \\ d_{\oplus_\text{max}}(y_\oplus) &= \begin{cases} 366 & (y_\oplus = 0 \pmod{4} \text{ and } y_\oplus \neq 0 \pmod{100}) \text{ or } y_\oplus = 0 \pmod{400} \\ 365 & \text{otherwise} \end{cases} \\ \text{offset}(y_\oplus) &= d_{\oplus_\text{max}}(y_\oplus) - 287 \\ d_{\text{K}_\text{max}}(y_\text{K}) &= \begin{cases} 391 & y_\text{K} = 65 \pmod{239} \text{ and } y_\text{K} = 2 \pmod{4} \\ 390 & y_\text{K} = 65 \pmod{239} \text{ or } y_\text{K} = 2 \pmod{4} \\ 389 & \text{otherwise} \end{cases} \\ y_\text{K}(y_\oplus) &= y_\oplus - 892 \\ y_\text{K} &= \begin{cases} y_\text{K}(y_\oplus) & d_\oplus - \text{offset}(y_\oplus) \geq \frac{d_{\oplus_\text{max}}(y_\oplus)}{d_{\text{K}_\text{max}}(y_\text{K}(y_\oplus))} \\ y_\text{K}(y_\oplus) - 1 & d_\oplus - \text{offset}(y_\oplus) < \frac{d_{\oplus_\text{max}}(y_\oplus)}{d_{\text{K}_\text{max}}(y_\text{K}(y_\oplus))} \end{cases} \\ d_\text{K} &= (\frac{d_\oplus - \text{offset}(y_\oplus)}{d_{\oplus_\text{max}}(y_\oplus)} \times d_{\text{K}_\text{max}}(y_\text{K}) - 1) \bmod{d_{\text{K}_\text{max}}(y_\text{K})} + 1 \\ L_\text{K} &= \lfloor \frac{d_\text{K} - 1}{32} \rfloor + 1 \\ d_{\text{K}_L} &= \lfloor (d_\text{K} - 1) \bmod{32} \rfloor + 1 \end{aligned}


local p = {}

-- libraries
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')

-- calculations
--- Earth
local function isEarthLeapYear(year)
	-- every 4 years
	-- except for every 100 years
	-- except for every 400 years
	return (year % 400 == 0) or (year % 100 ~= 0 and year % 4 == 0)
end

local function getEarthYearLength(year)
	-- if it's a leap year, add one day
	return 365 + (isEarthLeapYear(year) and 1 or 0)
end

--- Keppel
local function isKeppelLeapYearCycle1(year)
	-- every 4 years (offset 2)
	return year % 4 == 2
end

local function isKeppelLeapYearCycle2(year)
	-- every 239 years (offset 65)
	return year % 239 == 65
end

local function getKeppelYearLength(year)
	-- if it's a leap year (cycle 1), add one day
	-- if it's a leap year (cycle 2), add one day
	return 389 + (isKeppelLeapYearCycle1(year) and 1 or 0) + (isKeppelLeapYearCycle2(year) and 1 or 0)
end

local function getKeppelYearOffset(earthYear)
	-- if it's an Earth leap year, add one day for February 29
	-- March 20 (day 78 or 79) is Liliyogh 1 (day 1)
	return 78 + (isEarthLeapYear(earthYear) and 1 or 0)
end

local function getKeppelYear(earthYear)
	-- offset of 892 years
	return earthYear - 892
end

local function getLythryd(yday)
	-- length is 32 days
	return math.floor((yday - 1) / 32) + 1
end

local function getKeppelDay(yday)
	-- see previous function
	return math.floor(yday - 1) % 32 + 1
end

-- data
--- lythryds
local lythryds = {
	'Liliyogh',
	'Avonslau',
	'Bovvilag',
	'Haelaidhe',
	'Gildorne',
	'Auvhlaus',
	'Ludervinn',
	'Glausonn',
	'Yahataidh',
	'Serelakkan',
	'Frosma',
	'Tegeirukk',
	'Intercal'
}

--- ochtdays
local ochtdays = {
	'Astresday',
	'Solsday',
	'Embresday',
	'Jadesday',
	'Ryvysday',
	'Tyrrhsday',
	'Lysisday',
	'Octresday'
}

--- sun signs (see [[Seventeen Suns]])
local sun_signs = {
	'[[File:Keppel-astrology-Alcaernon.png|32px|alt=]] Alcaernon',
	'[[File:Keppel-astrology-Eumrakh.png|32px|alt=]] Eumrakh',
	'[[File:Keppel-astrology-Aureolin.png|32px|alt=]] Aureolin',
	'[[File:Keppel-astrology-Pereptia.png|32px|alt=]] Pereptia',
	'[[File:Keppel-astrology-Sechmas.png|32px|alt=]] Sechmas',
	'[[File:Keppel-astrology-Mananakus.png|32px|alt=]] Mananakus',
	'[[File:Keppel-astrology-Luxor.png|32px|alt=]] Luxor',
	'[[File:Keppel-astrology-Illyre.png|32px|alt=]] Illyre',
	'[[File:Keppel-astrology-Glauryuz.png|32px|alt=]] Glauryuz',
	'[[File:Keppel-astrology-Schismata.png|32px|alt=]] Schismata',
	'[[File:Keppel-astrology-Annrava.png|32px|alt=]] Annrava',
	'[[File:Keppel-astrology-Yahasain.png|32px|alt=]] Yahasain',
	'[[File:Keppel-astrology-Regalion.png|32px|alt=]] Regalion',
	'[[File:Keppel-astrology-Vaalgrota.png|32px|alt=]] Vaalgrota',
	'[[File:Keppel-astrology-Cancrus.png|32px|alt=]] Cancrus',
	'[[File:Keppel-astrology-Eremoor.png|32px|alt=]] Eremoor',
	'[[File:Keppel-astrology-Haerox.png|32px|alt=]] Haerox'
}

-- exported functions
function p.convert(time)
	-- extract from time
	local date = os.date('*t', time)
	-- add the fractional part of the day of year
	date.yday = date.yday + (date.hour / 24) + (date.min / (24 * 60)) + (date.sec / (24 * 60 * 60))
	-- get the year length
	date.yearLength = getEarthYearLength(date.year)

	-- convert years to Keppel years
	local keppelYear = getKeppelYear(date.year)
	-- get the Keppel year length
	local keppelYearLength = getKeppelYearLength(date.year)
	-- convert day of year to Keppel day of year
	local keppelYday = date.yday - getKeppelYearOffset(date.year)

	-- if day of year is before the start, use the previous year
	if keppelYday < date.yearLength / keppelYearLength then
		keppelYear = keppelYear - 1
		keppelYearLength = getKeppelYearLength(keppelYearLength)
	end

	-- stretch day of year to the Keppel year
	keppelYday = (keppelYday / date.yearLength - 1) * keppelYearLength % keppelYearLength + 1

	-- get the lythryd
	local lythryd = getLythryd(keppelYday)
	-- get the Keppel day
	local keppelDay = getKeppelDay(keppelYday)
	-- get the Keppel time
	local keppelYdayFractional = select(2, math.modf(keppelYday))
	local keppelHour = select(1, math.modf(keppelYdayFractional * 24))
	local keppelMin = select(1, math.modf(keppelYdayFractional * 24 * 60)) % 60
	local keppelSec = select(1, math.modf(keppelYdayFractional * 24 * 60 * 60)) % 60

	return {
		year = keppelYear,
		yday = keppelYday,
		lythryd = lythryd,
		day = keppelDay,
		hour = keppelHour,
		min = keppelMin,
		sec = keppelSec
	}
end

function p.invoke(frame)
	local args = getArgs(frame)

	-- construct time from args
	local time
	if args['year'] and args['month'] and args['day'] then
		time = os.time{
			year = args['year'],
			month = args['month'],
			day = args['day'],
			hour = args['hour'] or 0,
			min = args['min'] or 0,
			sec = args['sec'] or 0
		}
	elseif args['time'] then
		time = args['time']
	elseif args[1] then
		time = args[1]
	else
		time = os.time()
	end

	return p.convert(time)
end

function p.main(frame)
	local args = getArgs(frame)

	-- get result
	local result = p.invoke(frame)
	-- format result year
	result.year = result.year <= 0 and (math.abs(result.year) + 1) .. ' AT' or result.year .. ' LE'

	if yesno(args['year_only'] or false) then
		return year
	elseif yesno(args['month_day_only'] or false) then
		return string.format(
			'%s %d',
			lythryds[result.lythryd],
			result.day
		)
	else
		local formatted = string.format(
			'%s %d, %s',
			lythryds[result.lythryd],
			result.day,
			result.year
		)

		if yesno(args['show_ochtday'] or false) then
			formatted = string.format(
				'%s, %s',
				ochtdays[(result.day - 1) % 8 + 1],
				formatted
			)
		end

		if yesno(args['show_time'] or false) then
			formatted = string.format(
				'%s %02d:%02d:%02d',
				formatted,
				result.hour,
				result.min,
				result.sec
			)
		end

		if yesno(args['show_sun_sign'] or false) then
			formatted = string.format(
				'%s (%s)',
				formatted,
				sun_signs[math.floor((result.yday - 1) / 24) + 1]
			)
		end

		return formatted
	end
end

return p