打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

目前wiki关闭了自行注册账号的功能,如需注册账号,请查阅Help:注册账号

模块: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留言 | 贡献
无编辑摘要
第2行: 第2行:


-- 获取时间戳的辅助函数
-- 获取时间戳的辅助函数
-- 逻辑完全复刻原模板:
-- 1. 如果输入是 'now',直接使用当前服务器时间
-- 2. 如果是具体时间字符串,末尾添加 +08:00 (模拟北京时间/CST输入)
local function getTimestamp(timeStr)
local function getTimestamp(timeStr)
    -- 处理空值,默认为 'now'
     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)
      
      
    -- 检查是否为 'now' (不区分大小写)
     if string.lower(timeStr) == 'now' then
     if string.lower(timeStr) == 'now' then
         return os.time()
         return os.time()
     end
     end
      
      
    -- 按照原模板逻辑,非 now 的时间字符串默认视为 UTC+8
    -- 通过 mw.language 处理时间解析
     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
        -- 如果解析失败(例如格式极度不规范),返回当前时间或报错
        -- 为了健壮性,这里返回 nil,在主函数处理错误
         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['格式']) -- 如果未传入或非数字,则为 nil
     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


     -- 如果没有传入“格式”参数,保持原模板行为:仅输出秒数(向下取整)
     -- 未指定格式,返回原始秒数
     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 days = math.floor(absDiff / 86400)
     local totalDays = math.floor(absDiff / 86400)
     local remainder = absDiff % 86400
     local remainder = absDiff % 86400
      
      
第74行: 第60行:
     local minutes = math.floor(remainder / 60)
     local minutes = math.floor(remainder / 60)
     local seconds = remainder % 60
     local seconds = remainder % 60
    -- 年份计算逻辑 (仅当格式 >= 5 时启用)
    -- 定义 1年 = 365天
    local years = 0
    local days = totalDays
    if formatType >= 5 then
        years = math.floor(totalDays / 365)
        days = totalDays % 365
    end


     -- 构建输出字符串
     -- 构建输出字符串
     local output = days .. "天"
     local output = ""
   
    -- 格式5:输出年份
    if formatType >= 5 then
        output = output .. years .. "年"
    end
 
    -- 所有格式都有天数 (如果是格式5,这里的 days 已经是去掉了年份后的剩余天数)
    output = output .. days .. "天"
      
      
    -- 格式2及以上:输出小时
     if formatType >= 2 then
     if formatType >= 2 then
         output = output .. hours .. "小时"
         output = output .. hours .. "小时"
     end
     end
      
      
    -- 格式3及以上:输出分钟
     if formatType >= 3 then
     if formatType >= 3 then
         output = output .. minutes .. "分钟"
         output = output .. minutes .. "分钟"
     end
     end
      
      
    -- 格式4及以上:输出秒数
     if formatType >= 4 then
     if formatType >= 4 then
         output = output .. seconds .. "秒"
         output = output .. seconds .. "秒"

2025年11月24日 (一) 16:23的版本

此模块的文档可以在模块: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

    -- 未指定格式,返回原始秒数
    if not formatType then
        return math.floor(diff)
    end

    local absDiff = math.abs(diff)
    
    -- 基础单位计算
    local totalDays = math.floor(absDiff / 86400)
    local remainder = absDiff % 86400
    
    local hours = math.floor(remainder / 3600)
    remainder = remainder % 3600
    
    local minutes = math.floor(remainder / 60)
    local seconds = remainder % 60

    -- 年份计算逻辑 (仅当格式 >= 5 时启用)
    -- 定义 1年 = 365天
    local years = 0
    local days = totalDays

    if formatType >= 5 then
        years = math.floor(totalDays / 365)
        days = totalDays % 365
    end

    -- 构建输出字符串
    local output = ""
    
    -- 格式5:输出年份
    if formatType >= 5 then
        output = output .. years .. "年"
    end

    -- 所有格式都有天数 (如果是格式5,这里的 days 已经是去掉了年份后的剩余天数)
    output = output .. days .. "天"
    
    -- 格式2及以上:输出小时
    if formatType >= 2 then
        output = output .. hours .. "小时"
    end
    
    -- 格式3及以上:输出分钟
    if formatType >= 3 then
        output = output .. minutes .. "分钟"
    end
    
    -- 格式4及以上:输出秒数
    if formatType >= 4 then
        output = output .. seconds .. "秒"
    end

    return output
end

return p