模块:倒计时
跳转到导航
跳转到搜索
此模块的文档可以在模块:倒计时/doc创建
local p = {}
local function esc( s )
s = tostring( s )
s = s:gsub( '&', '&' )
s = s:gsub( '<', '<' )
s = s:gsub( '>', '>' )
return s
end
function p.main( frame )
local args = frame.args
local s = tostring( args.date or '' )
local y, mo, d = s:match( '^(%d+)-(%d+)-(%d+)' )
if not y then
return '<span style="color:#d33;">错误:缺少有效的 date 参数(格式 YYYY-MM-DD)</span>'
end
y, mo, d = tonumber( y ), tonumber( mo ), tonumber( d )
if y < 1900 or y > 2100 or mo < 1 or mo > 12 or d < 1 or d > 31 then
return '<span style="color:#d33;">错误:date 参数超出合理范围</span>'
end
local target = os.time{ year = y, month = mo, day = d, hour = 0, min = 0, sec = 0 }
local nowt = os.date( '*t' )
local today = os.time{ year = nowt.year, month = nowt.month, day = nowt.day, hour = 0, min = 0, sec = 0 }
local diff = math.floor( ( target - today ) / 86400 + 0.5 )
local ev = esc( args.event or '' )
local evs = ''
if ev ~= '' then
evs = '<span class="dsh-bigcd-ev">' .. ev .. '</span>'
end
local num = '<span class="dsh-bigcd-num">'
if diff > 0 then
return '距离' .. evs .. '还有 ' .. num .. diff .. '</span> 天'
elseif diff == 0 then
if ev ~= '' then
return evs .. '就是今天!'
end
return '目标日就是今天!'
else
return evs .. '已经 ' .. num .. ( -diff ) .. '</span> 天'
end
end
return p