"""Private candidate2: observed Excel YEARFRAC 1904 double-epoch compatibility."""
from pathlib import Path
import json,hashlib,datetime,difflib,shutil
root=Path(__file__).resolve().parents[1];out=root/'benchmarks/research/office-yearfrac2';source=Path.home()/'.cache/tabmorph/office-build/core-fc03117d3f8f2d7137d973e5db3b2514e8eba990';target=source/'scaddins/source/analysis/analysis.cxx';state=json.loads((out/'native-candidate.json').read_text());current=target.read_bytes();assert hashlib.sha256(current).hexdigest()==state['candidateSha256']
failed=Path.home()/'.cache/tabmorph/office-research/yearfrac2-failed1';failed.mkdir(exist_ok=True)
for name in ['soffice.wasm','soffice.js']:shutil.copyfile(source/'instdir/program'/name,failed/name)
(out/'analysis-failed1.cxx').write_bytes(current)
state.update(status='REJECTED after compiled independent validation: 199 distinct source-cell failures versus126 accepted baseline; no product changes',nativeWasm={'bytes':(failed/'soffice.wasm').stat().st_size,'sha256':hashlib.sha256((failed/'soffice.wasm').read_bytes()).hexdigest()},validation='benchmarks/research/office-legacy/2026-10-25T02-11-53-503Z-a41c5906/yearfrac2-excel-validation.json',observations=10664,failures=796)
(out/'native-candidate.json').write_text(json.dumps(state,indent=2)+'\n')
before=(out/'analysis-before.cxx').read_text();assert hashlib.sha256(before.encode()).hexdigest()==state['beforeSha256']
needle='''    double fRet = GetYearFrac(nullDate, nStartDate, nEndDate, mode,
        tabmorphAnalysisExcel1900(nullDate));'''
replacement='''    // Separate Excel 16.88 observations show that YEARFRAC on a 1904
    // workbook applies the 1462-day epoch displacement twice. Match this
    // observed compatibility behavior only for trusted Excel imports; keep
    // source-system range validation, ODS and other functions unchanged.
    const sal_Int32 excelEpoch = DateToDays(30, 12, 1899);
    const sal_Int32 yearfracEpoch = excel && nullDate == DateToDays(1, 1, 1904)
        ? nullDate + (nullDate - excelEpoch) : nullDate;
    double fRet = GetYearFrac(yearfracEpoch, nStartDate, nEndDate, mode,
        tabmorphAnalysisExcel1900(nullDate));'''
assert before.count(needle)==1;after=before.replace(needle,replacement);target.write_text(after)
(out/'yearfrac-1904-double-epoch.patch').write_text(''.join(difflib.unified_diff(before.splitlines(True),after.splitlines(True),fromfile='a/scaddins/source/analysis/analysis.cxx',tofile='b/scaddins/source/analysis/analysis.cxx')))
(out/'native-candidate2.json').write_text(json.dumps({'createdAt':datetime.datetime.now(datetime.timezone.utc).isoformat(),'status':'uncompiled private candidate2; no product changes','beforeSha256':hashlib.sha256(before.encode()).hexdigest(),'candidateSha256':hashlib.sha256(after.encode()).hexdigest(),'rule':'trusted Excel1904 YEARFRAC uses nullDate + (nullDate - canonical1900Epoch); other functions/ODS/1900 untouched','scope':'Observed Excel16.88 compatibility. Not a claim about internal Excel implementation, other versions or competitive superiority.'},indent=2)+'\n')
print('Candidate2 source patched; failed1 evidence preserved.')
