Modul:LinkFormDatum: Unterschied zwischen den Versionen

linkFormDate() zusätzlich ISO-P case YYYY-MM-DD
Keine Bearbeitungszusammenfassung
(linkFormDate() zusätzlich ISO-P case YYYY-MM-DD)
 
(12 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 4: Zeile 4:


function p.LinkFormDate(frame)
function p.LinkFormDate(frame)
-- #invoke wrapper
local args = frame.args
local args = frame.args
local date = args[1]
    return p.linkFormDate(args[1], args[2], args[3], args[4])
local attr_date = args[2]
local attr_day = args[3]
local attr_year = args[4]
    return p.link_form_date(date, attr_date, attr_day, attr_year)
end
end


function p.link_form_date(date, attr_date, attr_day, attr_year)
function p.linkFormDate(date, attr_date, attr_day, attr_year)
     local months=getMonthList()
     local months = c.getMonthList()
     local date_array=str.split(date, "/")
     local date_array
    local mda
    local mn -- monthname
 
if string.find(date, "/") ~= nil then -- regular case YYYY/MM/DD
    date_array = str.split(date, "/")
    else -- ISO-P case YYYY-MM-DD
    date_array = str.split(date, "-")
    end
      
      
     mw.smw.set({[attr_date] = date})
     -- wenn keine Attribute übergeben, werden keine gesetzt, sondern nur Links eingebaut
    if attr_day ~= nil then
    attr_day = attr_day .. "::"
    else
    attr_day = ""
    end
    if attr_year ~= nil then
    attr_year = attr_year .. "::"
    else
    attr_year = ""
    end
    if attr_date ~= nil then
    mw.smw.set({[attr_date] = date})
    end
     if c.isEmpty(date_array[2]) then
     if c.isEmpty(date_array[2]) then
     -- When datestring contained no "/"
     -- When datestring contained no "/"
     if p.isInteger(date_array[1]) then
     if c.isInteger(date_array[1]) then
     -- When dateformat is just YYYY
     -- When dateformat is just YYYY
     return "[[" .. attr_year .. "::" .. date_array[1] .. "]]"
     return "[[" .. attr_year .. date_array[1] .. "]]"
     else
     else
     -- Dateformat is <monthname> YYYY
     -- Dateformat is <monthname> YYYY
     local mda = str.split(date_array[1])
     mda = str.split(date_array[1])
     return mda[1] .. " [[" .. attr_year .. "::" .. mda[2] .. "]]"
     return mda[1] .. " [[" .. attr_year .. mda[2] .. "]]"
     end
     end
     elseif c.isEmpty(date_array[3]) then
     elseif c.isEmpty(date_array[3]) then
     -- Datestring contained one "/"
     -- Datestring contained one "/"
     local mn = months[tonumber(date_array[2])]
     mn = months[tonumber(date_array[2])]
     return mn .. " [[" .. attr_year .. "::" .. date_array[1] .. "]]"
     return mn .. " [[" .. attr_year .. date_array[1] .. "]]"
     else
     else
     -- Dateformat should be YYYY/MM/DD
     -- Dateformat should be YYYY/MM/DD or YYYY-MM-DD
     local mn = months[tonumber(date_array[2])]
     mn = months[tonumber(date_array[2])]
     date_array[3] = date_array[3]:gsub('0*', '', 1)
     date_array[3] = date_array[3]:gsub('0*', '', 1)
     return "[[" .. attr_day .. "::" .. date_array[3] .. ". " .. mn .. "]]" ..  
     return "[[" .. attr_day .. date_array[3] .. ". " .. mn .. "]]" ..  
     " [[" .. attr_year .. "::" .. date_array[1] .. "]]"
     " [[" .. attr_year .. date_array[1] .. "]]"
     end
     end
end
end


function p.FormatDate(frame)
function p.FormatDate(frame)
local args = frame.args
-- #invoke wrapper
return p.format_date(args[1])
return p.formatDate(frame.args[1])
end
end


function p.format_date(date)
function p.formatDate(date)
local date_array = str.split(date, " ")
local date_array = str.split(date, " ")
local day
if c.isEmpty(date_array[2]) then
if c.isEmpty(date_array[2]) then
     -- When datestring contained no " ", thus is year
     -- When datestring contained no " ", thus is year
Zeile 55: Zeile 75:
else
else
-- When datestring contains day, monthname and year
-- When datestring contains day, monthname and year
local day = string.gsub(date_array[1], "%.", "")
day = string.gsub(date_array[1], "%.", "")
return "[[" .. day .. ". " .. date_array[2] .. "]] " ..  
return "[[" .. day .. ". " .. date_array[2] .. "]] " ..  
"[[" .. date_array[3] .. "]]"
"[[" .. date_array[3] .. "]]"
Zeile 62: Zeile 82:


function p.HumanToFormDate(frame)
function p.HumanToFormDate(frame)
-- Converts a human date (3. Februar 1955) to 1955/02/03, like SMW needs
-- #invoke wrapper
local args = frame.args
return p.humanToFormDate(frame.args[1])
return p.human_to_form_date(args[1])
end
end


function p.human_to_form_date(date)
function p.humanToFormDate(date)
mw.log("p.humanToFormDate(date)")
mw.logObject(date)
-- Converts a human date (3. Februar 1955) to 1955/02/03, like SMW needs
local date_array=str.split(date, " ")
local date_array=str.split(date, " ")
local day
local mnr -- monthnr
if c.isEmpty(date_array[2]) then
if c.isEmpty(date_array[2]) then
-- When datestring contained no " ", thus is year
-- When datestring contained no " ", thus is year
Zeile 74: Zeile 99:
elseif c.isEmpty(date_array[3]) then
elseif c.isEmpty(date_array[3]) then
-- When datestring contains monthname and year
-- When datestring contains monthname and year
return date_array[1].." "..date_array[1]
return date_array[1].." "..date_array[2]
else
else
local day = string.gsub(date_array[1], "%.", "")
day = string.gsub(date_array[1], "%.", "")
day = string.format("%02d", day)
day = string.format("%02d", day)
local mnr = getMonthnr(date_array[2])
mnr = c.getMonthnr(date_array[2])
mnr = string.format("%02d", mnr)
mnr = string.format("%02d", mnr)
return date_array[2] .. "/" .. mnr .. "/" .. day
return date_array[3] .. "/" .. mnr .. "/" .. day
end
end
end
end


return p
return p