Mòdul:Data

De WikiPrat
La revisió el 10:16, 13 març 2019 per Frange (discussió | contribucions) (Es crea la pàgina amb «local p = {} local mes_num = { ['jan'] = 1, ['gen'] = 1, ['feb'] = 2, ['mar'] = 3, ['apr'] = 4, ['abr'] = 4, ['may'] = 5, ['mai'] = 5, ['jun'] = 6, ['jul'] = 7...».)
(dif) ← Versió més antiga | Versió actual (dif) | Versió més nova → (dif)
Jump to navigation Jump to search

local p = {}

local mes_num = { ['jan'] = 1, ['gen'] = 1, ['feb'] = 2, ['mar'] = 3, ['apr'] = 4, ['abr'] = 4, ['may'] = 5, ['mai'] = 5, ['jun'] = 6, ['jul'] = 7, ['aug'] = 8, ['ago'] = 8, ['ag'] = 8, ['sep'] = 9, ['set'] = 9, ['oct'] = 10, ['nov'] = 11, ['des'] = 12 }

local function f(...) return mw.language.new('ca'):formatDate(...) end

function p.data_format(data, formata) if type(data) == "table" then -- des de plantilles via invoke o des de mòduls via require formata = data.args[2] data = require('Module:Delink')._delink(data.args) end if formata == then formata = nil end

-- sense preposicions data = string.gsub(data, " de ", " ") data = string.gsub(data, " d'", " ")

dataparts = mw.text.split(data, "[-/. ]") if #dataparts == 3 then part1 = tonumber(dataparts[1]) part2 = tonumber(dataparts[2]) part3 = tonumber(dataparts[3]) if part1 and part2 and part3 then if part1 > 31 then -- format a m d any = part1 mes = part2 dia = part3 elseif part2 > 12 then -- format m d a mes = part1 dia = part2 any = part3 else -- format d m a dia = part1 mes = part2 any = part3 end else -- format d mes a mes_abr = string.lower(string.sub(dataparts[2], 1, 3)) mes = mes_num[mes_abr] dia = part1 any = part3 end

if dia and mes and any and type(dia) == 'number' and type(mes) == 'number' and type(any) == 'number' then datastr = dia .. '-' .. mes .. '-' .. any if formata == nil or formata == 'consulta' then if dia == 1 then formata = 'j"r" F Y' else formata = 'j F Y' end end local success, data = pcall(f, formata, datastr) if success then return data else return "Data incorrecta" end end end

return data end

return p