Module:Time

此后如竟没有炬火,我便是唯一的光。
荆哲留言 | 贡献2024年1月14日 (日) 18:55的版本
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在Module:Time/doc创建

local module = {}
local getArgs = require('Module:Arguments').getArgs

local tzbiou = 0 -- 特殊时期是否避开某些容易令人误会的色块
local winterDays = require('Module:冬至日期').winterDays
local winterSeconds = require('Module:冬至日期').winterSeconds

local winterLength = 23000
local firstWinter = 7322 -- 作为历元的冬至在上述数值的第7322号位

local rainbow = {"#f4592d", "#eda667", "#faf03e", "#5fb319", "#8eede2", "#0568e3","#49207c","#aa10aa"} -- 七元素的色号,赤橙黄绿青蓝紫,第八个是闰契的品红色

local tick = {18, 7, 23, 47, 10, 31, 16, 17, 12, 21, 34, 43, 37, 2, 13, 9, 36, 14, 4, 45, 27, 20, 39, 41, 24, 42, 19, 44, 32, 11, 26, 25, 30, 28, 8, 48, 5, 40, 29, 33, 6, 35, 38, 46, 15, 22, 3, 1}
local gua = {"火", "祭祀", "对手", "成败", "反叛", "救赎", "爆发", "欲望", "木", "创造", "新生", "凋谢", "重生", "弥散", "自我", "寻找", "存在", "经典", "革新", "梦幻", "倾听", "忍耐", "腐朽", "绝笔", "地", "孤独", "小人", "永恒", "恶魔", "盛开", "理解", "泥土", "风", "雾气", "神", "对抗", "绽放", "思考", "同伴", "变幻", "水", "隐退", "超越", "生命", "表达", "破碎", "时间", "朦胧", "自然"}

function module.s2d(frame) -- 秒转天,除以86400
	local args = getArgs(frame)
	local x = tonumber(args[1])
	return string.format("%.10f",x/86400)
end

function module.test()
	local now=os.time()
	local start=os.time({year=2021,month=9,day=1,hour=3})
	local days=(now-start)/86400
	local dayint=math.floor(days)
	local hours=((now-start)%86400)/3600
	return dayint..'天'..hours..'小时'
end
function module.directly() -- 直接获得当前时间
	return os.date('%p')..' '..os.date('%c')
end

-- 为方便起见,这里事实上是用补码,即数值 6 5 4 3 2 1 0 指代火木光土风水空。

function sunColor(inp) -- 太阳易双色显示,范围0-48,使用补码6543210
	local l = inp % 7 -- 下卦
	local u = math.floor(inp/7+0.125) % 7 -- 上卦
	if (l==4 and u==1 and tzbiou==1) then
		return ' '
	else
		return '<span style="background:-webkit-linear-gradient(90deg, '..rainbow[7-l]..' 0%, '..rainbow[7-l]..' 50%, '..rainbow[7-u]..' 50%, '..rainbow[7-u]..' 100%)"> </span>'
	end
end

function sunColor2(inp) -- 太阳易双色显示,范围1-49,使用原码1234567
	local l = inp % 7 -- 下卦
	local u = math.floor(inp/7+0.125) % 7 + 1 -- 上卦
	if (l==0) then
		l = 7
		u = u-1
	end
	if (l==3 and u==6 and tzbiou==1) then
		return ' '
	else
		return '<span style="background:-webkit-linear-gradient(90deg, '..rainbow[l]..' 0%, '..rainbow[l]..' 50%, '..rainbow[u]..' 50%, '..rainbow[u]..' 100%)"> </span>'
	end
end

function cheiColor(inp) -- 八契单色显示
	return '<span style="background:'..rainbow[inp]..'">  </span>'
end

function sun49(inp)
	return gua[49-(inp%49)]
end

