模块:Countdown:修订间差异
MJ Hamster(留言 | 贡献) 无编辑摘要 |
MJ Hamster(留言 | 贡献) 无编辑摘要 |
||
| (未显示同一用户的8个中间版本) | |||
| 第6行: | 第6行: | ||
local function formatMessage(secondsLeft, event, color) | local function formatMessage(secondsLeft, event, color) | ||
local timeLeft = lang:formatDuration(secondsLeft, {'years | local timeLeft = lang:formatDuration(secondsLeft, {'years', 'days', 'hours', 'minutes'}) | ||
-- Color and bold the numbers, because it makes them look important. | -- Color and bold the numbers, because it makes them look important. | ||
timeLeft = string.gsub(timeLeft, '(%d+)', '<span | timeLeft = string.gsub(timeLeft, '(%d+)', '<span> %1</span>') | ||
return string.format(' | return string.format('<span style="font-weight: bold;">%s</span>', timeLeft) | ||
end | end | ||
| 第51行: | 第51行: | ||
text = args.eventstart or ((lang:ucfirst(args.event or '事件')) .. ' 已开始。') | text = args.eventstart or ((lang:ucfirst(args.event or '事件')) .. ' 已开始。') | ||
end | end | ||
return text | |||
return text | |||
end | end | ||
return p | return p | ||
2025年9月15日 (一) 15:59的最新版本
此模块的文档可以在模块:Countdown/doc创建
local p = {}
-- Constants
local lang = mw.language.getContentLanguage()
local getArgs = require('Module:Arguments').getArgs
local function formatMessage(secondsLeft, event, color)
local timeLeft = lang:formatDuration(secondsLeft, {'years', 'days', 'hours', 'minutes'})
-- Color and bold the numbers, because it makes them look important.
timeLeft = string.gsub(timeLeft, '(%d+)', '<span> %1</span>')
return string.format('<span style="font-weight: bold;">%s</span>', timeLeft)
end
function p.main(frame)
local args = getArgs(frame)
if not (args.year and args.month and args.day) then
return '<strong class="error">错误:需指定年、月、日。</strong>'
end
local timeArgs = {year=args.year, month=args.month, day=args.day, hour=args.hour, min=args.minute, sec=args.second}
for k,v in pairs(timeArgs) do
if not tonumber(v) then
error('Argument ' .. k .. ' could not be parsed as a number: ' .. v)
end
end
local eventTime = os.time(timeArgs)
local timeToStart = os.difftime(eventTime, os.time()) -- (future time - current time)
local text
if timeToStart > 0 then
-- Event has not begun yet
text = formatMessage(timeToStart, args.event or '事件开始', args.color)
elseif args.duration then
local timeToEnd
if args['duration unit'] then
-- Duration is in unit other than seconds, use formatDate to add
timeToEnd = tonumber(lang:formatDate('U', '@' .. tostring(timeToStart) .. ' +' .. tostring(args.duration) .. ' ' .. args['duration unit']))
else
timeToEnd = timeToStart + (tonumber(args.duration) or error('args.duration should be a number of seconds', 0))
end
if timeToEnd > 0 then
-- Event is in progress
text = args.eventstart or formatMessage(timeToEnd, args.color)
else
-- Event had a duration and has now ended
text = args.eventend or ((lang:ucfirst(args.event or '事件')) .. ' 已结束。')
end
else
-- Event had no duration and has begun
text = args.eventstart or ((lang:ucfirst(args.event or '事件')) .. ' 已开始。')
end
return text
end
return p