Modul:DateTime: Unterschied zwischen den Versionen
updates
(update) |
(updates) |
||
Zeile 1: | Zeile 1: | ||
--[=[ 2014-02- | --[=[ 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 | ||
Private.foreign() | |||
r = | 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 ( | r.fair = function ( ... ) | ||
return Prototypes.fair( | return Prototypes.fair( ... ) | ||
end | end | ||
r.format = function ( ... ) | r.format = function ( ... ) | ||
return Prototypes.format( | return Prototypes.format( ... ) | ||
end | end | ||
setmetatable( r, meta ) | setmetatable( r, meta ) | ||
Zeile 742: | Zeile 743: | ||
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( | 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. | 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() | ||
Zeile 1.245: | Zeile 1.246: | ||
local p = { } | local p = { } | ||
function p. | function p.test( args ) | ||
local r = ( | local r | ||
if | local o = DateTime( args[ 1 ], "de" ) | ||
if type( o ) == "table" then | |||
local spec = args[ 2 ] | |||
local slang = args[ 3 ] | |||
local spec = | |||
local slang = | |||
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 | ||
r = o:format( spec, slang ) | |||
else | |||
r = ( args.noerror or "0" ) | |||
if r == "0" then | |||
r = fault( "Format nicht erkannt" ) | |||
else | else | ||
r = "" | |||
end | end | ||
end | |||
r = | 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 ( ... ) |