function queIa(seconds)
	local spmin = 56
	local minph = 35
	local hpd = 14
	local dpm = 35
	local mpy = 14
	local s = seconds % spmin
	local minutes = math.floor((seconds - s) / spmin + 0.5)
	local mi = minutes % minph
	local hours = math.floor((minutes - mi) / minph + 0.5)
	local h = hours % hpd
	local days = math.floor((hours - h) / hpd + 0.5)
	local d = days % dpm
	local d1 = d + 1
	local months = math.floor((days - d) / dpm + 0.5)
	local m = months % mpy
	local m1 = m + 1
	local y = math.floor((months - m) / mpy + 0.5)
	local y1 = y
	if (y1 >= 0) then
		y1 = y1 + 1
	end
	return string.format("%d年%d月%d日 %02d:%02d:%02d", y1, m1, d1, h, mi, s)
end

function module.nowQueIa(frame)
	local args = getArgs(frame)
	local offset = args[1]
	if (offset == nil) then
		offset = 0
	else
		offset = tonumber(offset)
	end
	local now = os.time()
	local start = os.time({year=1900,month=4,day=4,hour=10,min=12,sec=42})
	now = now - start
	if (offset ~= nil) then
		now = now + offset
	end
	return queIa(now)
end

function module.dateQueIa(frame)
	local args = getArgs(frame)
	local y = args[1]
	local m = args[2]
	local d = args[3]
	local h = args[4]
	local mi = args[5]
	local s = args[6]
	local time1 = os.time({year=y,month=m,day=d,hour=h,min=m,sec=s})
	local start = os.time({year=1900,month=4,day=4,hour=10,min=12,sec=42})
	time1 = time1 - start
	return queIa(time1)
end

function module.nowSunTick(frame)
	local args = getArgs(frame)
	local color = string.lower(tostring(args[1])) -- 是否显示颜色
	local offset = args[2] -- 时间偏移,单位为秒
	if (offset == nil) then
		offset = 0
	else
		offset = tonumber(offset)
	end
	local now = os.time()
	local start = os.time({year=1996,month=1,day=28,hour=0})
	now = now - start
	if (offset ~= nil) then
		now = now + offset
	end
	local clock = (now-43200) % 86400 -- 一天中的时间,单位为秒,正午为零点
	local whichTick = math.floor(clock / 1800) + 1
	local c = ""
	-- 如果显示颜色
	if (color == "true") then
		c = sunColor2(tick[whichTick])
	end
	return c..gua[tick[whichTick]].."时"
end

function module.nowDuaenn(frame)
	local args = getArgs(frame)
	local display = args[1] -- 显示模式
	local offset = args[2] -- 时间偏移,单位为秒
	if (offset == nil) then
		offset = 0
	else
		offset = tonumber(offset)
	end
	local now = os.time()
	local start = os.time({year=1996,month=1,day=28,hour=0})
	now = now - start
	if (offset ~= nil) then
		now = now + offset
	end
	local clock = now % 86400 - 43200 -- 一天中的时间,单位为秒,正午为零点
	local duaenn = clock / 400 -- 转化为段
	local duint = math.floor(duaenn) -- 段的整数部分
	local suennz = (clock - 400 * duint) / 20 -- 舛
	local sint = math.floor(suennz) -- 舛的整数部分
	local mint = math.floor(clock % 20) -- 秒
	local kc = '';
	if (string.find(display,'k')~=nil) then
		kc=' ';
	end -- 空格与否
	if (display==nil) then
		return duint
	else
		if (string.find(display,":")~=nil) then
			if (string.find(display,"m")~=nil) then
				return string.format("%+04d%s:%s%02d%s:%s%02d",duint,kc,kc,sint,kc,kc,mint)
			else
				return string.format("%+04d%s:%s%02d",duint,kc,kc,sint)
			end
		else
			if (string.find(display,"m")~=nil) then
				return string.format("%+d%s段%s%02d%s舛%s%02d%s秒",duint,kc,kc,sint,kc,kc,mint,kc)
			else
				if (string.find(display,"s")~=nil) then
					return string.format("%+d%s段%s%02d%s舛",duint,kc,kc,sint,kc)
				else
					return string.format("%+d%s段",duint,kc)
				end
			end
		end
	end
