לדלג לתוכן

יחידה:גיל לערכי אישים: הבדלים בין גרסאות בדף

מתוך צפונות ויקי
יצירת דף עם התוכן "local dateParser = require( "Module:תאריך" ) local Errors = { ['future-date'] = 'קטגוריה:תאריכי חיים לא נכונים' } function in..."
 
מאין תקציר עריכה
שורה 4: שורה 4:
}
}


function invalidDate(Date, ApproxDate)
function invalidDate(frame, Date, ApproxDate)
local isValidDate, diffDate = pcall(dateParser.parseDateRange, Date, 'raw', false)
local isValidDate, diffDate = pcall(dateParser.parseDateRange, Date, 'raw', false)
if not isValidDate then
if not isValidDate then
שורה 44: שורה 44:
end
end
local errBirthDate = invalidDate(BirthDate, ApproxDate)
local errBirthDate = invalidDate(frame, BirthDate, ApproxDate)
if errBirthDate  then
if errBirthDate  then
return errBirthDate
return errBirthDate
end
end
if IsDead then  
if IsDead then  
local errDeathDate = invalidDate(DeathDate, ApproxDate)
local errDeathDate = invalidDate(frame, DeathDate, ApproxDate)
if errDeathDate then
if errDeathDate then
return errDeathDate-- invalid death date
return errDeathDate-- invalid death date

גרסה מ־23:23, 26 בפברואר 2016

ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:גיל לערכי אישים/תיעוד

local dateParser = require( "Module:תאריך" )
local Errors = {
	['future-date'] = '[[קטגוריה:תאריכי חיים לא נכונים]]'
}

function invalidDate(frame, Date, ApproxDate)
	local isValidDate, diffDate = pcall(dateParser.parseDateRange, Date, 'raw', false)
	if not isValidDate then
		-- invalid date
		return frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=Date,
			['מחרוזת']=Date
			} }
	end
	if tonumber(diffDate)<0 then 
		return Errors['future-date']-- date is in the future
	end
	
end

function ageCalc(frame)
	local BirthDate = frame.args['תאריך לידה']
	local DeathDate = frame.args['תאריך פטירה']
	local DisplayForAlive = frame.args['הצג אם בחיים']
	local DisplayForDead = frame.args['הצג אם נפטר']
	local Brackets = frame.args['סוגריים']
	local ApproxDate = frame.args['מקורב']
	local warnings = {}
	DisplayForAlive = (DisplayForAlive=='כן')
	DisplayForDead = (DisplayForDead=='כן')
	Brackets = (Brackets~='לא')
	
	local IsDead = (DeathDate~=nil and DeathDate~='')
	
	-- early termination
	if (DisplayForDead~=IsDead) or (DisplayForAlive==IsDead) then
		return ''
	end
	
	-- validate dates are valid
	if BirthDate==nil or BirthDate=='' then
		return -- missing date, nothing to do
	end
	
	local errBirthDate = invalidDate(frame, BirthDate, ApproxDate)
	if errBirthDate  then
		return errBirthDate
	end
	if IsDead then 
		local errDeathDate = invalidDate(frame, DeathDate, ApproxDate)
		if errDeathDate then
			return errDeathDate-- invalid death date
		end
		prefix = 'בגיל'
		dateRangeStr = string.format('%s - %s', BirthDate, DeathDate)
	else
		prefix = frame:expandTemplate{ title = 'לפי מגדר/בדוק', args = { 'בן', 'בת' } }
		dateRangeStr = BirthDate
	end
	prefix = prefix..'&nbsp;'

	local dateRangeStr
	local success, result = pcall(dateParser.parseDateRange, dateRangeStr , 'years', true)
	
	if success then
		local success, AgeYears = pcall(dateParser.parseDateRange, dateRangeStr , 'raw', true)
		local YearToSeconds = 60*60*24*365
		AgeYears = tonumber(AgeYears)/YearToSeconds
		if AgeYears > 139 then 
			table.insert(warnings, Errors['future-date'])
		elseif AgeYears>109 then 
			table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל 110]]')
		elseif AgeYears>100 then
			table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל מאה]]')
		end
		warnings = table.concat( warnings, '')
	else
		result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=DeathDate,
			['מחרוזת']=dateRangeStr
			} }
	end

	if Brackets then
		result = string.format('(%s)', result)
	end
	return string.format('%s <span style="white-space: nowrap;">%s</span>', warnings, result)
end


return {
	['גיל'] = ageCalc
}