Module:Delta functions

De Semantic MediaWiki - Sandbox

Révision datée du 26 juin 2021 à 15:52 par Marco (discussion | contributions) (Page créée avec « local p = {} function p.numDelta(frame) x = frame.args[1]; y = frame.args[2]; return y - x; end function trim(s) return (string.gsub(s, "^%s*(.-)%s*$", "%1"))… »)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

La documentation pour ce module peut être créée à Module:Delta functions/doc

local p = {}
 
 
function p.numDelta(frame)
	x = frame.args[1];
	y = frame.args[2];
	
	return y - x;
end

function trim(s)
  return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

function p.smwDelta(frame)

    if not mw.smw then
        return "mw.smw module not found: install and activate SemanticScribunto extension"
    end

    if frame.args[1] == nil or frame.args[2] == nil or frame.args[3] == nil then
        return "specify all the parameters"
    end
    
    local queryResult = mw.smw.ask( '[[Category:' .. frame.args[2] .. ']] |?' .. frame.args[3] .. ' |sort= |limit=2 |mainlabel=-')

    if queryResult == nil then
        return "(no values)"
    end
	
    if type(queryResult) == "table" then
        local myResult = 0
        local timeFrom = nil
        local reference = nil
        local pattern = "(%d+)-(%d+)-(%d+)"
        for num, row in pairs( queryResult ) do
            for property, data in pairs( row ) do
            	if trim(frame.args[1]) == "n" then
            		if num < 2 then
            			reference = tonumber(data)
            		else
                		myResult = tonumber(data) - myResult
                	end
                else
					local runyear, runmonth, runday = data:match(pattern)
					--mw.log(runyear)
					if num < 2 then
						reference = os.time{day=runday, year=runyear, month=runmonth}
					else
						local to = os.time{day=runday, year=runyear, month=runmonth}
						if trim(frame.args[1]) == "d" then
							timeFrom = os.difftime(to, reference) / (24 * 60 * 60) -- seconds in a day
						elseif trim(frame.args[1]) == "n" then
							timeFrom = os.difftime(to, reference) / (24 * 60 * 60 * 30) -- seconds in a month
						elseif trim(frame.args[1]) == "n" then
							timeFrom = os.difftime(to, reference) / (24 * 60 * 60 * 30 * 12) -- seconds in a year
						end
						myResult = math.floor(timeFrom)
					end
				end
            end
        end
        return myResult
    end

    return queryResult
end

	
return p
Les cookies nous aident à fournir nos services. En utilisant nos services, vous acceptez notre utilisation de cookies.