end

function sunAriCalender(now, color)
	local start = os.time({year=1996,month=1,day=28,hour=0}) -- 以公历1996年1月28日零时开始算
	local days = (now-start) / 86400
	local dayint = math.floor(days)
	local dayfroms = dayint + 1706817 -- 事实上1996年1月28日是太阳易纪元第1,706,817天
	local dayFromOne = dayfroms - 49 -- 纪元零点开始过了49天再开始算
	local bigCount = math.floor((dayFromOne - 0.5) / 602651) -- 第几个1650年周期(从第0个开始算),1650年周期为602651天
	local bigRemain = dayFromOne - 602651 * bigCount
	local smallCount = math.floor((bigRemain - 0.5) / 4018) -- 第几个11年周期(从第0个开始算),11年周期为4018天
	local smallRemain = bigRemain - 4018 * smallCount
	local smallTotalCheiNum = math.floor((smallRemain + 48.5) / 49)
	local smallYear = math.floor((smallTotalCheiNum * 11 + 81.5) / 82)
	local formerCheis = math.floor(((smallYear - 1) * 82 + 0.5) / 11)
	local dayInYear = smallRemain - formerCheis * 49
	local cheiName = {"火","木","光","土","风","水","空","闰"}
	local cheiN = math.floor((dayInYear + 48.5) / 49) -- 第几契
	local dayN = cheiN * 49 - dayInYear
	local year = bigCount * 1650 + smallCount * 11 + smallYear
	local dc = ""
	local cc = ""
	-- 如果显示颜色
	if (color == "true") then
		dc = sunColor(dayN)
		cc = cheiColor(cheiN)
	end
	return year.."年 "..cc..cheiName[cheiN].."契 "..dc..sun49(dayN).."日"
end

function sunCalender(now, color, timezone)
	local start = os.time({year=1996,month=1,day=28,hour=0}) -- 以公历1996年1月28日零时开始算
	local days = (now-start) / 86400
	local dayint = math.floor(days)
	local dayfroms = dayint + 1706817 -- 事实上1996年1月28日是太阳易纪元第1,706,817天
	local i = 0
	local wd = winterDays
	local ws = winterSeconds
	local bottom = 9750 -- 只取[bottom,top]区间的冬至年份
	local top = bottom + 4095
	for i=bottom,top do
		-- 按时区调整冬至相应日期
		if (timezone ~= nil) then
			ws[i] = ws[i] + timezone
		end
		while (ws[i]>=86400)
		do
			wd[i] = wd[i] + 1
			ws[i] = ws[i] - 86400
		end
		while (ws[i]<0)
		do
			wd[i] = wd[i] - 1
			ws[i] = ws[i] + 86400
		end
		-- 冬至后(含)第二个自然日,为年末自然日
		wd[i] = wd[i] + 97 - (wd[i]-1)%49
	end
	local left = bottom --二分查找,左闭
	local right = top + 1 --二分查找,右开
	local middle = 0
	-- 寻找严格小于dayfroms的最后一个冬至
	while (right - left > 1)
	do
		middle = math.floor((left+right+0.5)/2) --二分中点,向下取整
		if (dayfroms <= wd[middle]) then
			--比middle所在年的冬至要早,不含middle
			right = middle
		else
			--可能比middle所在年要晚,也有可能正好是这一年
			left = middle
		end
	end
	--left即为所求年的下标
	local year = left - firstWinter + 1 -- 七历年份
	local restdays = dayfroms - wd[left] -- 这一年的具体第几天
	local cheiName = {"火","木","光","土","风","水","空","闰"}
	local cheiN = math.floor((restdays + 48.5) / 49) -- 第几契
	local dayN = cheiN * 49 - restdays
	local dc = ""
	local cc = ""
	-- 如果显示颜色
	if (color == "true") then
		dc = sunColor(dayN)
		cc = cheiColor(cheiN)
	end
	return year.."年 "..cc..cheiName[cheiN].."契 "..dc..sun49(dayN).."日"
