Documentation icon دستاویز [تخلیق]
--[[

{{#invoke:ملعب/Mr. Ibrahem/5|getdate|P569}}
{{#invoke:ملعب/Mr. Ibrahem/5|getdate|P570}}
--]]
local ModuleTime = require 'Module:wikidata2/time'
local p = {}
local o3 = '[[زمرہ:صفحات جن میں تاریخوں کے جائزے کی ضرورت ہے]]'

function getdatepart(time , option)
	if time and time ~='' then
		if option =='y' then
			return tonumber(string.sub(time, 2, 5))
	    elseif option =='m' then
			return tonumber(string.sub(time, 7, 8))
	    elseif option =='d' then
			return tonumber(string.sub(time, 10, 11))
		end
	end
end

function category(property, y, m, d)
		if property == 'P569' 
			then suffix = ' کی پیدائشیں'
				if d and m then cat2 = d.. ' ' .. m.. ' کی پیدائشیں' end
		elseif property == 'P570' 
			then suffix = ' کی وفیات'
				if d and m then cat3 = d.. ' ' .. m.. ' کی وفیات' end
		end
	if string.find( y , "ق م") == nil
	then
		suffix = "ء " .. suffix
	end
	mw.log("y .. suffix" .. y .. suffix)
	if y then cat = ' [[زمرہ:'.. y .. suffix ..']]' end
	if cat2 then cate =  ' [[زمرہ:'.. cat2 ..']]' end
	if cat3 then cate2 =  ' [[زمرہ:'.. cat3 ..']]' end
	return (cat or '') ..  (cate or '')..  (cate2 or '')
end

function getprop(propertyID,modifytime,entity)
		local val = formatStatements( {property =propertyID ,entityId=entity , modifytime =modifytime, noref='true' , enbarten ='true'})
	if val and val ~= '' then 
	return val
	else end
end

function mathyears(year, Ryear, month, Rmonth)
--	Ryear = Date( "%Y" )
--	Rmonth =Date( "%m" )
--	Rday = os.date( "%e" )
--	Ryear = Date( "%Y" )
--	Rmonth =Date( "%m" )
--	Rday = os.date( "%e" )
	--	if string.sub(Ryear, 1, 1) == '-' then Ryear = '+' .. string.sub(Ryear, 2) end
		if month and month ~=''
		then
			if Rmonth < month
			then 
				val = Ryear - year - 1
			else 
				if Rmonth > month
					then val = Ryear - year
					else val = Ryear - year - 1 ..'&ndash;' .. Ryear - year
				end
			end
		else
			val = Ryear - year - 1 ..'&ndash;' .. Ryear - year
		end
	if (Ryear - year) > 150 then val = val .. o3 elseif (year - Ryear) > 150 then val = val .. o3 end
	return val
end

function age(v, pr, add)
	if add and add ~='' then return '' else end
	--	if string.sub(Ryear, 1, 1) == '-' then Ryear = '+' .. string.sub(Ryear, 2) end
--	val =  mathyears(year, Ryear, month, Rmonth)
	if pr and pr ~='' then
		v = 'عمر ' .. v
	end
	return ' (' .. v ..' سال)'
end

function mathfulldate(yb, mb, db, Yd, Md, Dd)
	yd = Yd or tonumber(os.date( "%Y" ))
	md = Md or tonumber(os.date( "%m" ))
	dd = Dd or tonumber(os.date( "%e" ))
	function ma(f,s)
		if f < s 
			then return '1'
			elseif f == s then return '1'
			else return '0'
		end
	end
	function foo(f,s)
		if f < s 
			then return '1'
			else return '0'
		end
	end	
	if not foo(db,db) == '0' or not ma(md,mb) == '0'
		then vv = '1'
		else vv = '0'
	end
	val = (yd)-(yb)-(vv)
	if (yd - yb) > 150 then val = val .. o3 elseif (yb - yd) > 150 then val = val .. o3 end
	return val
end

function linkdate(property, y, m, d)
			if y then year = y ..'ء' end
			if m 
				then 
					if d
					then md =  d ..' ' .. m ..' '
					else md =  m 
					end
				else 
				year = 'سنہ ' .. year
			end	
		return (md or '') .. year .. category(property, y, m, d)
	end	
	
function getP570(P570precision,Timev,entity,P569precision,P569time)
--	local P569precision = P569pre or getprop('P569','precision',entity)
--	local P569time =   P569tim or getprop('P569','q',entity)
	if string.sub(Timev, 1, 1) == '-' then
		timev = '+' .. string.sub(Timev, 2)
		P570addon =  ' ق م'
	else timev= Timev
		end
	if P569time and P569time ~= '' then
	if string.sub(P569time, 1, 1) == '-' then
		P569time = '+' .. string.sub(P569time, 2)
		P569addon =  ' ق م'
	end end
		local Dyear = getdatepart(timev,'y')
		local Dmonth =getdatepart(timev,'m')
		local Dmonthname =mw.getContentLanguage():formatDate( 'F', timev )
		local Dday =getdatepart(timev,'d')
		local year = getdatepart(P569time ,'y')
		local month =getdatepart(P569time ,'m')
		local monthname =mw.getContentLanguage():formatDate( 'F', P569time )
		local day =getdatepart(P569time ,'d')
	--	if P570precision and P570precision~= '' 
	--		then
			if P569precision and P569precision  ~= '' 
				then 
					if P570precision == 11 or P570precision == '11'
						then 
							if P569precision == 11 or P569precision == '11'
								then 
									doo = mathfulldate(year, month, day, Dyear, Dmonth, Dday)
									dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname, Dday) .. age(doo, '', P570addon or P569addon)
								elseif P569precision == 10 or P569precision == '10' or P569precision == 9 or P569precision == '9'
										then  
											dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname, Dday) .. age(mathyears(year, Dyear), '',  P570addon or P569addon)
											
								else
								dii  = ModuleTime.getdate( {time=Timev ,precision=tonumber(P570precision)} , {})
							end
						elseif P570precision == 10 or P570precision == '10'
							then
								dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname) .. age(mathyears(year, Dyear), '',  P570addon or P569addon)
						elseif P570precision == 9 or P570precision == '9' then
							dii = linkdate('P570',  Dyear .. (P570addon or '')) .. age(mathyears(year, Dyear), '',  P570addon or P569addon)
						else 
							dii  = ModuleTime.getdate( {time=Timev ,precision=tonumber(P570precision)} , {})
							end
				else
					if P570precision == 11 or P570precision == '11'
						then 
							dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname, Dday)
						elseif P570precision  == 10 or P570precision  == '10'
							then 
								dii = linkdate('P570',  Dyear .. (P570addon or ''), Dmonthname)
						elseif P570precision == 9 or P570precision == '9' then 
							dii  =linkdate('P570',  Dyear .. (P570addon or ''))
						else 
							dii  = ModuleTime.getdate( {time=Timev ,precision=tonumber(P570precision)} , {})
					end
			end

				
	--		else
	--	end

	return dii -- .. (P570addon or '')
