模組:Duration
The module is meant for language-dependant time calculations.
It exports a single function: sumHMS, which accepts unnamed (aka "order based") arguments in the form ssss (any number of digits) or "mm:ss" or "hh:mm:ss:, sums up all the periods, and returns the output string describing the total duration.
An optional paramter, "units" controls the format of the output.
例子[编辑]
- {{#invoke:Duration | sumHMS | 7211 | units = minutes, seconds}}得到“120分11秒”。
- {{#invoke:Duration |sumHMS | 1:0:11 | 1:00:00 | units= seconds}}得到“7,211秒”。
- {{#invoke:Duration | sumHMS | 10:10:10 | 20:20:20 | 12:13:14 }}得到“1天18小时43分44秒”。
上述文档嵌入自Module:Duration/doc。 (编辑 | 历史) 编者可以在本模块的沙盒 (创建 | 镜像)和测试样例 (创建)页面进行实验。 本模块的子页面。 |
--[[
this module is meant for language-dependant time calculations.
ATM it exports a single function: sumHMS, which accepts unnamed (aka "order based")
arguments in the form ssss (any number of digits) or "mm:ss" or "hh:mm:ss:,
converts them all to seconds, sums them up, and returns the output string
describing the total duration, using mw.getContentLanguage():formatDuration()
an optional paramter, "units" tells the formatting function which units to use:
e.g. {{invoke:Duration | sumHMS | 7211 | units = minutes, seconds}} will return "120 minutes and 11 seconds".
{{invoke:Duration|sumHMS| 1:0:11 | 1:00:00 | units= seconds}} will return "7211 seconds"
]]
local function sumHMS( args )
local total = 0
function oneMore( st )
if type( st ) ~= 'string' then return end
local t, ar = 0, { st:match( "^%s*(%d*):?(%d*):?(%d*)%s*$" ) }
for _, v in ipairs( ar ) do
local n = tonumber( v )
if n then t = t * 60 + n end
end
total = total + t
end
for _, v in ipairs( args ) do oneMore( v ) end
local units
if args.units then
units = {}
for unit in mw.ustring.gmatch( args.units, "%w+" ) do -- this silliness should be replaced with mw.text.split("%W+") once mw.text is available.
table.insert( units, unit )
end
end
return mw.language.getContentLanguage():formatDuration( total, units )
end
return {
sumHMS = function( frame ) return sumHMS( frame.args ) end,
}