Modul:DateTime: Unterschied zwischen den Versionen

165 Bytes hinzugefügt ,  5. Februar 2014
updates
(update)
(updates)
Zeile 1: Zeile 1:
--[=[ 2014-02-01
--[=[ 2014-02-04
Date and time utilities
Date and time utilities
]=]
]=]
Zeile 8: Zeile 8:
local DateTime
local DateTime
local Parser    = { }
local Parser    = { }
local Private    = { }
local Prototypes = { }
local Prototypes = { }
local World      = { slang      = "en",
local World      = { slang      = "en",
Zeile 120: Zeile 121:
     --    string or false, if failed
     --    string or false, if failed
     local r
     local r
     Prototypes.foreign()
     Private.foreign()
     r = Prototypes.factory( assign, alien )
     r = Private.factory( assign, alien )
     if type( r ) == "table" then
     if type( r ) == "table" then
         local meta = { }
         local meta = { }
Zeile 146: Zeile 147:
                           end
                           end
         r              = { [ s ] = r }
         r              = { [ s ] = r }
         r.fair          = function ( self, ... )
         r.fair          = function ( ... )
                               return Prototypes.fair( self, ... )
                               return Prototypes.fair( ... )
                           end
                           end
         r.format        = function ( ... )
         r.format        = function ( ... )
                               return Prototypes.format( r[ s ], ... )
                               return Prototypes.format( ... )
                           end
                           end
         setmetatable( r, meta )
         setmetatable( r, meta )
Zeile 742: Zeile 743:




Prototypes.factory = function ( assign, alien )
Private.factory = function ( assign, alien )
     -- Create DateTime table (constructor)
     -- Create DateTime table (constructor)
     -- Parameter:
     -- Parameter:
Zeile 763: Zeile 764:
                                                             "c" )
                                                             "c" )
         end
         end
         l, r = pcall( Prototypes.fetch, stamp, slang )
         l, r = pcall( Private.fetch, stamp, slang )
     end
     end
     if l  and  type( r ) == "table" then
     if l  and  type( r ) == "table" then
Zeile 771: Zeile 772:
     end
     end
     return r
     return r
end -- Prototypes.factory()
end -- Private.factory()
 
 
 
Private.fetch = function ( analyse, alien )
    -- Retrieve object from string
    -- Parameter:
    --    analyse  -- string to be interpreted
    --    alien    -- string with language code, or nil
    -- Returns:
    --    table, if parsed
    --    false, if invalid text format
    --    string, if serious error (args)
    local r
    if type( analyse ) == "string" then
        r =  analyse:gsub( " ", " " )
                    :gsub( " ", " " )
                    :gsub( " ", " " )
                    :gsub( Nbsp, " " )
                    :gsub( Tab, " " )
                    :gsub( "  +", " " )
        r = mw.text.trim( r )
        if r == "" then
            r = { }
        else
            local slang = ( alien or "" )
            if slang == "" then
                slang = "en"
            else
                local s = slang:match( "^(%a+)%-" )
                if s then
                    slang = s
                end
            end
            slang = slang:lower()
            if slang == "en" or slang == "de" then
                local l
                l, r = pcall( Parser.GermanEnglish, r )
                if l and r then
                    if not Prototypes.fair( r ) then
                        r = false
                    end
                end
            else
                r = "unknown language"
            end
        end
    else
        r = "bad type"
    end
    return r
end -- Private.fetch()
 
 
 
Private.foreign =  function ()
    -- Retrieve localization submodule
    if not World.localization then
        local l, d = pcall( mw.loadData, "Module:DateTime/local" )
        if l then
            if d.slang then
                World.slang = d.slang
            end
            for k, v in pairs( d ) do
                if World[ k ].en then
                    local part = World[ k ]
                    for subk, subv in pairs( v ) do
                        part[ subk ] = subv
                    end -- for k, v
                else
                    World[ k ] = v
                end
            end -- for k, v
        end
        World.localization = true
    end
