Estimated Death Dates (code snippet)

Description

The EstimatedDeathDate() function usually produces a sensible Date, but some scenarios produce an erratic Date, and this Plugin function attempts to correct such errors. (The errors have been reported to Calico Pie.)

Usually if the Death Event has a Date then that overrides any estimated Date, but not always.

Some of the scenarios that cause erratic estimates still need to be clarified.

Requires: Get Day Number (code snippet)

Code

EstimatedDeathDates.fh_lua
-- Make EstimatedDeathDate EARLIEST <= LATEST <= DEAT/BURI/CREM Date --
function EstimatedDeathDates(ptrIndi,intGens)
	intGens = intGens or 2
	local dateMin = fhCallBuiltInFunction("EstimatedDeathDate",ptrIndi,"EARLIEST",intGens)
	local dateMax = fhCallBuiltInFunction("EstimatedDeathDate",ptrIndi,"LATEST",intGens)
	local dateMid = fhNewDatePt()
	if dateMin:IsNull() or dateMax:IsNull() then
		dateMin:SetNull()
		dateMax:SetNull()
	else
		local anyDate = false
		for intFact, strFact in ipairs ({"~.DEAT.DATE";"~.BURI.DATE";"~.CREM.DATE";}) do
			local datFact = fhGetValueAsDate(fhGetItemPtr(ptrIndi,strFact))
			if not datFact:IsNull() then	-- Find 1st Death/Burial/Cremation Date
				anyDate = true
				local datLast = datFact:GetDatePt1()	-- Last date = DatePt1 for Simple, Range, and Before
				local strType = datFact:GetSubtype()	-- Between = DatePt2 and After = DatePt1 + 100yrs
				if   strType == "Between" then datLast = datFact:GetDatePt2()
				elseif strType == "After" then datLast:SetValue(datLast:GetYear()+100,datLast:GetMonth(),datLast:GetDay(),datLast:GetYearDD()) end
				if dateMax:Compare(datLast) > 0 then dateMax = datLast end
				if dateMin:Compare(dateMax) > 0 then dateMin = dateMax end
				if strType ~= "After" then break end	-- Now EARLIEST <= LATEST <= Last date
			end
		end	
		if anyDate then						-- Need approximate MID year & month
			local intDays = ( GetDayNumber(dateMax) - GetDayNumber(dateMin) ) / 2
			local intYear,remYear = math.modf( intDays / 365.2422 )	-- Offset year @ 365.2422 days per year, and remainder fraction
			local intMnth = math.floor( remYear * 12 )	-- Offset month is remainder fraction of year * 12
			dateMid = fhCallBuiltInFunction("CalcDate",dateMin,intYear,intMnth)
		else
			dateMin:SetNull()				-- No Death/Burial/Cremation Date exists
			dateMax:SetNull()
		end
	end
	return { Min=dateMin; Mid=dateMid; Max=dateMax; }		-- Return EARLIEST, MID, LATEST dates
end -- function EstimatedDeathDates

Note

Preferably, the function should use the last pre-death fact, but the Normal Time Frame setting is not available to Plugins.

Usage

local arrDeathDate = EstimatedDeathDates(ptrIndi,9)
local datEarliest  = arrDeathDate.Min -- fhCallBuiltInFunction("EstimatedDeathDate",ptrIndi,"EARLIEST",9)
local datMiddle    = arrDeathDate.Mid -- fhCallBuiltInFunction("EstimatedDeathDate",ptrIndi,"MID",9)
local datLatest    = arrDeathDate.Max -- fhCallBuiltInFunction("EstimatedDeathDate",ptrIndi,"LATEST",9)