Мазмұнға өту

Module:Infocards

Уикикітап жобасынан

dateOfBirth функция мысалдары

[өңдеу]
  • {{#invoke:Infocards|dateOfBirth|-382|−336}} → б. з. д. 382 (-382)
  • {{#invoke:Infocardslocal infocards = {}; local calculateAge = true; function infocards._getParameters( frame_args, arg_list ) local new_args = {}; local index = 1; local value; for i,arg in ipairs( arg_list ) do value = frame_args[arg] if value == nil then value = frame_args[index]; index = index + 1; end new_args[arg] = value; end return new_args; end function infocards.isBlank( someString ) return someString == nil or mw.ustring.match(someString, '^%s*$') ~= nil; end function infocards.isDate ( frame ) local new_args = infocards._getParameters( frame.args, {'s', 't', 'f'} ); local s = new_args['s'] or ''; local t = new_args['t'] or ''; local f = new_args['f'] or ''; local result = infocards.isDateImpl ( s ) if (result) then return t else return f end end function infocards.isDateImpl ( s ) local converted = infocards.convertToDate ( s ); return converted ~= nil end function infocards.dateOfBirth( frame ) local new_args = infocards._getParameters( frame.args, {'dateOfBirth', 'dateOfDeath', 'nocat'} ); local dateOfBirth = new_args['dateOfBirth'] or ''; local dateOfDeath = new_args['dateOfDeath'] or ''; local nocat = new_args['nocat'] or mw.title.getCurrentTitle().nsText; return infocards.dateOfBirthImpl( dateOfBirth, dateOfDeath, nocat ); end function infocards.dateOfBirthImpl( dateOfBirth, dateOfDeath, nocat ) local appendToCategory = infocards.isBlank( nocat ); if ( infocards.isBlank( dateOfBirth ) ) then if ( appendToCategory ) then return '[[Санат:Туған жылы белгісіздер]]' else return '' end end if ( mw.ustring.match( dateOfBirth, '^%s*белгісіз.%s*$' ) ~= nil or mw.ustring.match( dateOfBirth, '^%s*%?%s*$' ) ~= nil ) then if ( appendToCategory ) then return "''белгісіз''[[Санат:Туған жылы белгісіздер]]" else return "''белгісіз''" end end local appendAge = calculateAge and infocards.isBlank( dateOfDeath ); local parsedDate = infocards.convertToDate ( dateOfBirth ) if ( parsedDate == nil ) then local bDateStart = '<span class="no-wikidata" data-wikidata-property-id="P569">' local bDateEnd = '</span>' if ( appendToCategory ) then return bDateStart .. dateOfBirth .. bDateEnd .. '' else return bDateStart .. dateOfBirth .. bDateEnd end end local result = infocards.formatDateImpl ( parsedDate, 'bday', appendToCategory and 'туғандар' or nil ) if ( appendAge ) then local age = infocards.age ( parsedDate, os.date("*t") ) if ( age and age > 0 and age < 125) then result = result .. ' <span class="nowrap">(' .. age .. ' ' .. mw.language.new( 'kk' ):plural( age, 'жас') .. ')</span>' end if ( appendToCategory ) then if ( age and age > 100 ) then result = result .. '[[Санат:Ұзақ өмір сүргендер]]' elseif ( age or ( parsedDate and parsedDate.year and os.date('*t').year - parsedDate.year <= 100 ) ) then result = result .. '[[Санат:Тірі тұлғалар]]' end end end return result end function infocards.dateOfDeath( frame ) local new_args = infocards._getParameters( frame.args, {'dateOfBirth', 'dateOfDeath', 'nocat'} ); local dateOfBirth = new_args['dateOfBirth'] or ''; local dateOfDeath = new_args['dateOfDeath'] or ''; local nocat = new_args['nocat'] or mw.title.getCurrentTitle().nsText; return infocards.dateOfDeathImpl( dateOfBirth, dateOfDeath, nocat ); end function infocards.dateOfDeathImpl( dateOfBirth, dateOfDeath, nocat ) if ( infocards.isBlank( dateOfDeath ) ) then return '' end local appendToCategory = infocards.isBlank( nocat ); if ( mw.ustring.match( dateOfDeath, '^%s*белгісіз%s*$' ) ~= nil or mw.ustring.match( dateOfDeath, '^%s*%?%s*$' ) ~= nil ) then if ( appendToCategory ) then return "''белгісіз''[[Санат:Қайтыс болған жылы белгісіздер]]" else return "''белгісіз''" end end local parsedDateOfBirth = infocards.convertToDate ( dateOfBirth ) local parsedDateOfDeath = infocards.convertToDate ( dateOfDeath ) if ( parsedDateOfDeath == nil ) then local dDateStart = '<span class="no-wikidata" data-wikidata-property-id="P570">' local dDateEnd = '</span>' if ( appendToCategory ) then return dDateStart .. dateOfDeath .. dDateEnd .. '' else return dDateStart .. dateOfDeath .. dDateEnd end end local result = infocards.formatDateImpl ( parsedDateOfDeath, 'dday', appendToCategory and 'қайтыс болғандар' or nil ) if ( calculateAge ) then local age = infocards.age ( parsedDateOfBirth, parsedDateOfDeath ) if ( age and age > 0 ) then result = result .. ' <span class="nowrap">(' .. age .. ' ' .. mw.language.new( 'kk' ):plural( age, 'жас') .. ')</span>' end local unixAvailable, unixDateOfDeath = pcall(function() local r = os.time(parsedDateOfDeath) if ( r ~= os.time() ) then return r end error() end) if ( unixAvailable and os.time() - unixDateOfDeath < 31536000 and appendToCategory ) then result = result .. '[[Санат:Соңғы жылы қайтыс болғандар]]' end end return result end function infocards.age( parsedBirthDate, parsedFinishDate ) if ( parsedBirthDate == nil or parsedFinishDate == nil ) then return nil end local bd = parsedBirthDate["day"] local bm = parsedBirthDate["month"] local by = parsedBirthDate["year"] local dd = parsedFinishDate["day"]; local dm = parsedFinishDate["month"]; local dy = parsedFinishDate["year"]; if ( bd and bm and by and dd and dm and dy ) then if ( dm > bm or ( dm == bm and dd >= bd ) ) then return dy - by else return dy - by - 1 end else return nil end end local genitivusMonthes = {'қаңтар', 'ақпан', 'наурыз', 'сәуір', 'мамыр', 'маусым', 'шілде', 'тамыз', 'қыркүйек', 'қазан', 'қараша', 'желтоқсан'} local genitivusMonthes2 = {'қаңтарда','ақпанда','наурызда','сәуірде','мамырда','маусымда','шілдеде','тамызда','қыркүйекте','қазанда','қарашада','желтоқсанда'} function infocards.nominativeYear( year ) if ( year >= 0 ) then return '[[' .. year .. ' жыл|' .. year .. ' ]]' else return '[[б. з. д. ' .. ( 0 - year ) .. ' жыл |б. з. д. ' .. ( 0 - year ) .. ' ]]' end end local suffixes = { "", "", "", "", "", "", "", "", "", [0] = "", [10] = "", [20] = "", [30] = "", [40] = "", [50] = "", [60] = "", [70] = "", [80] = "", [90] = "" } local function ordinal(number) local suffix if number % 1000 == 0 then suffix = "" elseif number %100 == 0 then suffix = "" elseif number %10 == 0 then local rem = number % 100 suffix = suffixes[rem] end if not suffix then rem = number %10 suffix = suffixes[rem] end return number .. "" .. suffix end local function inYear( year ) if year >= 0 then return ordinal(year) else year = -year return 'Б. з. д. ' .. ordinal(year) end end function infocards.formatDateImpl( parsedDate, infocardClass, categoryNamePrefix ) local nd = parsedDate["day"]; local nm = parsedDate["month"]; local ny = parsedDate["year"]; local od = parsedDate["osday"]; local om = parsedDate["osmonth"]; local oy = parsedDate["osyear"]; local template = (nd ~= nil and "1" or "") .. (nm ~= nil and "2" or "") .. (ny ~= nil and "3" or "") .. (od ~= nil and "4" or "") .. (om ~= nil and "5" or "") .. (oy ~= nil and "6" or "") local datePart = '<span class="nowrap">' if (infocardClass == "bday") then datePart = '<span class="no-wikidata" data-wikidata-property-id="P569">' elseif (infocardClass == "dday") then datePart = '<span class="no-wikidata" data-wikidata-property-id="P570">' end if (template == "12") then datePart = datePart .. string.format( "[[%d %s]]", nd, genitivusMonthes[nm] ) elseif (template == "3") then datePart = datePart .. infocards.nominativeYear( ny ) elseif (template == "123") then datePart = datePart .. string.format( "[[%d %s]] %s", nd, genitivusMonthes[nm], infocards.nominativeYear( ny ) ) elseif (template == "124") then datePart = datePart .. string.format( "[[%d %s|%d (%d) %s]]", nd, genitivusMonthes[nm], od, nd, genitivusMonthes[nm] ) elseif (template == "1234") then datePart = datePart .. string.format( "[[%d %s|%d (%d) %s]] %s", nd, genitivusMonthes[nm], od, nd, genitivusMonthes[nm], infocards.nominativeYear( ny ) ) elseif (template == "1245") then datePart = datePart .. string.format( "%d %s ([[%d %s]])", od, genitivusMonthes[om], nd, genitivusMonthes[nm] ) elseif (template == "12345") then datePart = datePart .. string.format( "%d %s ([[%d %s]]) %s", od, genitivusMonthes[om], nd, genitivusMonthes[nm], infocards.nominativeYear( ny ) ) elseif (template == "123456") then datePart = datePart .. string.format( '%d %s %d</span> <span class="nowrap">([[%d %s]] %s)', od, genitivusMonthes[om], oy, nd, genitivusMonthes[nm], infocards.nominativeYear( ny ) ) else datePart = datePart .. 'форматы дұрыс емес' end datePart = datePart .. '</span>' local infocardTemplate = (nd ~= nil and "1" or "") .. (nm ~= nil and "2" or "") .. (ny ~= nil and "3" or "") if (infocardTemplate == "123") then datePart = datePart .. string.format('<span style="display:none">(<span class="%s">%04d-%02d-%02d</span>)</span>', infocardClass , ny , nm , nd ) elseif (infocardTemplate == "23") then datePart = datePart .. string.format('<span style="display:none">(<span class="%s">%04d-%02d</span>)</span>', infocardClass , ny , nm ) elseif (infocardTemplate == "3") then datePart = datePart .. string.format('<span style="display:none;">(<span class="%s">%04d</span>)</span>', infocardClass , ny ) end if ( categoryNamePrefix ~= nil ) then if ( nd ~= nil and nm ~= nil) then datePart = datePart .. '[[Санат:' .. nd .. ' ' .. genitivusMonthes2[nm] .. ' ' .. categoryNamePrefix .. ']]' end if ( ny ~= nil) then datePart = datePart .. '[[Санат:' .. inYear( ny ) .. ' жылы ' .. categoryNamePrefix .. ']]' end end return datePart end function infocards.convertToDate( possibleDateString ) possibleDateString = mw.ustring.gsub( possibleDateString, '−', '-') local simpleDate = mw.ustring.match(possibleDateString, '^%s*([%-%d%.]+)%s*$', 0) if ( simpleDate ) then return infocards.convertToDateNewStylePart( simpleDate ); end local complexDate1, complexDate2 = mw.ustring.match(possibleDateString, '^%s*([%-%d%.]+)%s*%(([%-%d%.]+)%)%s*$', 0) if ( complexDate1 and complexDate2) then local table1 = infocards.convertToDateNewStylePart( complexDate1 ); local table2 = infocards.convertToDateOldStylePart( complexDate2 ); if ( table1 and table2 ) then return { year = table1["year"], month = table1["month"], day = table1["day"], osyear = table2["year"], osmonth = table2["month"], osday = table2["day"] } else return nil end end return nil end function infocards.convertToDateNewStylePart( possibleDateString ) local ny = mw.ustring.match(possibleDateString, '^(%-?%d+)$', 0) if (ny ~= nil) then return {year = tonumber(ny)} end return infocards.convertToDateCommonPart( possibleDateString ) end function infocards.convertToDateOldStylePart( possibleDateString ) local nd = mw.ustring.match(possibleDateString, '^(%-?%d+)$', 0) if (nd ~= nil) then return {day = tonumber(nd)} end return infocards.convertToDateCommonPart( possibleDateString ) end function infocards.convertToDateCommonPart( possibleDateString ) local nd, nm = mw.ustring.match(possibleDateString, '^(%d?%d)%.(%d?%d)$', 0) if (nd ~= nil and nm ~= nil) then return {day = tonumber(nd), month = tonumber(nm)} end local nd, nm, ny = mw.ustring.match(possibleDateString, '^(%d?%d)%.(%d?%d)%.(%-?%d+)$', 0) if (nd ~= nil and nm ~= nil and ny ~= nil) then local ndn = tonumber(nd) local nmn = tonumber(nm) local nyn = tonumber(ny) if (ndn > 0 and ndn < 33 and nmn > 0 and nmn < 13) then return {day = ndn, month = nmn, year = nyn} end end return nil end return infocards dateOfBirth|11.1|}} → 11 қаңтар
  • {{#invoke:Infocards|dateOfBirth|24.12.3|15.01.69}} → 24 желтоқсан 3 (0003-12-24)
  • {{#invoke:Infocards|dateOfBirth|23.09.-63|19.08.14}} → 23 қыркүйек б. з. д. 63 (-063-09-23)
  • {{#invoke:Infocards|dateOfBirth|42|9.6.42}} → 42 (0042)
  • {{#invoke:Infocards|dateOfBirth|870|13.8.900}} → 870 (0870)
  • {{#invoke:Infocards|dateOfBirth||22.01.984}} →
  • {{#invoke:Infocards|dateOfBirth|16.06.1066|қайтыс болған күні белгісіз}} → 16 маусым 1066 (1066-06-16)
  • {{#invoke:Infocards|dateOfBirth|8.2.1834 (27.1)|2.2.1907 (20.1)}} → 27 қаңтар (8 ақпан) 1834 (1834-02-08)
  • {{#invoke:Infocards|dateOfBirth|29.06.1844 (17)|28.10.1883 (16)}} → 17 (29) маусым 1844 (1844-06-29)
  • {{#invoke:Infocards|dateOfBirth|19.7.1893 (7)|}} → 7 (19) шілде 1893 (1893-07-19)
  • {{#invoke:Infocards|dateOfBirth|3.10.1895 (21.9)|28.12.1925}} → 21 қыркүйек (3 қазан) 1895 (1895-10-03)
  • {{#invoke:Infocards|dateOfBirth|4.10.1916|8.11.2009}} → 4 қазан 1916 (1916-10-04)
  • {{#invoke:Infocards|dateOfBirth|09.06.1942|}} → 9 маусым 1942 (1942-06-09) (82 жас)
  • {{#invoke:Infocards|dateOfBirth|1955|}} → 1955 (1955)
  • {{#invoke:Infocards|dateOfBirth|29.02.1984|}} → 29 ақпан 1984 (1984-02-29) (40 жас)
  • {{#invoke:Infocards|dateOfBirth||}} →
  • {{#invoke:Infocards|dateOfBirth|4.1.1885 (23.12.1884)|17.5.1951}} → 23 желтоқсан 1884 (4 қаңтар 1885 )(1885-01-04)

dateOfDeath функциясының мысалдары

[өңдеу]
  • {{#invoke:Infocards|dateOfDeath|-382|−336}} → б. з. д. 336 (-336)
  • {{#invoke:Infocards|dateOfDeath|24.12.3|15.01.69}} → 15 қаңтар 69 (0069-01-15) (65 жас)
  • {{#invoke:Infocards|dateOfDeath|23.09.-63|19.08.14}} → 19 тамыз 14 (0014-08-19) (76 жас)
  • {{#invoke:Infocards|dateOfDeath|42|9.6.42}} → 9 маусым 42 (0042-06-09)
  • {{#invoke:Infocards|dateOfDeath|870|13.8.900}} → 13 тамыз 900 (0900-08-13)
  • {{#invoke:Infocards|dateOfDeath||22.01.984}} → 22 қаңтар 984 (0984-01-22)
  • {{#invoke:Infocards|dateOfDeath|16.06.1066|қайтыс болған күні белгісіз}} → қайтыс болған күні белгісіз
  • {{#invoke:Infocards|dateOfDeath|8.2.1834 (27.1)|2.2.1907 (20.1)}} → 20 қаңтар (2 ақпан) 1907 (1907-02-02) (72 жас)
  • {{#invoke:Infocards|dateOfDeath|29.06.1844 (17)|28.10.1883 (16)}} → 16 (28) қазан 1883 (1883-10-28) (39 жас)
  • {{#invoke:Infocards|dateOfDeath|19.7.1893 (7)|}} → 14 сәуір 1930 (1930-04-14) (36 жас)
  • {{#invoke:Infocards|dateOfDeath|3.10.1895 (21.9)|28.12.1925}} → 28 желтоқсан 1925 (1925-12-28) (30 жас)
  • {{#invoke:Infocards|dateOfDeath|4.10.1916|8.11.2009}} → 8 қараша 2009 (2009-11-08) (93 жас)
  • {{#invoke:Infocards|dateOfDeath|09.06.1942|}} →
  • {{#invoke:Infocards|dateOfDeath|1955|}} →
  • {{#invoke:Infocards|dateOfDeath|29.02.1984|}} →
  • {{#invoke:Infocards|dateOfDeath||}} →

isDate функциясының мысалдары

[өңдеу]
  • {{#invoke:Infocards|isDate||TRUE|FALSE}} → FALSE
  • {{#invoke:Infocards|isDate|-382|TRUE|FALSE}} → TRUE
  • {{#invoke:Infocards|isDate|−336|TRUE|FALSE}} → TRUE
  • {{#invoke:Infocards|isDate|24.12.3|TRUE|FALSE}} → TRUE
  • {{#invoke:Infocards|isDate|19.7.1893 (7)|TRUE|FALSE}} → TRUE
  • {{#invoke:Infocards|isDate|19.7.67.18/93 (7)|TRUE|FALSE}} → FALSE
  • {{#invoke:Infocards|isDate|13 желтоқсан 2005|TRUE|FALSE}} → FALSE
  • {{#invoke:Infocards|isDate|қайтыс болған күні белгісіз|TRUE|FALSE}} → FALSE

local infocards = {};
local calculateAge = true;

function infocards._getParameters( frame_args, arg_list )
    local new_args = {};
    local index = 1;
    local value;
 
    for i,arg in ipairs( arg_list ) do
        value = frame_args[arg]
        if value == nil then
            value = frame_args[index];
            index = index + 1;
        end
        new_args[arg] = value;
    end
 
    return new_args;
end        

function infocards.isBlank( someString )
    return someString == nil or mw.ustring.match(someString, '^%s*$') ~= nil;
end

function infocards.isDate ( frame )
    local new_args = infocards._getParameters( frame.args, {'s', 't', 'f'} );
    local s = new_args['s'] or '';
    local t = new_args['t'] or '';
    local f = new_args['f'] or '';

    local result = infocards.isDateImpl ( s )
    if (result) then
        return t
    else
        return f
    end
end

function infocards.isDateImpl ( s )
    local converted = infocards.convertToDate ( s );
    return converted ~= nil
end

function infocards.dateOfBirth( frame )
    local new_args = infocards._getParameters( frame.args, {'dateOfBirth', 'dateOfDeath', 'nocat'} );
    local dateOfBirth = new_args['dateOfBirth'] or '';
    local dateOfDeath = new_args['dateOfDeath'] or '';
    local nocat = new_args['nocat'] or mw.title.getCurrentTitle().nsText;

    return infocards.dateOfBirthImpl( dateOfBirth, dateOfDeath, nocat );
end

function infocards.dateOfBirthImpl( dateOfBirth, dateOfDeath, nocat )

    local appendToCategory = infocards.isBlank( nocat );

    if ( infocards.isBlank( dateOfBirth ) ) then
        if ( appendToCategory ) then
            return '[[Санат:Туған жылы белгісіздер]]'
        else
            return ''
        end
    end

    if ( mw.ustring.match( dateOfBirth, '^%s*белгісіз.%s*$' ) ~= nil
            or mw.ustring.match( dateOfBirth, '^%s*%?%s*$' ) ~= nil ) then
        if ( appendToCategory ) then
            return "''белгісіз''[[Санат:Туған жылы белгісіздер]]"
        else
            return "''белгісіз''"
        end
    end

    local appendAge = calculateAge and infocards.isBlank( dateOfDeath );

    local parsedDate = infocards.convertToDate ( dateOfBirth )
    if ( parsedDate == nil ) then
		local bDateStart = '<span class="no-wikidata" data-wikidata-property-id="P569">'
		local bDateEnd = '</span>'

        if ( appendToCategory ) then
            return bDateStart .. dateOfBirth .. bDateEnd .. ''
        else
            return bDateStart .. dateOfBirth .. bDateEnd
        end
    end

    local result = infocards.formatDateImpl ( parsedDate, 'bday', appendToCategory and 'туғандар' or nil )

    if ( appendAge ) then
        local age = infocards.age ( parsedDate,  os.date("*t") )
        if ( age and age > 0 and age < 125) then
            result = result .. ' <span class="nowrap">(' .. age .. ' ' .. mw.language.new( 'kk' ):plural( age, 'жас') .. ')</span>'
        end
        
		if ( appendToCategory ) then
			if ( age and age > 100 ) then
				result = result .. '[[Санат:Ұзақ өмір сүргендер]]'
			elseif ( age or ( parsedDate and parsedDate.year and os.date('*t').year - parsedDate.year <= 100 ) ) then
				result = result .. '[[Санат:Тірі тұлғалар]]'
			end
		end
    end

    return result
end

function infocards.dateOfDeath( frame )
    local new_args = infocards._getParameters( frame.args, {'dateOfBirth', 'dateOfDeath', 'nocat'} );
    local dateOfBirth = new_args['dateOfBirth'] or '';
    local dateOfDeath = new_args['dateOfDeath'] or '';
    local nocat = new_args['nocat'] or mw.title.getCurrentTitle().nsText;

    return infocards.dateOfDeathImpl( dateOfBirth, dateOfDeath, nocat );
end

function infocards.dateOfDeathImpl( dateOfBirth, dateOfDeath, nocat )
    if ( infocards.isBlank( dateOfDeath ) ) then
        return ''
    end

    local appendToCategory = infocards.isBlank( nocat );

    if ( mw.ustring.match( dateOfDeath, '^%s*белгісіз%s*$' ) ~= nil
            or mw.ustring.match( dateOfDeath, '^%s*%?%s*$' ) ~= nil ) then
        if ( appendToCategory ) then
            return "''белгісіз''[[Санат:Қайтыс болған жылы белгісіздер]]"
        else
            return "''белгісіз''"
        end
    end

    local parsedDateOfBirth = infocards.convertToDate ( dateOfBirth )
    local parsedDateOfDeath = infocards.convertToDate ( dateOfDeath )

    if ( parsedDateOfDeath == nil ) then
		local dDateStart = '<span class="no-wikidata" data-wikidata-property-id="P570">'
		local dDateEnd = '</span>'

        if ( appendToCategory ) then
            return dDateStart .. dateOfDeath .. dDateEnd .. ''
        else
            return dDateStart .. dateOfDeath .. dDateEnd
        end
    end

    local result = infocards.formatDateImpl ( parsedDateOfDeath, 'dday', appendToCategory and 'қайтыс болғандар' or nil )

	if ( calculateAge ) then
		local age = infocards.age ( parsedDateOfBirth, parsedDateOfDeath )
		if ( age and age > 0 ) then
			result = result .. ' <span class="nowrap">(' .. age .. ' ' .. mw.language.new( 'kk' ):plural( age, 'жас') .. ')</span>'
		end
		
		local unixAvailable, unixDateOfDeath = pcall(function()
			local r = os.time(parsedDateOfDeath)
			if ( r ~= os.time() ) then
				return r
			end
			error()
		end)
		if ( unixAvailable and os.time() - unixDateOfDeath < 31536000 and appendToCategory ) then
			result = result .. '[[Санат:Соңғы жылы қайтыс болғандар]]'
		end
	end

    return result
end

function infocards.age( parsedBirthDate, parsedFinishDate ) 
    if ( parsedBirthDate == nil or parsedFinishDate == nil ) then
        return nil
    end

    local bd = parsedBirthDate["day"]
    local bm = parsedBirthDate["month"]
    local by = parsedBirthDate["year"]

    local dd = parsedFinishDate["day"];
    local dm = parsedFinishDate["month"];
    local dy = parsedFinishDate["year"];

    if ( bd and bm and by and dd and dm and dy ) then
        if ( dm > bm or ( dm == bm and dd >= bd ) ) then
            return dy - by
        else
            return dy - by - 1
        end
    else
        return nil
    end
end

local genitivusMonthes = {'қаңтар', 'ақпан', 'наурыз', 'сәуір', 'мамыр', 'маусым',
    'шілде', 'тамыз', 'қыркүйек', 'қазан', 'қараша', 'желтоқсан'}
local genitivusMonthes2 = {'қаңтарда','ақпанда','наурызда','сәуірде','мамырда','маусымда','шілдеде','тамызда','қыркүйекте','қазанда','қарашада','желтоқсанда'}

function infocards.nominativeYear( year )
    if ( year >= 0 ) then
        return '[[' .. year .. ' жыл|' .. year .. ' ]]'
    else
        return '[[б. з. д. ' .. ( 0 - year ) .. ' жыл |б. з. д. ' .. ( 0 - year ) .. ' ]]'
    end
end

local suffixes = {
	"", "", "", "", "", "", "", "", "", [0] = "", [10] = "", [20] = "",
	[30] = "", [40] = "", [50] = "", [60] = "", [70] = "", [80] = "", [90] = ""
}

local function ordinal(number)
	local suffix
	if number % 1000 == 0 then
		suffix = ""
	elseif number %100 == 0 then
		suffix = ""
	elseif number %10 == 0 then
		local rem = number % 100
		suffix = suffixes[rem]
	end
	if not suffix then
		rem = number %10
		suffix = suffixes[rem]
	end
	return number .. "" .. suffix
end

local function inYear( year )
    if year >= 0 then
        return ordinal(year)
    else
    	year = -year
        return 'Б. з. д. ' .. ordinal(year) 
    end
end

function infocards.formatDateImpl( parsedDate, infocardClass, categoryNamePrefix )
    local nd = parsedDate["day"];
    local nm = parsedDate["month"];
    local ny = parsedDate["year"];
    local od = parsedDate["osday"];
    local om = parsedDate["osmonth"];
    local oy = parsedDate["osyear"];
    
    local template =
        (nd ~= nil and "1" or "") .. (nm ~= nil and "2" or "") .. (ny ~= nil and "3" or "") ..
        (od ~= nil and "4" or "") .. (om ~= nil and "5" or "") .. (oy ~= nil and "6" or "")

    local datePart = '<span class="nowrap">'
    if (infocardClass == "bday") then
    	datePart = '<span class="no-wikidata" data-wikidata-property-id="P569">'
    elseif (infocardClass == "dday") then
    	datePart = '<span class="no-wikidata" data-wikidata-property-id="P570">'
    end	
    if (template == "12") then
        datePart = datePart .. string.format( "[[%d %s]]", nd, genitivusMonthes[nm] )
    elseif (template == "3") then
        datePart = datePart .. infocards.nominativeYear( ny )
    elseif (template == "123") then
        datePart = datePart .. string.format( "[[%d %s]] %s",
                                        nd, genitivusMonthes[nm], infocards.nominativeYear( ny ) )
    elseif (template == "124") then
        datePart = datePart .. string.format( "[[%d %s|%d (%d) %s]]",
                                        nd, genitivusMonthes[nm], od, nd, genitivusMonthes[nm] )
    elseif (template == "1234") then
        datePart = datePart .. string.format( "[[%d %s|%d (%d) %s]] %s",
                                        nd, genitivusMonthes[nm], od, nd, genitivusMonthes[nm], infocards.nominativeYear( ny ) )
    elseif (template == "1245") then
        datePart = datePart .. string.format( "%d %s ([[%d %s]])",
                                        od, genitivusMonthes[om], nd, genitivusMonthes[nm] )
    elseif (template == "12345") then
        datePart = datePart .. string.format( "%d %s ([[%d %s]]) %s",
                                        od, genitivusMonthes[om], nd, genitivusMonthes[nm], infocards.nominativeYear( ny ) )
    elseif (template == "123456") then
        datePart = datePart .. string.format( '%d %s %d</span> <span class="nowrap">([[%d %s]] %s)',
                                        od, genitivusMonthes[om], oy, nd, genitivusMonthes[nm], infocards.nominativeYear( ny ) )
    else
        datePart = datePart .. 'форматы дұрыс емес'
    end
    datePart = datePart .. '</span>'

    local infocardTemplate =
        (nd ~= nil and "1" or "") .. (nm ~= nil and "2" or "") .. (ny ~= nil and "3" or "")

	if (infocardTemplate == "123") then
		datePart = datePart .. string.format('<span style="display:none">(<span class="%s">%04d-%02d-%02d</span>)</span>', infocardClass , ny , nm , nd )
	elseif (infocardTemplate == "23") then
		datePart = datePart .. string.format('<span style="display:none">(<span class="%s">%04d-%02d</span>)</span>', infocardClass , ny , nm )
	elseif (infocardTemplate == "3") then
		datePart = datePart .. string.format('<span style="display:none;">(<span class="%s">%04d</span>)</span>', infocardClass , ny )
	end

    if ( categoryNamePrefix ~= nil ) then
        if ( nd ~= nil and nm ~= nil) then
            datePart = datePart .. '[[Санат:' .. nd .. ' ' .. genitivusMonthes2[nm] .. ' ' .. categoryNamePrefix .. ']]'
        end
        if ( ny ~= nil) then
            datePart = datePart .. '[[Санат:' .. inYear( ny ) .. ' жылы ' .. categoryNamePrefix .. ']]'
        end
    end

    return datePart
end

function infocards.convertToDate( possibleDateString )

    possibleDateString = mw.ustring.gsub( possibleDateString, '−', '-')

    local simpleDate = mw.ustring.match(possibleDateString, '^%s*([%-%d%.]+)%s*$', 0)
    if ( simpleDate ) then
        return infocards.convertToDateNewStylePart( simpleDate );
    end

    local complexDate1, complexDate2 = mw.ustring.match(possibleDateString, '^%s*([%-%d%.]+)%s*%(([%-%d%.]+)%)%s*$', 0)
    if ( complexDate1 and complexDate2) then
        local table1 = infocards.convertToDateNewStylePart( complexDate1 );
        local table2 = infocards.convertToDateOldStylePart( complexDate2 );
        if ( table1 and table2 ) then
            return {
                    year = table1["year"], month = table1["month"], day = table1["day"], 
                    osyear = table2["year"], osmonth = table2["month"], osday = table2["day"]
                }
        else
            return nil
        end
    end

    return nil
end

function infocards.convertToDateNewStylePart( possibleDateString )

    local ny = mw.ustring.match(possibleDateString, '^(%-?%d+)$', 0)
    if (ny ~= nil) then
        return {year = tonumber(ny)}
    end

    return infocards.convertToDateCommonPart( possibleDateString )
end

function infocards.convertToDateOldStylePart( possibleDateString )

    local nd = mw.ustring.match(possibleDateString, '^(%-?%d+)$', 0)
    if (nd ~= nil) then
        return {day = tonumber(nd)}
    end

    return infocards.convertToDateCommonPart( possibleDateString )
end


function infocards.convertToDateCommonPart( possibleDateString )

    local nd, nm
        = mw.ustring.match(possibleDateString, '^(%d?%d)%.(%d?%d)$', 0)
    if (nd ~= nil and nm ~= nil) then
        return {day = tonumber(nd), month = tonumber(nm)}
    end

    local nd, nm, ny
        = mw.ustring.match(possibleDateString, '^(%d?%d)%.(%d?%d)%.(%-?%d+)$', 0)
    if (nd ~= nil and nm ~= nil and ny ~= nil) then
    	local ndn = tonumber(nd)
    	local nmn = tonumber(nm)
    	local nyn = tonumber(ny)
    	if (ndn > 0 and ndn < 33 and nmn > 0 and nmn < 13) then
        	return {day = ndn, month = nmn, year = nyn}
        end
    end

    return nil
end

return infocards