end -- Private.foreign()




Zeile 915: Zeile 992:
     return r
     return r
end -- Prototypes.fair()
end -- Prototypes.fair()
Prototypes.fetch = function ( analyse, alien )
    -- Retrieve object from string
    -- Parameter:
    --    analyse  -- string to be interpreted
    --    alien    -- string with language code, or nil
    -- Returns:
    --    table, if parsed
    --    false, if invalid text format
    --    string, if serious error (args)
    local r
    if type( analyse ) == "string" then
        r =  analyse:gsub( " ", " " )
                    :gsub( " ", " " )
                    :gsub( " ", " " )
                    :gsub( Nbsp, " " )
                    :gsub( Tab, " " )
                    :gsub( "  +", " " )
        r = mw.text.trim( r )
        if r == "" then
            r = { }
        else
            local slang = ( alien or "" )
            if slang == "" then
                slang = "en"
            else
                local s = slang:match( "^(%a+)%-" )
                if s then
                    slang = s
                end
            end
            slang = slang:lower()
            if slang == "en" or slang == "de" then
                local l
                l, r = pcall( Parser.GermanEnglish, r )
                if l and r then
                    if not Prototypes.fair( r ) then
                        r = false
                    end
                end
            else
                r = "unknown language"
            end
        end
    else
        r = "bad type"
    end
    return r
end -- Prototypes.fetch()
Prototypes.foreign =  function ()
    -- Retrieve localization submodule
    if not World.localization then
        local l, d = pcall( mw.loadData, "Module:DateTime/local" )
        if l then
            if d.slang then
                World.slang = d.slang
            end
            for k, v in pairs( d ) do
                if World[ k ].en then
                    local part = World[ k ]
                    for subk, subv in pairs( v ) do
                        part[ subk ] = subv
                    end -- for k, v
                else
                    World[ k ] = v
                end
            end -- for k, v
        end
        World.localization = true
    end
end -- Prototypes.foreign()




Zeile 1.245: Zeile 1.246:
local p = { }
local p = { }


function p.format( frame )
function p.test( args )
     local r = ( frame.args[ 1 ] or "now" )
     local r
     if r == "now" then
    local o = DateTime( args[ 1 ], "de" )
        r = frame:callParserFunction( "#timel", "c" )
     if type( o ) == "table" then
    end
         local spec  = args[ 2 ]
    local l, o = pcall( DateTime, r, "de" )
         local slang = args[ 3 ]
    if l then
         local spec  = frame.args[ 2 ]
         local slang = frame.args[ 3 ]
         if spec then
         if spec then
             spec = trim( spec )
             spec = mw.text.trim( spec )
         end
         end
         if slang then
         if slang then
             slang = trim( slang )
             slang = mw.text.trim( slang )
         end
         end
         if type( o ) == "table" then
         r = o:format( spec, slang )
             l, r = pcall( o, format, spec, slang )
    else
        r = ( args.noerror or "0" )
        if r == "0" then
             r = fault( "Format nicht erkannt" )
         else
         else
             l = ( frame.args.noerror or "0" )
             r = ""
            if l == "0" then
                r = fault( "Format nicht erkannt" )
            else
                r = ""
            end
         end
         end
     else
     end
         r = o
    return r
end -- test
 
 
 
function p.format( frame )
    local l, r
    local v = { frame.args[ 1 ],
                frame.args[ 2 ],
                frame.args[ 3 ],
                noerror = frame.args.noerror }
    if not v[ 1 ]  or  v[ 1 ] == "now" then
         v[ 1 ] = frame:callParserFunction( "#timel", "c" )
    end
    l, r = pcall( p.test, v )
    if not l then
        r = fault( r )
     end
     end
     return r
     return r
end -- format
end -- format


p.DateTime = function ( ... )
p.DateTime = function ( ... )
Anonymer Benutzer