七日内新公告:全站维护公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:Astrology

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]

该模块可以免去人工计算星座的麻烦。

使用方法

兼容多种输入:

  • {{#invoke:Astrology|convert|1|8}} 摩羯座
  • {{#invoke:Astrology|convert|0108}} 摩羯座
  • {{#invoke:Astrology|convert|01月08日}} 摩羯座
  • {{#invoke:Astrology|convert|1月8日}} 摩羯座

日期非法时,报错:

  • {{#invoke:Astrology|convert|1|32}} Lua错误 在第32行:输入的日期无效:1月32日
local p = {}
 
local getArgs = require('Module:Arguments').getArgs
local from_text = require('Module:生日分类').from_text
local dayPerMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
 
function p._convert(args)
	local month, day
	if args[2] then
		month = args[1]
		day = args[2]
	else
		local text = from_text(args[1],1)
		month, day = string.match(text,'(%d%d?)月(%d%d?)日') -- "01月02日" / "1月2日"
		if not (month and day) then
			month, day = string.match(args[1], '^(%d%d?)[-/](%d%d?)$') -- "01-02" / "01/02" / "1/2"
		end
		if not (month and day) then
			month, day = string.match(args[1], '^(%d%d)(%d%d)$') -- "0102"
		end
		if not (month and day) then
			error('不支持的日期格式:' .. args[1]) 
		end
	end
 
	month = tonumber(month)
	day = tonumber(day)
	if month == nil or day == nil then
		error('调用模板或模块的参数错误:1=' .. args[1] .. ' 2=' .. args[2])
	end
	if day > (dayPerMonth[month] or 0) or day <= 0 then
		error('输入的日期无效:' .. month .. '月' .. day .. '日')
	end
	local combined = string.format('%02d%02d', month, day)
	if combined < '0121' then
		return '摩羯座'
	elseif combined < '0220' then
		return '水瓶座'
	elseif combined < '0321' then
		return '双鱼座'
	elseif combined < '0421' then
		return '白羊座'
	elseif combined < '0522' then
		return '金牛座'
	elseif combined < '0622' then
		return '双子座'
	elseif combined < '0723' then
		return '巨蟹座'
	elseif combined < '0823' then
		return '狮子座'
	elseif combined < '0923' then
		return '处女座'
	elseif combined < '1024' then
		return '天秤座'
	elseif combined < '1123' then
		return '天蝎座'
	elseif combined < '1222' then
		return '射手座'
	else
		return '摩羯座'
	end
 
end
 
function p.convert(frame)
	local args = getArgs(frame, {wrappers='Template:Astrology'})
	return p._convert(args)
end
 
return p