לדלג לתוכן

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

מתוך צפונות ויקי
מאין תקציר עריכה
מאין תקציר עריכה
שורה 28: שורה 28:
local ApproxDate = frame.args['מקורב']
local ApproxDate = frame.args['מקורב']
local warnings = {}
local warnings = {}
local dateRangeStr
DisplayForAlive = (DisplayForAlive=='כן')
DisplayForAlive = (DisplayForAlive=='כן')
DisplayForDead = (DisplayForDead=='כן')
DisplayForDead = (DisplayForDead=='כן')
שורה 48: שורה 49:
return errBirthDate
return errBirthDate
end
end
if IsDead then  
if IsDead then  
local errDeathDate = invalidDate(frame, DeathDate, ApproxDate)
local errDeathDate = invalidDate(frame, DeathDate, ApproxDate)
שורה 61: שורה 63:
prefix = prefix..' '
prefix = prefix..' '


local dateRangeStr
local success, result = pcall(dateParser.parseDateRange, dateRangeStr , {'years'}, true)
local success, result = pcall(dateParser.parseDateRange, dateRangeStr , 'years', true)
 
if success then
if success then
result= mw.ustring.gsub(result,"כ־(.+) שנים","%1 בערך")
result = prefix..mw.ustring.gsub(result," שנים","")
local success, AgeYears = pcall(dateParser.parseDateRange, dateRangeStr , 'raw', true)
local success, AgeYears = pcall(dateParser.parseDateRange, dateRangeStr , 'raw', true)
local YearToSeconds = 60*60*24*365
local YearToSeconds = 60*60*24*365
שורה 75: שורה 78:
table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל מאה]]')
table.insert(warnings, '[[קטגוריה:אישים שהגיעו לגיל מאה]]')
end
end
warnings = table.concat( warnings, '')
else
else
result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = {  
result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = {  
['מקורב']=ApproxDate,
['מקורב']=ApproxDate,
שורה 84: שורה 87:
end
end


warnings = table.concat( warnings, '')
if Brackets then
if Brackets then
result = string.format('(%s)', result)
result = string.format('(%s)', result)

גרסה מ־23:58, 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 = {}
	local dateRangeStr
	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 success, result = pcall(dateParser.parseDateRange, dateRangeStr , {'years'}, true)

	if success then
		result= mw.ustring.gsub(result,"כ־(.+) שנים","%1 בערך")
		result = prefix..mw.ustring.gsub(result," שנים","")
		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
	else

		result = '?' .. frame:expandTemplate{ title = 'גיל לערכי אישים/בעיה', args = { 
			['מקורב']=ApproxDate,
			['נוסף']=DeathDate,
			['מחרוזת']=dateRangeStr
			} }
	end

	warnings = table.concat( warnings, '')
	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
}