模块:TimeDiff:修订间差异
来自RIA | Wiki
更多操作
BCMonomial(留言 | 贡献) 创建页面,内容为“local p = {} -- 获取时间戳的辅助函数 -- 逻辑完全复刻原模板: -- 1. 如果输入是 'now',直接使用当前服务器时间 -- 2. 如果是具体时间字符串,末尾添加 +08:00 (模拟北京时间/CST输入) local function getTimestamp(timeStr) -- 处理空值,默认为 'now' if not timeStr or mw.text.trim(timeStr) == '' then timeStr = 'now' end timeStr = mw.text.trim(timeStr) -- 检查是否…” |
BCMonomial(留言 | 贡献) 无编辑摘要 |
||
| (未显示同一用户的1个中间版本) | |||
| 第2行: | 第2行: | ||
-- 获取时间戳的辅助函数 | -- 获取时间戳的辅助函数 | ||
local function getTimestamp(timeStr) | local function getTimestamp(timeStr) | ||
if not timeStr or mw.text.trim(timeStr) == '' then | if not timeStr or mw.text.trim(timeStr) == '' then | ||
timeStr = 'now' | timeStr = 'now' | ||
| 第13行: | 第9行: | ||
timeStr = mw.text.trim(timeStr) | timeStr = mw.text.trim(timeStr) | ||
if string.lower(timeStr) == 'now' then | if string.lower(timeStr) == 'now' then | ||
return os.time() | return os.time() | ||
end | end | ||
local lang = mw.language.getContentLanguage() | local lang = mw.language.getContentLanguage() | ||
local success, timestamp = pcall(function() | local success, timestamp = pcall(function() | ||
| 第28行: | 第21行: | ||
return timestamp | return timestamp | ||
else | else | ||
return nil | return nil | ||
end | end | ||
| 第35行: | 第26行: | ||
function p.main(frame) | function p.main(frame) | ||
local args = frame:getParent().args | local args = frame:getParent().args | ||
if not args['开始时间'] and not args['结束时间'] and not args['格式'] then | if not args['开始时间'] and not args['结束时间'] and not args['格式'] then | ||
| 第41行: | 第31行: | ||
end | end | ||
local startTimeStr = args['开始时间'] | local startTimeStr = args['开始时间'] | ||
local endTimeStr = args['结束时间'] | local endTimeStr = args['结束时间'] | ||
local formatType = tonumber(args['格式']) | local formatType = tonumber(args['格式']) | ||
local startTs = getTimestamp(startTimeStr) | local startTs = getTimestamp(startTimeStr) | ||
local endTs = getTimestamp(endTimeStr) | local endTs = getTimestamp(endTimeStr) | ||
| 第54行: | 第42行: | ||
end | end | ||
local diff = endTs - startTs | local diff = endTs - startTs | ||
-- | -- 情况0:未指定格式,返回原始秒数(带正负号) | ||
if not formatType then | if not formatType then | ||
return math.floor(diff) | return math.floor(diff) | ||
end | end | ||
-- | -- 准备计算格式化时间,使用绝对值 | ||
local absDiff = math.abs(diff) | local absDiff = math.abs(diff) | ||
local totalDays = math.floor(absDiff / 86400) | |||
-- | |||
local | -- 情况5:特殊处理,仅输出 年+天 | ||
if formatType == 5 then | |||
local years = math.floor(totalDays / 365) | |||
local days = totalDays % 365 | |||
return years .. "年" .. days .. "天" | |||
end | |||
-- 情况1-4:常规处理 | |||
local remainder = absDiff % 86400 | local remainder = absDiff % 86400 | ||
| 第75行: | 第69行: | ||
local seconds = remainder % 60 | local seconds = remainder % 60 | ||
-- 构建输出字符串 | -- 构建输出字符串 (格式 1-4) | ||
local output = | local output = totalDays .. "天" | ||
if formatType >= 2 then | if formatType >= 2 then | ||
2025年11月24日 (一) 16:31的最新版本
此模块的文档可以在模块:TimeDiff/doc创建
local p = {}
-- 获取时间戳的辅助函数
local function getTimestamp(timeStr)
if not timeStr or mw.text.trim(timeStr) == '' then
timeStr = 'now'
end
timeStr = mw.text.trim(timeStr)
if string.lower(timeStr) == 'now' then
return os.time()
end
local lang = mw.language.getContentLanguage()
local success, timestamp = pcall(function()
return tonumber(lang:formatDate('U', timeStr .. '+08:00'))
end)
if success and timestamp then
return timestamp
else
return nil
end
end
function p.main(frame)
local args = frame:getParent().args
if not args['开始时间'] and not args['结束时间'] and not args['格式'] then
args = frame.args
end
local startTimeStr = args['开始时间']
local endTimeStr = args['结束时间']
local formatType = tonumber(args['格式'])
local startTs = getTimestamp(startTimeStr)
local endTs = getTimestamp(endTimeStr)
if not startTs or not endTs then
return '<span class="error">时间格式错误</span>'
end
local diff = endTs - startTs
-- 情况0:未指定格式,返回原始秒数(带正负号)
if not formatType then
return math.floor(diff)
end
-- 准备计算格式化时间,使用绝对值
local absDiff = math.abs(diff)
local totalDays = math.floor(absDiff / 86400)
-- 情况5:特殊处理,仅输出 年+天
if formatType == 5 then
local years = math.floor(totalDays / 365)
local days = totalDays % 365
return years .. "年" .. days .. "天"
end
-- 情况1-4:常规处理
local remainder = absDiff % 86400
local hours = math.floor(remainder / 3600)
remainder = remainder % 3600
local minutes = math.floor(remainder / 60)
local seconds = remainder % 60
-- 构建输出字符串 (格式 1-4)
local output = totalDays .. "天"
if formatType >= 2 then
output = output .. hours .. "小时"
end
if formatType >= 3 then
output = output .. minutes .. "分钟"
end
if formatType >= 4 then
output = output .. seconds .. "秒"
end
return output
end
return p