Modul:DateTime: Unterschied zwischen den Versionen
2020-09-30 - Administratoren-Anfrage: https://de.wikipedia.org/w/index.php?title=Wikipedia:Administratoren/Anfragen&diff=204106510&oldid=204105921
ICON (Diskussion | Beiträge) (Die Seite wurde neu angelegt: „local DateTime = { serial = "2018-01-24", suite = "DateTime", item = 20652535 } -- Date and time objects loca…“) |
(2020-09-30 - Administratoren-Anfrage: https://de.wikipedia.org/w/index.php?title=Wikipedia:Administratoren/Anfragen&diff=204106510&oldid=204105921) |
||
Zeile 1: | Zeile 1: | ||
local DateTime = { serial = " | local DateTime = { serial = "2020-09-30", | ||
suite = "DateTime", | suite = "DateTime", | ||
item = 20652535 } | item = 20652535 } | ||
-- Date and time objects | |||
local Failsafe = DateTime | |||
local GlobalMod = DateTime | |||
local Calc = { } | local Calc = { } | ||
local Meta = { } | local Meta = { } | ||
Zeile 13: | Zeile 16: | ||
months4 = { } } | months4 = { } } | ||
local MaxYear = 2099 | local MaxYear = 2099 | ||
local Frame | local Frame | ||
DateTime.char = { nbsp = mw.ustring.char( 160 ), | |||
tab = mw.ustring.char( 9 ) } | |||
World.era = { en = { "BC", "AD" } } | World.era = { en = { "BC", "AD" } } | ||
World.monthsAbbr = { en = { n = 3 } } | World.monthsAbbr = { en = { n = 3 } } | ||
Zeile 104: | Zeile 107: | ||
HST = -1000 -- Hawaiian Standard Time | HST = -1000 -- Hawaiian Standard Time | ||
} | } | ||
local foreignModule = function ( access, advanced, append, alt, alert ) | |||
-- Fetch global module | |||
-- Precondition: | |||
-- access -- string, with name of base module | |||
-- advanced -- true, for require(); else mw.loadData() | |||
-- append -- string, with subpage part, if any; or false | |||
-- alt -- number, of wikidata item of root; or false | |||
-- alert -- true, for throwing error on data problem | |||
-- Postcondition: | |||
-- Returns whatever, probably table | |||
-- 2020-01-01 | |||
local storage = access | |||
local fun, lucky, r | |||
if advanced then | |||
fun = require | |||
else | |||
fun = mw.loadData | |||
end | |||
if append then | |||
storage = string.format( "%s/%s", storage, append ) | |||
end | |||
lucky, r = pcall( fun, "Module:" .. storage ) | |||
if not lucky then | |||
local suited | |||
GlobalMod.globalModules = GlobalMod.globalModules or { } | |||
suited = GlobalMod.globalModules[ access ] | |||
if not suited and | |||
type( alt ) == "number" and | |||
alt > 0 then | |||
suited = string.format( "Q%d", alt ) | |||
suited = mw.wikibase.getSitelink( suited ) | |||
GlobalMod.globalModules[ access ] = suited or true | |||
end | |||
if type( suited ) == "string" then | |||
storage = suited | |||
if append then | |||
storage = string.format( "%s/%s", storage, append ) | |||
end | |||
lucky, r = pcall( fun, storage ) | |||
end | |||
if not lucky and alert then | |||
error( "Missing or invalid page: " .. storage ) | |||
end | |||
end | |||
return r | |||
end -- foreignModule() | |||
Zeile 125: | Zeile 177: | ||
-- Returns: | -- Returns: | ||
-- string, HTML span | -- string, HTML span | ||
local e = mw.html.create( "span" ) | |||
:addClass( "error" ) | |||
:wikitext( a ) | |||
return tostring( e ) | |||
end -- fault() | end -- fault() | ||
Zeile 261: | Zeile 316: | ||
end -- Meta.fiat() | end -- Meta.fiat() | ||
setmetatable( DateTime, Meta.tableL ) | setmetatable( DateTime, Meta.tableL ) | ||
DateTime.serial = nil | DateTime.serial = nil | ||
Zeile 328: | Zeile 382: | ||
n = min + m | n = min + m | ||
else -- dom | else -- dom | ||
if adjust.month and adjust.year | if adjust.month and adjust.year and | ||
adjust.month >= 1 and | adjust.month >= 1 and | ||
adjust.month <= 12 and | adjust.month <= 12 and | ||
adjust.year > 1900 then | adjust.year > 1900 then | ||
max = Calc. | if n > 0 then | ||
if adjust.month == | max = Calc.final( adjust ) | ||
while n > max do | |||
n = n - max | |||
if adjust.month < 12 then | |||
adjust.month = adjust.month + 1 | |||
else | |||
adjust.month = 1 | |||
adjust.year = adjust.year + 1 | |||
end | |||
max = Calc.final( adjust ) | |||
end -- while n <= max | |||
else | |||
while n < 1 do | |||
if adjust.month == 1 then | |||
adjust.month = 12 | |||
adjust.year = adjust.year - 1 | |||
else | |||
adjust.month = adjust.month - 1 | |||
end | |||
max = Calc.final( adjust ) | |||
n = n + max | |||
end -- while n < 1 | |||
end | end | ||
end | end | ||
end | end | ||
end | |||
adjust[ s ] = n | adjust[ s ] = n | ||
end | end | ||
Zeile 355: | Zeile 418: | ||
end -- for i | end -- for i | ||
end -- Calc.fair() | end -- Calc.fair() | ||
Calc.final = function ( adjust ) | |||
-- Retrieve number of days in particular month | |||
-- Parameter: | |||
-- adjust -- table, with date specification | |||
-- Returns: | |||
-- number, of days in month | |||
local r = Calc.months[ adjust.month ] | |||
if adjust.month == 2 and | |||
( adjust.year % 4 ~= 0 or | |||
adjust.year % 400 == 0 ) then | |||
r = 28 | |||
end | |||
return r | |||
end -- Calc.final() | |||
Zeile 445: | Zeile 525: | ||
if amount <= 4 then | if amount <= 4 then | ||
r.year = tonumber( analyse ) | r.year = tonumber( analyse ) | ||
elseif | elseif amount == 14 then | ||
-- timestamp | -- timestamp | ||
r.year = tonumber( analyse:sub( 1, 4 ) ) | r.year = tonumber( analyse:sub( 1, 4 ) ) | ||
r.month = tonumber( analyse:sub( 5, | r.month = tonumber( analyse:sub( 5, 6 ) ) | ||
r.dom = tonumber( analyse:sub( 7, | r.dom = tonumber( analyse:sub( 7, 8 ) ) | ||
r.hour = tonumber( analyse:sub( 9, | r.hour = tonumber( analyse:sub( 9, 10 ) ) | ||
r.min = tonumber( analyse:sub( 11, | r.min = tonumber( analyse:sub( 11, 12 ) ) | ||
r.sec = tonumber( analyse:sub( 13, | r.sec = tonumber( analyse:sub( 13, 14 ) ) | ||
else | else | ||
r = false | r = false | ||
Zeile 673: | Zeile 753: | ||
if rS:byte( 1, 1 ) == 45 then | if rS:byte( 1, 1 ) == 45 then | ||
local suffix | local suffix | ||
s = rS:match( "^%-([ | s = rS:match( "^%-([0-3][0-9])" ) | ||
if s then | if s then | ||
n = tonumber( s ) | n = tonumber( s ) | ||
Zeile 747: | Zeile 827: | ||
n = #s2 | n = #s2 | ||
if n <= 2 and #s3 == 4 then | if n <= 2 and #s3 == 4 then | ||
rO.dom = tonumber( | rO.dom = tonumber( s2 ) | ||
rO.year = tonumber( s3 ) | rO.year = tonumber( s3 ) | ||
rO.dom2 = ( n == 2 ) | rO.dom2 = ( n == 2 ) | ||
Zeile 1.162: | Zeile 1.242: | ||
-- analyse -- string to be interpreted | -- analyse -- string to be interpreted | ||
-- alien -- string with language code, or nil | -- alien -- string with language code, or nil | ||
-- add -- | -- add -- table, with interval, or nil | ||
-- Returns: | -- Returns: | ||
-- table, if parsed | -- table, if parsed | ||
Zeile 1.169: | Zeile 1.249: | ||
local r | local r | ||
if type( analyse ) == "string" then | if type( analyse ) == "string" then | ||
local strip = mw.ustring.char( 0x5B, 0x200E, 0x200F, 0x5D ) | |||
r = analyse:gsub( " ", " " ) | r = analyse:gsub( " ", " " ) | ||
:gsub( " ", " " ) | :gsub( " ", " " ) | ||
:gsub( "&#x[aA]0;", " " ) | :gsub( "&#x[aA]0;", " " ) | ||
:gsub( " ", " " ) | :gsub( " ", " " ) | ||
:gsub( | :gsub( DateTime.char.nbsp, " " ) | ||
:gsub( | :gsub( DateTime.char.tab, " " ) | ||
:gsub( " +", " " ) | :gsub( " +", " " ) | ||
:gsub( "%[%[", "" ) | :gsub( "%[%[", "" ) | ||
:gsub( "%]%]", "" ) | :gsub( "%]%]", "" ) | ||
:gsub( strip, "" ) | |||
r = mw.text.trim( r ) | r = mw.text.trim( r ) | ||
if r == "" then | if r == "" then | ||
r = { } | r = { } | ||
else | else | ||
local slang = ( alien or "" ) | local slang = ( alien or "" ) | ||
local parser = { en = "GermanEnglish", | |||
de = "GermanEnglish", | |||
frr = "GermanEnglish", | |||
nds = "GermanEnglish" } | |||
local suitable | |||
if slang == "" then | if slang == "" then | ||
slang = "en" | slang = "en" | ||
Zeile 1.191: | Zeile 1.278: | ||
end | end | ||
end | end | ||
slang = slang:lower() | slang = slang:lower() | ||
suitable = parser[ slang ] | |||
if suitable then | |||
local l | local l | ||
l, r = pcall( Parser | l, r = pcall( Parser[ suitable ], r ) | ||
if l and r then | if l and r then | ||
if not Prototypes.fair( r ) then | if not Prototypes.fair( r ) then | ||
Zeile 1.201: | Zeile 1.289: | ||
r = Prototypes.future( r, add ) | r = Prototypes.future( r, add ) | ||
end | end | ||
else | |||
r = "invalid format" | |||
end | end | ||
else | else | ||
r = "unknown language" | r = "unknown language: " .. slang | ||
end | end | ||
end | end | ||
Zeile 1.214: | Zeile 1.304: | ||
Private. | Private.field = function ( at, ask, adapt, atleast ) | ||
-- | -- Format object as string | ||
-- Parameter: | -- Parameter: | ||
-- | -- at -- DateTime | ||
-- | -- ask -- string, with format spec, or nil | ||
-- adapt -- table, with options, or nil | |||
-- .lang -- string, with particular language code | |||
-- .london -- true: UTC output; default: local | |||
-- .lonely -- true: permit lonely hour | |||
-- atleast -- string, with default value, or nil | |||
-- Returns: | -- Returns: | ||
-- | -- string, or false, if invalid, or number for julian date | ||
local r = | local r, spec | ||
if type( ask ) == "string" then | |||
if ask:sub( 1, 1 ) == "$" then | |||
if ask:sub( 1, 11 ) == "$JulianDate" then | |||
local luxury = ( ask:sub( -2 ) == ",$" ) | |||
if ask:sub( 1, 14 ) == "$JulianDateJul" then | |||
at.legacy = true | |||
elseif ask:sub( 1, 15 ) == "$JulianDateGreg" then | |||
else | |||
at.legacy = Private.former( at ) | |||
end | |||
r = Private.fixed( at, luxury ) | |||
elseif ask:sub( 1, 11 ) == "$JulianCal$" then | |||
adapt.legacy = true | |||
spec = ask:sub( 12 ) | |||
elseif ask:sub( 1, 3 ) == "$\"$" then | |||
r = ask:sub( 4 ) | |||
else | |||
spec = ask | |||
end | |||
else | else | ||
spec = ask | |||
end | end | ||
else | else | ||
spec = false | |||
end | |||
if not r then | |||
r = Private.format( at, spec, adapt ) | |||
end | |||
return r or atleast | |||
end -- Private.field() | |||
Private.fixed = function ( at, advanced ) | |||
-- Compute julian date | |||
-- Parameter: | |||
-- at -- DateTime | |||
-- .legacy -- true: at is in Julian calendar | |||
-- advanced -- true: format long number | |||
-- Returns: | |||
-- number, or string | |||
local mM, mMY, mY, nY, r | |||
if at.year then | |||
mY = at.year * 12 | |||
else -- actually invalid | |||
mY = 0 | |||
end | |||
if at.month then | |||
mMY = at.month | |||
else | |||
mMY = 1 | |||
end | |||
mMY = mMY + 57609 | |||
if at.dom then | |||
r = at.dom | |||
else | |||
r = 1 | |||
end | end | ||
mM = ( mY + mMY ) * 0.08333333333 -- divided by 12 months | |||
nY = math.floor( mM - 1 ) | |||
r = math.floor( nY * 365.25 ) | |||
+ math.floor( ( mMY%12 + 4 ) * 30.6 ) | |||
+ r | |||
if at.legacy then | |||
-- | r = r - 32205.5 | ||
else | |||
r = r - math.floor( nY * 0.01 ) -- no leap day in century | |||
+ math.floor( nY * 0.0025 ) -- but every 400 years | |||
- 32167.5 | |||
end | |||
if at.hour then -- divided by 24 hours per day | |||
r = r + at.hour * 0.0416666666666667 | |||
else | |||
r = r + 0.5 | |||
end | |||
if at.min then -- divided by 1440 minutes per day | |||
r = r + at.min * 0.000694444444 | |||
end | |||
if at.sec then -- divided by 86400 seconds per day | |||
r = r + at.min * 0.00001157407407 | |||
end | |||
if at.bc then | |||
r = 3442406 - r | |||
if at.legacy then | |||
r = r + 3 | |||
end | end | ||
end | end | ||
end -- Private. | if advanced then | ||
local slang = ( at.lang or World.slang ) | |||
local o = mw.language.new( slang ) | |||
r = o:formatNum( r ) | |||
end | |||
return r | |||
end -- Private.fixed() | |||
Private. | Private.flow = function ( at1, at2 ) | ||
-- | -- Compare two objects | ||
-- Parameter: | -- Parameter: | ||
-- | -- at1 -- DateTime | ||
-- at2 -- DateTime | |||
-- Returns: | -- Returns: | ||
-- | -- -1, 0, 1 or nil if not comparable | ||
local | local r = 0 | ||
if at1.bc or at2.bc and at1.bc ~= at2.bc then | |||
if at1.bc then | |||
if | r = -1 | ||
else | else | ||
r = 1 | |||
end | end | ||
else | |||
local life = false | |||
local s, v1, v2 | |||
for i = 2, 10 do | |||
s = Meta.order[ i ] | |||
v1 = at1[ s ] | |||
end | v2 = at2[ s ] | ||
return r | if v1 or v2 then | ||
end -- Private. | if v1 and v2 then | ||
if v1 < v2 then | |||
r = -1 | |||
elseif v1 > v2 then | |||
r = 1 | |||
end | |||
elseif life then | |||
if v2 then | |||
r = -1 | |||
else | |||
r = 1 | |||
end | |||
else | |||
r = nil | |||
end | |||
if r ~= 0 then | |||
if at1.bc and r then | |||
r = r * -1 | |||
end | |||
break -- for i | |||
end | |||
life = true | |||
end | |||
end -- for i | |||
end | |||
return r | |||
end -- Private.flow() | |||
Private. | Private.foreign = function () | ||
-- | -- Retrieve localization submodule | ||
if not Meta.localized then | |||
local d = foreignModule( DateTime.suite, | |||
false, | |||
"local", | |||
DateTime.item ) | |||
if type( d ) == "table" then | |||
local wk | |||
if d.slang then | |||
Meta.suite = string.format( "%s %s", | |||
Meta.suite, d.slang ) | |||
World.slang = d.slang | |||
end | |||
for k, v in pairs( d ) do | |||
wk = World[ k ] | |||
if wk and wk.en then | |||
for subk, subv in pairs( v ) do | |||
wk[ subk ] = subv | |||
end -- for k, v | |||
else | |||
World[ k ] = v | |||
end | |||
end -- for k, v | |||
end | |||
Meta.localized = true | |||
end | |||
end -- Private.foreign() | |||
Private.format = function ( at, ask, adapt ) | |||
-- Format object as string | |||
-- Parameter: | -- Parameter: | ||
-- | -- at -- table, with numbers etc. | ||
-- ask -- string, format spec, or nil | |||
-- adapt -- table, with options, or nil | |||
-- .lang -- string, with particular language code | |||
-- .london -- true: UTC output; default: local | |||
-- .lonely -- true: permit lonely hour | |||
-- Returns: | -- Returns: | ||
-- string, | -- string, or not | ||
local r | local slang = at.lang or "en" | ||
if | local opts = { lang = slang } | ||
local | local babel, r | ||
if | if type( adapt ) == "table" then | ||
if type( adapt.lang ) == "string" then | |||
local i = adapt.lang:find( "-", 3, true ) | |||
if i then | |||
slang = adapt.lang:lower() | |||
opts.lang = slang:sub( 1, i - 1 ) | |||
else | |||
opts.lang = adapt.lang:lower() | |||
end | |||
end | |||
opts.london = adapt.london | |||
opts.lonely = adapt.lonely | |||
end | |||
babel = mw.language.new( opts.lang:lower() ) | |||
if babel then | |||
local shift, show, stamp, suffix, limit4, locally | |||
if at.month then | |||
stamp = World.monthsLong.en[ at.month ] | |||
if at.year then | |||
stamp = string.format( "%s %04d", stamp, at.year ) | |||
end | |||
if at.dom then | |||
stamp = string.format( "%d %s", at.dom, stamp ) | |||
end | |||
if ask and ask:find( "Mon4", 1, true ) then | |||
local mon4 = World.months4[ opts.lang:lower() ] | |||
if mon4 and mon4[ at.month ] then | |||
limit4 = true | |||
end | |||
end | |||
elseif at.year then | |||
stamp = string.format( "%04d", at.year ) | |||
end | end | ||
if | if at.hour then | ||
if stamp then | |||
stamp = stamp .. " " | |||
else | |||
stamp = "" | |||
end | |||
stamp = string.format( "%s%02d:", stamp, at.hour ) | |||
if at.min then | |||
stamp = string.format( "%s%02d", stamp, at.min ) | |||
if at.sec then | |||
stamp = string.format( "%s:%02d", | |||
stamp, at.sec ) | |||
if at.msec then | |||
stamp = string.format( "%s.%03d", | |||
stamp, at.msec ) | |||
if at.mysec then | |||
stamp = string.format( "%s%03d", | |||
stamp, | |||
at.mysec ) | |||
end | |||
end | |||
end | |||
else | else | ||
stamp = stamp .. "00" | |||
end | end | ||
if at.zone then | |||
stamp = stamp .. World.zones.formatter( at, "+-" ) | |||
end | |||
end | |||
show, suffix = World.templates.formatter( at, ask, opts ) | |||
if limit4 then | |||
show = show:gsub( "M", "F" ) | |||
end | end | ||
if | if type( opts.london ) == "boolean" then | ||
locally = not opts.london | |||
else | |||
locally = true | |||
end | end | ||
end | r = babel:formatDate( show, stamp, locally ) | ||
r = r:gsub( " $", "" ) | |||
if at.year and at.year < 1000 then | |||
r = r:gsub( string.format( "%04d", at.year ), | |||
tostring( at.year ) ) | |||
end | |||
if at.month then | |||
local bucket, m, suite, x | |||
if show:find( "F", 1, true ) then | |||
suite = "monthsLong" | |||
elseif show:find( "M", 1, true ) then | |||
suite = "monthsAbbr" | |||
end | |||
bucket = World[ suite ] | |||
if bucket then | |||
m = bucket[ opts.lang:lower() ] | |||
if slang then | |||
x = bucket[ slang:lower() ] | |||
end | |||
if m then | |||
local base = m[ at.month ] | |||
local ex | |||
if x then | |||
ex = x[ at.month ] | |||
end | |||
if suite == "monthsAbbr" then | |||
local stop | |||
if ex then | |||
stop = x.suffix | |||
base = ex | |||
else | |||
stop = m.suffix | |||
end | |||
if base and stop then | |||
local shift, std | |||
std = string.format( "%s%%%s", | |||
base[ 1 ], stop ) | |||
shift = string.format( "%s%s", | |||
base[ 2 ], stop ) | |||
r = mw.ustring.gsub( r, std, shift ) | |||
end | |||
elseif suite == "monthsLong" then | |||
if base and ex then | |||
r = mw.ustring.gsub( r, base, ex ) | |||
end | |||
end | |||
end | |||
end | |||
end | |||
if suffix then | |||
r = r .. suffix | |||
end | |||
end | |||
return r | return r | ||
end -- Private. | end -- Private.format() | ||
Private.former = function ( at ) | |||
-- | -- Analyze whether Julian calendar | ||
-- Parameter: | -- Parameter: | ||
-- | -- at -- table, to be evaluated | ||
-- Returns: | -- Returns: | ||
-- | -- true, i | ||
local r = | local r | ||
if at.year then | |||
if at.year < 1582 then | |||
r = true | |||
elseif at.year == 1582 then | |||
if at.month then | |||
if at.month < 10 then | |||
r = true | |||
elseif at.month == 10 then | |||
r = ( at.dom <= 15 ) | |||
end | |||
end | |||
end | |||
end | |||
return r | return r | ||
end -- | end -- Private.former() | ||
Private.from = function ( attempt ) | |||
-- | -- Create valid raw table from arbitrary table | ||
-- Parameter: | -- Parameter: | ||
-- | -- attempt -- table, to be evaluated | ||
-- Returns: | -- Returns: | ||
-- | -- table, with valid components, or nil | ||
local r = ( type( | local data = { } | ||
if | local r | ||
for k, v in pairs( Meta.components ) do | |||
if v then | |||
v = ( type( attempt[ k ] ) == v ) | |||
else | |||
v = true | |||
end | |||
if v then | |||
data[ k ] = attempt[ k ] | |||
end | |||
end -- for k, v | |||
if Prototypes.fair( data ) then | |||
r = data | |||
end | |||
local | return r | ||
end -- Private.from() | |||
Private.future = function ( add ) | |||
-- Normalize move interval | |||
-- Parameter: | |||
-- add -- string or number, to be added | |||
-- Returns: | |||
-- table, with shift, or false/nil | |||
local r | |||
if add then | |||
local s = type( add ) | |||
if s == "string" and add:match( "^%s*[+-]?%d+%.?%d*%s*$" ) then | |||
r = tonumber( add ) | |||
s = "number" | |||
end | |||
if s == "number" then | |||
if r == 0 then | |||
r = false | |||
else | |||
r = string.format( "%d second", r or add ) | |||
end | |||
elseif s == "string" then | |||
r = add | |||
else | |||
r = false | |||
end | |||
if r then | |||
r = Calc.future( r ) | |||
end | |||
end | |||
return r | |||
end -- Private.future() | |||
local | |||
Prototypes.clone = function ( self ) | |||
-- Clone object | |||
-- Parameter: | |||
-- self -- table, with object, to be cloned | |||
-- Returns: | |||
-- table, with object | |||
local r = { [ Meta.signature ] = self[ Meta.signature ] } | |||
setmetatable( r, Meta.tableI ) | |||
return r | |||
end -- Prototypes.clone() | |||
Prototypes.failsafe = function ( self, atleast ) | |||
-- Retrieve versioning and check for compliance | |||
-- Precondition: | |||
-- self -- table, or not, with DateTime object, unused | |||
-- atleast -- string, with required version | |||
-- or "wikidata" or "~" or "@" or false | |||
-- Postcondition: | |||
-- Returns string -- with queried version/item, also if problem | |||
-- false -- if appropriate | |||
-- 2020-08-17 | |||
local since = atleast | |||
local last = ( since == "~" ) | |||
local linked = ( since == "@" ) | |||
local link = ( since == "item" ) | |||
local r | |||
if last or link or linked or since == "wikidata" then | |||
local item = Meta.item | |||
since = false | |||
if type( item ) == "number" and item > 0 then | |||
local suited = string.format( "Q%d", item ) | |||
if linkedlink then | |||
r = suited | |||
else | |||
local entity = mw.wikibase.getEntity( suited ) | |||
if type( entity ) == "table" then | |||
local seek = Failsafe.serialProperty or "P348" | |||
local vsn = entity:formatPropertyValues( seek ) | |||
if type( vsn ) == "table" and | |||
type( vsn.value ) == "string" and | |||
vsn.value ~= "" then | |||
if last and | |||
vsn.value == ( Meta.serial or | |||
DateTime.serial ) then | |||
r = false | |||
elseif linked then | |||
if mw.title.getCurrentTitle().prefixedText | |||
== mw.wikibase.getSitelink( suited ) then | |||
r = false | |||
else | |||
r = suited | |||
end | end | ||
else | |||
r = vsn.value | |||
end | |||
if last then | |||
r = false | |||
else | |||
r = vsn.value | |||
end | end | ||
end | end | ||
end | end | ||
end | |||
end | end | ||
end | |||
if type( r ) == "nil" then | |||
if not since or since <= Meta.serial then | |||
r = Meta.serial | |||
else | |||
r = false | |||
end | |||
end | |||
return r | |||
end -- Prototypes.failsafe() | |||
Prototypes.fair = function ( self, access, assign ) | |||
-- Check formal validity of table | |||
-- Parameter: | |||
-- self -- table, to be checked | |||
-- access -- string or nil, single item to be checked | |||
-- assign -- single access value to be checked | |||
-- Returns: | |||
local | -- true, if valid; false, if not | ||
if | local r = ( type( self ) == "table" ) | ||
if r then | |||
local defs = { year = { max = MaxYear }, | |||
month = { min = 1, | |||
max = 12 }, | |||
week = { min = 1, | |||
max = 53 }, | |||
dom = { min = 1, | |||
max = 31 }, | |||
hour = { max = 23 }, | |||
min = { max = 59 }, | |||
sec = { max = 61 }, | |||
msec = { max = 999 }, | |||
mysec = { max = 999 } | |||
} | |||
local fNum = | |||
function ( k, v ) | |||
local ret = true | |||
local dk = defs[ k ] | |||
if dk then | |||
if type( dk.max ) == "number" then | |||
ret = ( type( v ) == "number" ) | |||
if ret then | |||
local min | |||
if dk.min then | |||
min = dk.min | |||
else | |||
min = 0 | |||
end | |||
ret = ( v >= min and v <= dk.max | |||
and math.floor( v ) == v ) | |||
if ret and dk.f then | |||
ret = dk.f( v ) | |||
end | |||
end | end | ||
end | end | ||
end | end | ||
end | return ret | ||
end -- fNum() | |||
if self.bc then | |||
defs.year.max = 999999 | |||
end | end | ||
defs.dom.f = | |||
function () | |||
local ret | |||
local d | |||
if access == "dom" then | |||
d = assign | |||
else | |||
d = self.dom | |||
end | |||
if d then | |||
ret = ( d <= 28 ) | |||
if not ret then | |||
local m | |||
if access == "month" then | |||
m = assign | |||
else | |||
m = self.month | |||
end | |||
if m then | |||
ret = ( d <= Calc.months[ m ] ) | |||
if ret then | |||
local y | |||
if access == "year" then | |||
y = assign | |||
else | |||
y = self.year | |||
end | |||
if d == 29 and m == 2 and y then | |||
if y % 4 ~= 0 or | |||
( y % 100 == 0 and | |||
y % 400 ~= 0 ) then | |||
ret = false | |||
end | |||
end | |||
end | |||
end | |||
end | |||
else | else | ||
ret = true | |||
end | end | ||
return ret | |||
end -- defs.dom.f() | |||
defs.sec.f = | |||
function () | |||
local ret | |||
local second | |||
if access == "sec" then | |||
second = assign | |||
else | |||
second = self.sec | |||
end | |||
if second then | |||
ret = ( second <= 59 ) | |||
if not ret and self.leap then | |||
ret = true | |||
end | |||
end | |||
return ret | |||
end -- defs.sec.f() | |||
if access or assign then | |||
r = ( type( access ) == "string" ) | |||
if r then | |||
local def = defs[ access ] | |||
if def then | |||
r = fNum( access, assign ) | |||
if r then | |||
if def == "dom" or | |||
def == "month" or | |||
def == "year" then | |||
r = defs.dom.f() | |||
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 | |||
else | |||
local life = false | |||
local leak = false | |||
local s, v | |||
for i = 1, 10 do | |||
s = Meta.order[ i ] | |||
v = self[ s ] | |||
if v then | |||
if not life and leak then | |||
-- gap detected | |||
r = false | |||
break | |||
else | |||
if not fNum( s, v ) then | |||
if | r = false | ||
break -- for i | |||
if r | end | ||
life = true | |||
elseif | leak = true | ||
end | |||
elseif i == 3 then | |||
if not self.week then | |||
life = false | |||
end | end | ||
elseif i ~= 4 then | |||
life = false | |||
end | end | ||
end -- for i | |||
if self.week and ( self.month or self.dom ) then | |||
r = false | |||
end | end | ||
end | end | ||
end | end | ||
return r | return r | ||
end -- Prototypes. | end -- Prototypes.fair() | ||
Prototypes. | Prototypes.figure = function ( self, assign ) | ||
-- | -- Assign month by name | ||
-- Parameter: | -- Parameter: | ||
-- self | -- self -- table, to be filled | ||
-- | -- assign -- string, with month name | ||
-- Returns: | -- Returns: | ||
-- | -- number 1...12, if valid; false, if not | ||
local r = false | local r = false | ||
if type( self ) == "table" | if type( self ) == "table" and type( assign ) == "string" then | ||
r = Parser.monthNumber( assign ) | |||
if r then | |||
self.month = r | |||
end | 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 ] | |||
if | |||
else | else | ||
local n = brief.n or 3 | |||
r = mw.ustring.sub( r, 1, n ) | |||
end | end | ||
if | if s then | ||
r = r .. s | |||
end | end | ||
end | end | ||
end | |||
else | |||
r = false | |||
end | end | ||
return r | return r | ||
end -- Prototypes. | end -- Prototypes.first() | ||
Prototypes. | Prototypes.fix = function ( self ) | ||
-- | -- Adapt this object to local time if no explicit zone given | ||
-- Parameter: | -- Parameter: | ||
-- self -- table, | -- self -- table, with numbers etc. | ||
if type( self ) == "table" and | |||
not self.zone then | |||
local seconds = Prototypes.format( self, "Z" ) | |||
if type( self ) == "table" and | Prototypes.future( self, - tonumber( seconds ) ) | ||
local | end | ||
end -- Prototypes.fix() | |||
Prototypes.flow = function ( self, another, assert ) | |||
-- Compare this object with another timestamp | |||
end | |||
end -- Prototypes. | |||
Prototypes. | |||
-- | |||
-- Parameter: | -- Parameter: | ||
-- self | -- self -- table, with numbers etc. | ||
-- | -- another -- DateTime or string or nil (now) | ||
-- | -- assert -- nil, or string with operator | ||
-- "lt", "le", "eq", "ne", "ge", "gt", | |||
-- "<", "<=", "==", "~=", "<>", ">=", "=>", ">" | |||
-- Returns: | -- Returns: | ||
-- | -- if assert: true or false | ||
local | -- else: -1, 0, 1 | ||
-- nil if invalid | |||
local base, other, r | |||
if type( self ) == "table" then | if type( self ) == "table" then | ||
base = self | |||
other = another | |||
elseif type( | elseif type( another ) == "table" then | ||
base = another | |||
other = self | |||
end | end | ||
if | if base then | ||
if type( other ) ~= "table" then | |||
other = Meta.fiat( other ) | |||
end | |||
if | if type( other ) == "table" then | ||
r = Private.flow( base, other ) | |||
if r and type( assert ) == "string" then | |||
local trsl = { lt = "<", | |||
["<"] = "<", | |||
le = "<=", | |||
["<="] = "<=", | |||
eq = "=", | |||
["=="] = "=", | |||
ne = "<>", | |||
["<>"] = "<>", | |||
["~="] = "<>", | |||
ge = ">=", | |||
[">="] = ">=", | |||
["=>"] = ">=", | |||
gt = ">", | |||
[">"] = ">" } | |||
local same = trsl[ assert:lower() ] | |||
if same then | |||
local s = "=" | |||
if r < 0 then | |||
s = "<" | |||
elseif r > 0 then | |||
s = ">" | |||
end | |||
r = ( same:find( s, 1, true ) ~= nil ) | |||
else | |||
r = nil | |||
end | |||
end | |||
end | end | ||
end | end | ||
return r | return r | ||
end -- Prototypes. | end -- Prototypes.flow() | ||
Prototypes. | Prototypes.format = function ( self, ask, adapt ) | ||
-- | -- Format object as string | ||
-- Parameter: | -- Parameter: | ||
-- self -- table, | -- self -- table, with numbers etc. | ||
-- ask -- string, format spec, or nil | |||
-- table, with multiple formats | |||
-- string may contain multiple formats joined by "|||" | |||
-- adapt -- table, with options, or nil | |||
-- .lang -- string, with particular language code | |||
-- .london -- true: UTC output; default: local | |||
-- .lonely -- true: permit lonely hour | |||
-- Returns: | -- Returns: | ||
-- string | -- string, or false, if invalid, or number for julian date | ||
local | local r | ||
if type( self ) == "table" then | |||
local s = type( ask ) | |||
local poly | |||
if s == "string" and ask:find( "|||", 1, true ) then | |||
poly = mw.text.split( ask, "|||" ) | |||
elseif s == "table" then | |||
poly = ask | |||
end | |||
if poly then | |||
r = "" | |||
for i = 1, #poly do | |||
r = r .. Private.field( self, poly[ i ], adapt ) | |||
end -- for i | |||
else | |||
r = Private.field( self, ask, adapt ) | |||
end | |||
end | |||
return r or false | |||
end -- Prototypes.format() | |||
if | |||
r = r | |||
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 | else | ||
r = false | |||
end | end | ||
return r | return r | ||
end -- Prototypes. | end -- Prototypes.full() | ||
Prototypes. | Prototypes.future = function ( self, add, allocate ) | ||
-- | -- Relative move by interval | ||
-- Parameter: | -- Parameter: | ||
-- self | -- self -- table, to be used as base | ||
-- add -- string or number, to be added | |||
-- allocate -- true, if a clone shall be returned | |||
-- Returns: | -- Returns: | ||
-- table, | -- table, with shift | ||
local r | local r, raw, rel, shift | ||
if type( self ) == "table" then | if type( self ) == "table" then | ||
r = self[ Meta.signature ] | r = self | ||
shift = add | |||
elseif type( add ) == "table" then | |||
r = add | |||
shift = self | |||
end | |||
if r then | |||
if r[ Meta.signature ] then | |||
raw = r[ Meta.signature ] | |||
else | |||
raw = r | |||
end | |||
if type( shift ) == "table" then | |||
rel = shift | |||
else | |||
rel = Private.future( shift ) | |||
end | |||
end | end | ||
if raw and rel then | |||
if allocate then | |||
r = Prototypes.clone( r ) | |||
raw = r[ Meta.signature ] | |||
end | end | ||
for k, v in pairs( rel ) do | |||
raw[ k ] = ( raw[ k ] or 0 ) + v | |||
end -- for k, v | |||
Calc.fair( raw ) | |||
r[ Meta.signature ] = raw | |||
end | end | ||
return r | |||
end -- Prototypes.future() | |||
Prototypes.tostring = function ( self ) | |||
-- Stringify yourself | |||
return r | |||
end -- | |||
-- | |||
-- Parameter: | -- Parameter: | ||
-- | -- self -- table, to be stringified | ||
-- Returns: | -- Returns: | ||
-- 1 -- string | -- string | ||
-- 2 -- string or nil; append suffix (zone) | local dels = { false, "", "-", "-", "", "", ":", ":", ".", "" } | ||
local r1, r2 | local wids = { false, 4, 2, 2, 2, 2, 2, 2, 3, 3 } | ||
if not ask or ask == "" then | local s = "" | ||
r1 = "c" | local n, r, spec | ||
else | local f = function ( a ) | ||
local template = World.templates[ ask ] | n = self[ Meta.order[ a ] ] | ||
r1 = ask | s = s .. dels[ a ] | ||
if not template then | if n then | ||
local slang = ( adapt.lang or assigned.lang or World.slang ) | spec = string.format( "%%s%%0%dd", wids[ a ] ) | ||
local tmp = World.templates[ slang ] | s = string.format( spec, s, n ) | ||
if tmp then | end | ||
template = tmp[ ask ] | end -- f() | ||
end | for i = 2, 5 do | ||
end | f( i ) | ||
if type( template ) == "table" then | end -- for i | ||
local low = ( ask == "ISO" or ask == "ISO-T" ) | r = s | ||
r1 = template.spec | s = "" | ||
if assigned.year then | for i = 6, 10 do | ||
if not assigned.dom then | f( i ) | ||
r1 = r1:gsub( "[ .%-]?[dDjlNwz][ .,%-]*", "" ) | end -- for i | ||
:gsub( "^ ", "" ) | if s == "::." then | ||
if not assigned.month then | r = r:gsub( "%-+$", "" ) | ||
r1 = r1:gsub( "[ .%-]?[FmMnt][ .%-]*", "" ) | else | ||
if r == "--" then | |||
r = s | |||
else | |||
r = string.format( "%sT%s", r, s ) | |||
end | |||
end | |||
r = r:gsub( "%.$", "" ) | |||
if self.bc then | |||
if self.year then | |||
r = "-" .. r | |||
else | |||
r = r .. " BC" | |||
end | |||
end | |||
return r | |||
end -- Prototypes.tostring() | |||
Prototypes.valueOf = function ( self ) | |||
-- Returns yourselves primitive value (primitive table) | |||
-- Parameter: | |||
-- self -- table, to be dumped | |||
-- Returns: | |||
-- table, or false | |||
local r | |||
if type( self ) == "table" then | |||
r = self[ Meta.signature ] | |||
end | |||
return r or false | |||
end -- Prototypes.valueOf() | |||
Templates.flow = function ( frame, action ) | |||
-- Comparison invokation | |||
-- Parameter: | |||
-- frame -- object | |||
-- Returns: | |||
-- string, either "" or "1" | |||
local r | |||
local s1 = frame.args[ 1 ] | |||
local s2 = frame.args[ 2 ] | |||
if s1 then | |||
s1 = mw.text.trim( s1 ) | |||
if s1 == "" then | |||
s1 = false | |||
end | |||
end | |||
if s2 then | |||
s2 = mw.text.trim( s2 ) | |||
if s2 == "" then | |||
s2 = false | |||
end | |||
end | |||
if s1 or s2 then | |||
local l | |||
Frame = frame | |||
l, r = pcall( Prototypes.flow, | |||
Meta.fiat( s1 ), s2, action ) | |||
if r == true then | |||
r = "1" | |||
end | |||
end | |||
return r or "" | |||
end -- Templates.flow() | |||
World.templates.formatter = function ( assigned, ask, adapt ) | |||
-- Retrieve format specification string | |||
-- Parameter: | |||
-- assigned -- table, with numbers etc. | |||
-- ask -- string, format spec, or nil | |||
-- adapt -- table, with options | |||
-- .lang -- string, with particular language code | |||
-- .lonely -- true: permit lonely hour | |||
-- Returns: | |||
-- 1 -- string | |||
-- 2 -- string or nil; append suffix (zone) | |||
local r1, r2 | |||
if not ask or ask == "" then | |||
r1 = "c" | |||
elseif ask == "*" then | |||
if World.present then | |||
if assigned.hour then | |||
if assigned.dom or assigned.month or assigned.year then | |||
if World.present.both and | |||
World.present.date and | |||
World.present.time then | |||
r1 = World.present.both | |||
:gsub( "$date", World.present.date ) | |||
:gsub( "$time", World.present.time ) | |||
else | |||
r1 = World.present.date | |||
end | |||
end | |||
r1 = r1 or World.present.time | |||
else | |||
r1 = World.present.date | |||
end | |||
end | |||
r1 = r1 or "c" | |||
else | |||
local template = World.templates[ ask ] | |||
r1 = ask | |||
if not template then | |||
local slang = ( adapt.lang or assigned.lang or World.slang ) | |||
local tmp = World.templates[ slang ] | |||
if tmp then | |||
template = tmp[ ask ] | |||
end | |||
if not template then | |||
local i = slang:find( "-", 3, true ) | |||
if i then | |||
slang = slang:sub( 1, i - 1 ):lower() | |||
tmp = World.templates[ slang ] | |||
if tmp then | |||
template = tmp[ ask ] | |||
end | |||
end | |||
end | |||
end | |||
if type( template ) == "table" then | |||
local low = ( ask == "ISO" or ask == "ISO-T" ) | |||
r1 = template.spec | |||
if assigned.year then | |||
if not assigned.dom then | |||
r1 = r1:gsub( "[ .%-]?[dDjlNwz][ .,%-]*", "" ) | |||
:gsub( "^ ", "" ) | |||
if not assigned.month then | |||
r1 = r1:gsub( "[ .%-]?[FmMnt][ .%-]*", "" ) | |||
end | |||
end | |||
else | |||
r1 = r1:gsub( " ?[yY] ?", "" ) | |||
if not assigned.dom then | |||
r1 = r1:gsub( "[ .]?[dDjlNwz][ .,%-]*", "" ) | |||
:gsub( "^ ", "" ) | |||
end | |||
end | |||
if template.lift and | |||
( assigned.dom or | |||
not ( assigned.month or assigned.year or assigned.bc ) | |||
) then | |||
local stamp = false | |||
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.%03d", | |||
stamp, | |||
assigned.msec ) | |||
if assigned.mysec then | |||
stamp = string.format( "%s.%03d", | |||
stamp, | |||
assigned.mysec ) | |||
end | |||
end | |||
end | |||
elseif adapt.lonely then | |||
stamp = "H" | |||
end | |||
end | |||
if low or ask:find( "hh:mm:ss", 1, true ) then | |||
if stamp then | |||
r1 = string.format( "%s %s", r1, stamp ) | |||
end | end | ||
elseif ask:find( "hh:mm", 1, true ) and | |||
stamp and | |||
#stamp > 3 then | |||
r1 = string.format( "%s H:i", r1 ) | |||
end | end | ||
if stamp then | |||
if low or template.long then | |||
local scheme | |||
if template.long then | |||
scheme = mw.language.getContentLanguage() | |||
if stamp then | |||
if low or template.long then | |||
local scheme | |||
if template.long then | |||
scheme = mw.language.getContentLanguage() | |||
scheme = scheme.code | scheme = scheme.code | ||
end | end | ||
Zeile 2.059: | Zeile 2.456: | ||
if #s == 1 then | if #s == 1 then | ||
-- "YXWVUTSRQPONZABCDEFGHIKLM" | -- "YXWVUTSRQPONZABCDEFGHIKLM" | ||
move = World.zones[ "!" ]:find( s ) | move = World.zones[ "!" ]:find( s, 1, true ) | ||
if move then | if move then | ||
move = ( move - 13 ) * 100 | move = ( move - 13 ) * 100 | ||
Zeile 2.074: | Zeile 2.471: | ||
if tmp then | if tmp then | ||
code = tmp[ s ] | code = tmp[ s ] | ||
end | |||
if not code and | |||
slang ~= "en" and | |||
World.zones.en then | |||
code = World.zones.en[ s ] | |||
end | end | ||
end | end | ||
Zeile 2.128: | Zeile 2.530: | ||
-- Export | -- Export | ||
local p = { } | local p = { } | ||
p.test = function ( args, alien ) | |||
local slang = args.lang | local slang = args.lang or alien | ||
local obj = Meta.fiat( args[ 1 ], false, args.shift ) | local obj = Meta.fiat( args[ 1 ], false, args.shift ) | ||
local r | local r | ||
if type( obj ) == "table" then | if type( obj ) == "table" then | ||
local spec = args[ 2 ] | local spec = args[ 2 ] | ||
local opt | local opt | ||
if spec then | if spec then | ||
spec = mw.text.trim( spec ) | spec = mw.text.trim( spec ) | ||
end | end | ||
if slang then | if slang then | ||
opt = { lang = mw.text.trim( slang ) } | opt = { lang = mw.text.trim( slang ) } | ||
end | end | ||
r = obj:format( spec, opt ) | r = obj:format( spec, opt ) | ||
else | else | ||
r = ( args.noerror or "0" ) | r = ( args.noerror or "0" ) | ||
if r == "0" then | if r == "0" then | ||
r = fault( "Format | r = fault( "Format invalid" ) | ||
else | else | ||
r = "" | r = "" | ||
end | |||
if args.errCat then | |||
local cats = mw.text.split( args.errCat, "%s*|%s*" ) | |||
for i = 1, #cats do | |||
r = string.format( "%s[[Category:%s]]", r, cats[ i ] ) | |||
end -- for i | |||
end | end | ||
end | end | ||
Zeile 2.157: | Zeile 2.565: | ||
p.failsafe = function ( frame ) | |||
local s = type( frame ) | local s = type( frame ) | ||
local r, since | local r, since | ||
Zeile 2.168: | Zeile 2.576: | ||
end | end | ||
end | end | ||
return Prototypes.failsafe( false, since ) or "" | |||
end -- p.failsafe | end -- p.failsafe | ||
p.format = function ( frame ) | |||
-- 1 -- stamp | -- 1 -- stamp | ||
-- 2 -- spec | -- 2 -- spec | ||
Zeile 2.208: | Zeile 2.591: | ||
frame.args[ 2 ], | frame.args[ 2 ], | ||
shift = frame.args.shift, | shift = frame.args.shift, | ||
noerror = frame.args.noerror } | noerror = frame.args.noerror, | ||
errCat = frame.args.errCat } | |||
if not v[ 1 ] or v[ 1 ] == "now" then | if not v[ 1 ] or v[ 1 ] == "now" then | ||
v[ 1 ] = frame:callParserFunction( "#timel", "c", v.shift ) | v[ 1 ] = frame:callParserFunction( "#timel", "c", v.shift ) | ||
Zeile 2.223: | Zeile 2.607: | ||
p.lt = function ( frame ) | |||
return Templates.flow( frame, "lt" ) | return Templates.flow( frame, "lt" ) | ||
end -- p.lt | end -- p.lt | ||
p.le = function ( frame ) | |||
return Templates.flow( frame, "le" ) | return Templates.flow( frame, "le" ) | ||
end -- p.le | end -- p.le | ||
p.eq = function ( frame ) | |||
return Templates.flow( frame, "eq" ) | return Templates.flow( frame, "eq" ) | ||
end -- p.eq | end -- p.eq | ||
p.ne = function ( frame ) | |||
return Templates.flow( frame, "ne" ) | return Templates.flow( frame, "ne" ) | ||
end -- p.ne | end -- p.ne | ||
p.ge = function ( frame ) | |||
return Templates.flow( frame, "ge" ) | return Templates.flow( frame, "ge" ) | ||
end -- p.ge | end -- p.ge | ||
p.gt = function ( frame ) | |||
return Templates.flow( frame, "gt" ) | return Templates.flow( frame, "gt" ) | ||
end -- p.gt | end -- p.gt |