end

function sunDay(now, display, color)
	local start = os.time({year=1996,month=1,day=28,hour=0}) -- 以公历1996年1月28日零时开始算
	local days = (now-start) / 86400
	local dayint = math.floor(days)
	local dayfroms = dayint + 1706817 -- 事实上1996年1月28日是太阳易纪元第1,706,817天
	local phase = (-dayfroms) % (49*49*49*49)
	local d = phase % 49 -- 日
	local s = math.floor(phase/(49)+0.00000000001) % 49 -- 季
	local t = math.floor(phase/(49*49)+0.00000000001) % 49 -- 轮
	local p = math.floor(phase/(49*49*49)+0.0000000000001) -- 朝
	local dc = ""
	local sc = ""
	local tc = ""
	local pc = ""
	-- 如果显示颜色
	if (color == "true") then
		dc = sunColor(d)
		sc = sunColor(s)
		tc = sunColor(t)
		pc = sunColor(p)
	end
	if (display=='1') then
		return dc..sun49(d).."日"
	else
		if (display=='2') then
			return sc..sun49(s).."季 "..dc..sun49(d).."日"
		else
			if (display=='3') then
				return tc..sun49(t).."轮 "..sc..sun49(s).."季 "..dc..sun49(d).."日"
			else
				return pc..sun49(p).."朝 "..tc..sun49(t).."轮 "..sc..sun49(s).."季 "..dc..sun49(d).."日"
			end
		end
	end
end

function module.nowSunCalender(frame)
	local args = getArgs(frame)
	local color = string.lower(tostring(args[1])) -- 是否显示颜色
	local timezone = args[2] -- 时区,【相对于东八区】,单位为【秒】,注意!
	local offset = args[3] -- 时间偏移,单位为天
	if (timezone == nil) then
		timezone = 0
	else
		timezone = tonumber(timezone)
	end
	if (offset == nil) then
		offset = 0
	else
		offset = tonumber(offset)
	end
	local now = os.time()
	if (offset ~= nil) then
		now = now + offset * 86400
	end
	if (timezone ~= nil) then
		now = now + timezone
	end
	return sunCalender(now, color, timezone)
end

function module.dateSunCalender(frame)
	local args = getArgs(frame)
	local y = tonumber(args[1])
	local m = tonumber(args[2])
	local d = tonumber(args[3])
	local color = string.lower(tostring(args[4])) -- 是否显示颜色
	local timezone = args[5] -- 时区,【相对于东八区】,单位为【秒】,注意!
	if (timezone == nil) then
		timezone = 0
	else
		timezone = tonumber(timezone)
	end
	local date = os.time({year=y,month=m,day=d})
	if (timezone ~= nil) then
		date = date + timezone
	end
	return sunCalender(date, color, timezone)
end

function module.nowSunAriCalender(frame)
	local args = getArgs(frame)
	local color = string.lower(tostring(args[1])) -- 是否显示颜色
	local timezone = args[2] -- 时区,【相对于东八区】,单位为【秒】,注意!
	local offset = args[3] -- 时间偏移,单位为天
	if (timezone == nil) then
		timezone = 0
	else
		timezone = tonumber(timezone)
	end
	if (offset == nil) then
		offset = 0
	else
		offset = tonumber(offset)
	end
	local now = os.time()
	if (offset ~= nil) then
		now = now + offset * 86400
	end
	if (timezone ~= nil) then
		now = now + timezone
	end
	return sunAriCalender(now, color)
end

function module.dateSunAriCalender(frame)
	local args = getArgs(frame)
	local y = tonumber(args[1])
	local m = tonumber(args[2])
	local d = tonumber(args[3])
	local color = string.lower(tostring(args[4])) -- 是否显示颜色
	local timezone = args[5] -- 时区,【相对于东八区】,单位为【秒】,注意!
	if (timezone == nil) then
		timezone = 0
	else
		timezone = tonumber(timezone)
	end
	local date = os.time({year=y,month=m,day=d})
	if (timezone ~= nil) then
		date = date + timezone
	end
	return sunAriCalender(date, color)
