local p = {}
local function splitISO8601( str )
if 'table' == type( str ) then
if str.args and str.args[1] then
str = '' .. str.args[1]
else
return 'unknown argument type: ' .. type( str ) .. ': ' .. table.tostring( str )
end
end
local Y, M, D = ( function( str )
local pattern = '(%-?%d+)%-(%d+)%-(%d+)T'
local Y, M, D = mw.ustring.match( str, pattern )
return tonumber( Y ), tonumber( M ), tonumber( D )
end )( str );
local h, m, s = ( function( str )
local pattern = 'T(%d+):(%d+):(%d+)%Z';
local H, M, S = mw.ustring.match( str, pattern);
return tonumber( H ), tonumber( M ), tonumber( S );
end )( str );
local oh, om = ( function( str )
-- ends with Z, Zulu time
if str:sub( -1 ) == 'Z' then
return 0, 0
end;
-- matches ±hh:mm, ±hhmm or ±hh; else returns nils
local pattern = '([-+])(%d%d):?(%d?%d?)$';
local sign, oh, om = mw.ustring.match( str, pattern );
sign, oh, om = sign or '+', oh or '00', om or '00';
return tonumber( sign .. oh ), tonumber( sign .. om );
end )( str )
return {year=Y, month=M, day=D, hour=( h + oh ), min=( m + om ), sec=s};
end
local function splitDate( str )
local Y, M, D = ( function( str )
local pattern = "^(%d+)%-(%d+)%-(%d+)$"
local Y, M, D = mw.ustring.match( str, pattern )
return tonumber( Y ), tonumber( M ), tonumber( D )
end )( str );
return { year=Y, month=M, day=D, hour=0, min=0, sec=0 };
end
function p.parseTimeFromSnak( snak )
if ( snak and snak.datavalue and snak.datavalue.value and snak.datavalue.value.time ) then
return tonumber( os.time( splitISO8601( tostring( snak.datavalue.value.time ) ) ) ) * 1000;
end
return nil;
end
function p.parseTimeFromDate( datestring )
return tonumber( os.time( splitDate( datestring ) ) ) * 1000
end
return p