end

function getP569(P569precision,Timev,entity,P570precision)
--	local P570precision = P570pre or getprop('P570','precision',entity)   
--	local P570time = getprop('P570','q',entity)
	local P569addon = ''
	if string.sub(Timev, 1, 1) == '-' then
		timev = '+' .. string.sub(Timev, 2)
		P569addon =  ' ق م'
	else timev= Timev
		end
		local year = getdatepart(timev,'y')
		local month =getdatepart(timev,'m')
		local monthname =mw.getContentLanguage():formatDate( 'F', timev )
		local day =getdatepart(timev,'d')

	--	if P569precision and P569precision ~= '' 
	--	then
			if P570precision and P570precision ~= '' 
				then 
					if P569precision == 11 or  P569precision == '11'
						then 
							val = linkdate('P569',  year .. (P569addon or ''), monthname, day)
						elseif P569precision == 10 or  P569precision == '10'
							then 
								val = linkdate('P569',  year .. (P569addon or ''), monthname)
						elseif P569precision == 9 or P569precision == '9' then 
							val = linkdate('P569',  year .. (P569addon or ''))
						else 
							val = ModuleTime.getdate( {time=Timev ,precision=tonumber(P569precision)} , {})
					end
				else 
					if P569precision == 11 or  P569precision == '11' 
						then 
							doo = mathfulldate(year, month, day)
							val = linkdate('P569',  year .. (P569addon or ''), monthname, day) .. age(doo, '', P569addon)
						elseif P569precision == 10 or P569precision == '10' 
							then  
								val =  linkdate('P569',  year .. (P569addon or ''), monthname)
								.. age(mathyears(year, tonumber(os.date("%Y")), month, tonumber(os.date("%m"))), '', P569addon)
						elseif P569precision == '9' or P569precision == 9 then 
							val =  linkdate('P569',  year .. (P569addon or '')) .. age(mathyears(year, tonumber(os.date("%Y"))), 't', P569addon)
						else 
							val = ModuleTime.getdate( {time=Timev ,precision=tonumber(P569precision)} , {})
					end
			end
	--	end
	return val -- .. (P569addon or '')
end

function propert(precision,timev,propertyID,entity)
	 P569precision = getprop('P569','precision',entity)
	 P569time =   getprop('P569','q',entity)
	 P570precision = getprop('P570','precision',entity)
	local P569addon = ''
--	if string.sub(P569time, 1, 1) == '-' then
--		P569time = '+' .. string.sub(P569time, 2)
--		P569addon =  ' ق م'
--	end
		if propertyID == 'P569' then
		val= getP569(precision, timev, entity, P570precision)
		
		elseif propertyID == 'P570' then
		val=  getP570(precision,timev,entity,P569precision,P569time)
	end
	return val-- .. (addon or '')
end

function p.getdate( datavalue ,datatype , options)
	local  propertyID = options.property
	local entity = mw.wikibase.getEntityObject(options.entityId)

	if datavalue.type == 'time'  
	then
		precision = tonumber(datavalue.value.precision)
		timev = datavalue.value.time
	
		tt = propert(precision,timev,propertyID,entity.id)
	return tt
	end
end

function p.test(frame)
	
	local propertyID = frame.args.property
	if propertyID and propertyID ~='' 
		then
			if propertyID == 'P569' 
				then
					val= getP569(frame.args.P569pre,frame.args.P569time,entity,frame.args.P570pre)
				elseif propertyID == 'P570' 
					then
						val= getP570(frame.args.P570pre,frame.args.P570time,entity,frame.args.P569pre,frame.args.P569time)
			end

		return val
	end
end


return p