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
No edit summary
 
(214 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


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

-- calculations
--- Earth
local function isEarthLeapYear(year)
local function isEarthLeapYear(year)
-- every 4 years
-- every 4 years
Line 9: Line 14:
end
end


local function isKeppelLeapYear1(year)
local function getEarthYearLength(year)
-- every 4 years beginning on year 2
-- if it's a leap year, add one day
return year % 4 == 2
return 365 + (isEarthLeapYear(year) and 1 or 0)
end
end


--- Keppel
local function isKeppelLeapYear2(year)
local function isKeppelLeapYearCycle1(year)
-- every 239 years beginning on year 65
-- every 4 years (offset 2)
return year % 4 == 2
end

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


local function getKeppelYearLength(year)
-- lythryd number to name map
-- 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 = {
local lythryds = {
'Liliyogh',
'Liliyogh',
Line 26: Line 65:
'Haelaidhe',
'Haelaidhe',
'Gildorne',
'Gildorne',
'Glausonn',
'Ludervinn',
'Auvhlaus',
'Auvhlaus',
'Ludervinn',
'Glausonn',
'Yahataidh',
'Yahataidh',
'Serelakkan',
'Serelakkan',
Line 36: Line 75:
}
}


--- ochtdays
function p.convertDate(date)
local ochtdays = {
-- maximum amount of days in a year (Earth)
'Astresday',
local ydayMaxEarth = isEarthLeapYear(date.year) and 366 or 365
'Solsday',
-- maximum amount of days in a year (Keppel)
'Embresday',
local ydayMax = 389
'Jadesday',
if isEarthLeapYear(date.year) then ydayMax = ydayMax + 1 end
'Ryvysday',
if isKeppelLeapYear1(date.year) then ydayMax = ydayMax + 1 end
'Tyrrhsday',
if isKeppelLeapYear2(date.year) then ydayMax = ydayMax + 1 end
'Lysisday',
-- rollover offset
'Octresday'
local rolloverOffset = isEarthLeapYear(date.year) and 79 or 78
}


--- sun signs (see [[Seventeen Suns]])
-- Keppel year and yday
local year = date.year - 892
local sun_signs = {
'[[File:Keppel-astrology-Alcaernon.png|32px|alt=]] Alcaernon',
local yday = ((ydayMax * (date.yday - rolloverOffset)) / ydayMaxEarth) % ydayMax
'[[File:Keppel-astrology-Eumrakh.png|32px|alt=]] Eumrakh',
local daynumraw = math.floor(yday)
'[[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
-- lythryd
function p.convert(time)
local lythrydNumber = math.ceil(yday / 32)
-- extract from time
local day = math.floor((yday - lythrydNumber * 32) % 32)
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(keppelYear)
-- 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(keppelYear)
end

-- stretch day of year to the Keppel year
keppelYday = (keppelYday / date.yearLength * keppelYearLength - 1) % 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 {
return {
year = year,
year = keppelYear,
daynumraw = daynumraw,
yday = keppelYday,
lythryd = lythryd,
lythrydNumber = lythrydNumber,
day = keppelDay,
lythryd = lythryds[lythrydNumber],
day = day
hour = keppelHour,
min = keppelMin,
sec = keppelSec
}
}
end
end


function p.invoke(frame)
function p.invoke(frame)
local args = require('Module:Arguments').getArgs(frame)
local args = getArgs(frame)


local time = args['time'] or os.time()
-- construct time from args
local date = os.date('!*t', time)
local time
if args['year'] and args['month'] and args['day'] then
return p.convertDate(date)
time = os.time{
year = args['year'],
month = args['month'],
day = args['day'],
hour = args['hour'] or 0,
min = args['min'] or args['minute'] or 0,
sec = args['sec'] or args['second'] 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
end


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

-- get result
local result = p.invoke(frame)
local result = p.invoke(frame)
return result.lythryd .. ' ' .. result.day .. ', ' .. result.year
-- 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
end



Latest revision as of 15:24, March 21, 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(keppelYear)
	-- 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(keppelYear)
	end

	-- stretch day of year to the Keppel year
	keppelYday = (keppelYday / date.yearLength * keppelYearLength - 1) % 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 args['minute'] or 0,
			sec = args['sec'] or args['second'] 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