Modul:DateTime: Unterschied zwischen den Versionen
update
(updates) |
(update) |
||
Zeile 1: | Zeile 1: | ||
--[=[ 2014-03- | --[=[ 2014-03-16 | ||
Date and time utilities | Date and time utilities | ||
]=] | ]=] | ||
Zeile 44: | Zeile 44: | ||
[ "Sep" ] = 9 | [ "Sep" ] = 9 | ||
} | } | ||
World.months4.en = { [ 6 ] = | World.months4.en = { [ 6 ] = true, | ||
[ 7 ] = | [ 7 ] = true } | ||
World.templates = { [ "ISO" ] = | World.templates = { [ "ISO" ] = | ||
{ spec = "Y-m-d", | { spec = "Y-m-d", | ||
Zeile 152: | Zeile 152: | ||
r.fair = function ( ... ) | r.fair = function ( ... ) | ||
return Prototypes.fair( ... ) | return Prototypes.fair( ... ) | ||
end | |||
r.figure = function ( ... ) | |||
return Prototypes.figure( ... ) | |||
end | |||
r.first = function ( ... ) | |||
return Prototypes.first( ... ) | |||
end | end | ||
r.format = function ( ... ) | r.format = function ( ... ) | ||
return Prototypes.format( ... ) | return Prototypes.format( ... ) | ||
end | |||
r.full = function ( ... ) | |||
return Prototypes.full( ... ) | |||
end | end | ||
setmetatable( r, meta ) | setmetatable( r, meta ) | ||
Zeile 610: | Zeile 619: | ||
-- analyse -- string, starting with year | -- analyse -- string, starting with year | ||
-- assign -- table | -- assign -- table | ||
-- Returns: | -- Returns: | ||
-- table, extended if parsed | -- table, extended if parsed | ||
Zeile 806: | Zeile 814: | ||
r = analyse:gsub( " ", " " ) | r = analyse:gsub( " ", " " ) | ||
:gsub( " ", " " ) | :gsub( " ", " " ) | ||
:gsub( "&#x[aA]0;", " " ) | |||
:gsub( " ", " " ) | :gsub( " ", " " ) | ||
:gsub( Nbsp, " " ) | :gsub( Nbsp, " " ) | ||
:gsub( Tab, " " ) | :gsub( Tab, " " ) | ||
:gsub( " +", " " ) | :gsub( " +", " " ) | ||
:gsub( "%[%[", "" ) | |||
:gsub( "%]%]", "" ) | |||
r = mw.text.trim( r ) | r = mw.text.trim( r ) | ||
if r == "" then | if r == "" then | ||
Zeile 873: | Zeile 884: | ||
-- Check formal validity of table | -- Check formal validity of table | ||
-- Parameter: | -- Parameter: | ||
-- self -- table to be checked | -- self -- table, to be checked | ||
-- access -- string or nil, single item to be checked | -- access -- string or nil, single item to be checked | ||
-- assign -- single access value to be checked | -- assign -- single access value to be checked | ||
Zeile 978: | Zeile 989: | ||
end | end | ||
end | end | ||
elseif access == "lang" then | |||
r = ( type( assign ) == "string" ) | |||
if r then | |||
r = assign:match( "^%l%l%l?-?%a*$" ) | |||
end | |||
elseif access == "london" then | |||
r = ( type( assign ) == "boolean" ) | |||
end | end | ||
end | end | ||
Zeile 1.013: | Zeile 1.031: | ||
Prototypes.format = function ( self, ask, | Prototypes.figure = function ( self, assign ) | ||
-- Assign month by name | |||
-- Parameter: | |||
-- self -- table, to be filled | |||
-- assign -- string, with month name | |||
-- Returns: | |||
-- number 1...12, if valid; false, if not | |||
local r = false | |||
if type( self ) == "table" and type( assign ) == "string" then | |||
r = Parser.monthNumber( assign ) | |||
if r then | |||
self.month = r | |||
end | |||
end | |||
return r | |||
end -- Prototypes.figure() | |||
Prototypes.first = function ( self ) | |||
-- Retrieve abbreviated month name in current language | |||
-- Parameter: | |||
-- self -- table, to be evaluated | |||
-- Returns: | |||
-- string, if defined; false, if not | |||
local r | |||
if type( self ) == "table" and self.month then | |||
local slang = ( self.lang or World.slang ) | |||
r = World.monthsLong[ slang ] | |||
if r then | |||
local brief = World.monthsAbbr[ slang ] | |||
r = r[ self.month ] | |||
if brief then | |||
local ex = brief[ self.month ] | |||
local s = brief.suffix | |||
if ex then | |||
r = ex[ 2 ] | |||
else | |||
local n = brief.n or 3 | |||
r = mw.ustring.sub( r, 1, n ) | |||
end | |||
if s then | |||
r = r .. s | |||
end | |||
end | |||
end | |||
else | |||
r = false | |||
end | |||
return r | |||
end -- Prototypes.first() | |||
Prototypes.format = function ( self, ask, adapt ) | |||
-- Format object as string | -- Format object as string | ||
-- Parameter: | -- Parameter: | ||
-- self -- table, with numbers etc. | -- self -- table, with numbers etc. | ||
-- ask -- string, format spec, or nil | -- ask -- string, format spec, or nil | ||
-- | -- adapt -- table, with options, or nil | ||
-- .lang -- string, with particular language code | |||
-- .lonely -- true: permit lonely hour | |||
-- Returns: | -- Returns: | ||
-- string, or false, if invalid | -- string, or false, if invalid | ||
local r = false | local r = false | ||
if type( self ) == "table" then | if type( self ) == "table" then | ||
local | local opts = { lang = self.lang } | ||
local babel | |||
if type( adapt ) == "table" then | |||
if type( adapt.lang ) == "string" then | |||
opts.lang = adapt.lang | |||
end | |||
opts.london = adapt.london | |||
opts.lonely = adapt.lonely | |||
end | |||
babel = mw.language.new( opts.lang ) | |||
if babel then | if babel then | ||
local show | local shift, show, stamp, suffix, limit4, locally | ||
if self.month then | if self.month then | ||
stamp = World.monthsLong.en[ self.month ] | stamp = World.monthsLong.en[ self.month ] | ||
Zeile 1.037: | Zeile 1.116: | ||
if self.dom then | if self.dom then | ||
stamp = string.format( "%d %s", self.dom, stamp ) | stamp = string.format( "%d %s", self.dom, stamp ) | ||
end | |||
if ask and ask:find( "Mon4" ) then | |||
local mon4 = World.months4[ opts.lang ] | |||
if mon4 then | |||
if mon4[ self.month ] then | |||
limit4 = true | |||
end | |||
end | |||
end | end | ||
elseif self.year then | elseif self.year then | ||
Zeile 1.060: | Zeile 1.147: | ||
end | end | ||
end | end | ||
show, suffix = World.templates.formatter( self, ask, | show, suffix = World.templates.formatter( self, ask, opts ) | ||
if | if limit4 then | ||
show = show:gsub( "M", "F" ) | |||
end | |||
if type( opts.london ) == "boolean" then | |||
locally = not opts.london | |||
else | |||
locally = true | locally = true | ||
end | end | ||
r = babel:formatDate( show, stamp, locally ) | r = babel:formatDate( show, stamp, locally ) | ||
r = r:gsub( " $", "" ) | |||
if self.year and self.year < 1000 then | if self.year and self.year < 1000 then | ||
r = r:gsub( string.format( "%04d", self.year ), | r = r:gsub( string.format( "%04d", self.year ), | ||
tostring( self.year ) ) | tostring( self.year ) ) | ||
end | end | ||
if self.month and | if self.month and show:find( "M" ) then | ||
local | local m = World.monthsAbbr[ opts.lang ] | ||
if | if m then | ||
local ex = m[ self.month ] | |||
if | if ex then | ||
local | local stop = m.suffix | ||
r = | local std = ex[ 1 ] | ||
local shift = ex[ 2 ] | |||
if stop then | |||
std = string.format( "%s%%%s", | |||
std, stop ) | |||
shift = string.format( "%s%%%s", | |||
shift, stop ) | |||
end | |||
r = mw.ustring.gsub( r, stop, shift ) | |||
end | end | ||
end | end | ||
Zeile 1.091: | Zeile 1.190: | ||
World.templates.formatter = function ( assigned, ask, | Prototypes.full = function ( self ) | ||
-- Retrieve month name in current language | |||
-- Parameter: | |||
-- self -- table, to be evaluated | |||
-- Returns: | |||
-- string, if defined; false, if not | |||
local r | |||
if type( self ) == "table" and self.month then | |||
local slang = ( self.lang or World.slang ) | |||
r = World.monthsLong[ slang ] | |||
if r then | |||
r = r[ self.month ] | |||
end | |||
else | |||
r = false | |||
end | |||
return r | |||
end -- Prototypes.full() | |||
World.templates.formatter = function ( assigned, ask, adapt ) | |||
-- Retrieve format specification string | -- Retrieve format specification string | ||
-- Parameter: | -- Parameter: | ||
-- assigned -- table, with numbers etc. | -- assigned -- table, with numbers etc. | ||
-- ask -- string, format spec, or nil | -- ask -- string, format spec, or nil | ||
-- | -- adapt -- table, with options | ||
-- .lang -- string, with particular language code | |||
-- .lonely -- true: permit lonely hour | |||
-- Returns: | -- Returns: | ||
-- 1 -- string | -- 1 -- string | ||
Zeile 1.107: | Zeile 1.229: | ||
r1 = ask | r1 = ask | ||
if not template then | if not template then | ||
local slang = ( | local slang = ( adapt.lang or assigned.lang or World.slang ) | ||
local tmp = World.templates[ slang ] | local tmp = World.templates[ slang ] | ||
if tmp then | if tmp then | ||
Zeile 1.131: | Zeile 1.253: | ||
end | end | ||
if template.lift then | if template.lift then | ||
local stamp = false | local stamp = false | ||
local low = ( ask == "ISO" or ask == "ISO-T" ) | local low = ( ask == "ISO" or ask == "ISO-T" ) | ||
if | if assigned.hour then | ||
if assigned.min then | |||
stamp = "H:i" | |||
if assigned.sec then | |||
stamp = "H:i:s" | |||
if assigned.msec then | |||
stamp = string.format( "%s.%d", | |||
stamp, | |||
assigned.msec ) | |||
end | |||
end | end | ||
elseif adapt.lonely then | |||
stamp = "H" | |||
end | end | ||
end | end | ||
if low or | if low or ask:find( "hh:mm:ss" ) then | ||
if stamp then | if stamp then | ||
r1 = string.format( "%s %s", r1, stamp ) | r1 = string.format( "%s %s", r1, stamp ) | ||
Zeile 1.153: | Zeile 1.276: | ||
end | end | ||
if stamp then | if stamp then | ||
local dewiki = ( ask == "dewiki" or | local dewiki = ( ask == "dewiki" or | ||
ask:find( "Zone$" ) ) | |||
if low or dewiki then | if low or dewiki then | ||
local scheme | local scheme | ||
Zeile 1.164: | Zeile 1.288: | ||
end | end | ||
if type ( assigned.bc ) == "boolean" then | if type ( assigned.bc ) == "boolean" then | ||
local eras = World.era[ | local eras = World.era[ adapt.lang ] or World.era.en | ||
local i | local i | ||
if not r2 then | if not r2 then | ||
Zeile 1.276: | Zeile 1.400: | ||
function p.test( args ) | function p.test( args ) | ||
local r | local r | ||
local | local obj = DateTime( args[ 1 ], "de" ) | ||
if type( | if type( obj ) == "table" then | ||
local opt | |||
local spec = args[ 2 ] | local spec = args[ 2 ] | ||
local slang = args[ 3 ] | local slang = args[ 3 ] | ||
Zeile 1.284: | Zeile 1.409: | ||
end | end | ||
if slang then | if slang then | ||
opt = { lang = mw.text.trim( slang ) } | |||
end | end | ||
r = | r = obj:format( spec, opt ) | ||
else | else | ||
r = ( args.noerror or "0" ) | r = ( args.noerror or "0" ) |