end

function module.nowSunDay(frame)
	local args = getArgs(frame)
	local display = args[1] -- 显示长度
	local color = string.lower(tostring(args[2])) -- 是否显示颜色
	local offset = args[3] -- 时间偏移,单位为天
	if (offset == nil) then
		offset = 0
	else
		offset = tonumber(offset)
	end
	local now = os.time()
	if (offset ~= nil) then
		now = now + offset * 86400
	end
	return sunDay(now, display, color)
end

function module.dateSunDay(frame)
	local args = getArgs(frame)
	local y = tonumber(args[1])
	local m = tonumber(args[2])
	local d = tonumber(args[3])
	local display = args[4] -- 显示长度
	local color = string.lower(tostring(args[5])) -- 是否显示颜色
	local now = os.time({year=y,month=m,day=d})
	return sunDay(now, display, color)
end

function module.latinTime(frame)
	local args = getArgs(frame)
	local offset = args[1] -- 时间偏移,单位为时
	local month = {"Jeneiro", "Fabruario", "Marco", "Abrile", "Maio", "Jugno", "Julio", "Agosto", "Setembre", "Oktubre", "Novembre", "Decembre"} -- 1至12月
	local weekday = {"Lunde", "Marde", "Merkrede", "Jeude", "Vendrede", "Sabado", "Domingo"} -- 周一至周日
	local now = os.time() - 3600 * 8
	if (offset ~= nil) then
		if (tonumber(offset)~=nil) then
			now = now + offset * 3600
		end
	end
	return os.date("%X\n",now)..weekday[7-((7-tonumber(os.date("%w",now)))%7)]..os.date(", %d ",now)..month[tonumber(os.date("%m",now))]..os.date(" %Y",now)
end


function module.NorasiaTime(frame)
	local args = getArgs(frame)
	local offset = args[1] -- 时间偏移,单位为时
	local month = {"1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"} -- 1至12月
	local weekday = {"月曜日", "火曜日", "水曜日", "木曜日", "金曜日", "土曜日", "日曜日"} -- 周一至周日
	local now = os.time() - 3600 * 8
	if (offset ~= nil) then
		if (tonumber(offset)~=nil) then
			now = now + offset * 3600
		end
	end
	return os.date("%X\n",now)..os.date(" %Y年 ",now)..month[tonumber(os.date("%m",now))]..os.date("%d日 ",now)..weekday[7-((7-tonumber(os.date("%w",now)))%7)]
end

function stringAt(str, pos)
	return string.sub(str, pos, pos)
end

function HetesflusYear(year)
	year = year - 1326
	return stringAt('eDcszywhAaoiu', year % 13 + 1) ..
		stringAt('tbpmfvd', year % 7 + 1) ..
		stringAt('ZnlrgkNxjqS', year % 11 + 1)
end

function module.HetesflusTime(frame)
	local args = getArgs(frame)
	local offset = args[1]
	local now = os.time()
	if (offset ~= nil and tonumber(offset) ~= nil) then
		now = now + offset * 3600
	end
	local dayOfYear = tonumber(os.date('%j', now))
	local year = tonumber(os.date('%Y', now))
	local month = 1
	local sum = -11
	local days = {30, 30, 30, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30}
	if year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0) then
		days[3] = 31
	end
	while sum + days[month] < dayOfYear do
		sum = sum + days[month]
		month = month + 1
	end
	if month < 4 then
		year = year - 1
	end
	local monthName = {'冬', '寒', '花', '春', '收', '祭', '夏', '航', '酒', '秋', '種', '雨', '冬'}
	return '<samp>' .. HetesflusYear(year) .. '</samp>年' ..
		monthName[month] .. '月' ..
		(dayOfYear - sum) .. '日'
end

return module