end  
-- calculate date precision value
date1, state.units1, state.precision1 = datePrecision(date1, state.units1)
date2, state.units2, state.precision2 = datePrecision(date2, state.units2)  
-- Handle special cases
-- Some complex phrases can be created out of simpler ones. Therefore on pass # 1 we try to create
-- the phrase using complex phrase and if that is not found than on the second pass we try to build
-- the phrase out of the simpler ones
if passNr==1 then
if state.adj1=='circa' and state.nDates == 2 then
state.conj = 'circa2'
state.adj1 = ''
state.adj2 = ''
end
if state.nDates == 2 and state.adj1=='late' and state.adj2=='early' and state.conj=='and'
and state.units1==state.units2 and state.era1==state.era2 then
if state.units1=='century' then
state.conj='turn of the century'
elseif state.units1=='decade' then
state.conj='turn of the decade'
elseif state.units1=='' then
state.conj='turn of the year'
end
state.adj1 = ''
state.adj2 = ''
state.units1 = ''
state.units2 = ''
end
end  
state.adj, state.era, state.units, state.precision = state.adj1, state.era1, state.units1, state.precision1
return date1, date2, state
end  
-- ==================================================
-- === External functions ===========================
-- ==================================================  
function p.Era
-- process inputs
local dateStr
local args = .args
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
args.lang = :callParserFunction( "int", "lang" ) -- get user's chosen language
end
local lang = args['lang']
local dateStr = args['date'] or ''
local eraType = string.lower(args['era'] or '')  
dateStr = ISOdate(dateStr, lang, *, *, 1)
if eraType then
eraType = checkAliases(eraType ,'','e')
dateStr = translatePhrase(dateStr, '', eraType, lang, {})
end
return dateStr
end  
-- =======================================================================
function p._complex_date(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, passNr)
local Output=''
local state  
-- process inputs and save date in state array
date1, date2, state = processInputParams(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, passNr)
if p.Error~=nil then
return nil
end  
local errorStr = string.format(
'\n*conj=%s, adj1=%s, era1=%s, unit1=%s, prec1=%i, adj2=%s, era2=%s, unit2=%s, prec2=%i, special=%s',
state.conj, state.adj1, state.era1, state.units1, state.precision1,
state.adj2, state.era2, state.units2, state.precision2, state.special)  
-- call specialized functions
local QScode = ''
if state.special~='' then
Output = otherPhrases(date1, date2, state.special, state.era1, lang, state)
elseif state.conj~='' then
QScode = twoDateQScode(date1, date2, state)
Output = twoDatePhrase(date1, date2, state, lang)
elseif state.adj1~=* or state.era1~=* or state.units1~='' then
Output = oneDatePhrase(date1, state.adj1, state.era1, state.units1, lang, 1, nil, state)
QScode = oneDateQScode(date1, state.adj1, state.era1, state.precision1)
elseif date1~='' then
Output = ISOdate(date1, lang, '', 'dtstart', '100-999')
end
if p.Error~=nil then
return errorStr
end  
-- if there is any wikicode in the string than execute it
if mw.ustring.find(Output, '{') then
Output = mw.getCurrentFrame:preprocess(Output)
end
if QScode and #QScode>0 then
QScode = ' <div>date QS:P,' .. QScode .. '</div>'
end  
return Output .. QScode
end  
-- =======================================================================
function p._complex_date_cer(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, certainty, lang)
-- same as p._complex_date but with extra parameter for certainty: probably, possibly, presumably, etc.
local dateStr = p._complex_date(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, 1)
certainty = checkAliases(certainty, conj, 'r')
local LUT = {probably='Q56644435', presumably='Q18122778', possibly='Q30230067', circa='Q5727902' }
if certainty and LUT[certainty] then
local state = {}
date1, date2, state = processInputParams(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, 1)
dateStr = translatePhrase(dateStr, '', certainty, lang, state)
dateStr = string.gsub(dateStr, '(%<div%>date QS:P,[^%<]+)(%</div%>)', '%1,P1480,' .. LUT[certainty] .. '%2' )
end
return dateStr
end  
-- =======================================================================
function p.complex_date
-- process inputs
local dateStr
local args = .args
if not (args.lang and mw.language.isSupportedLanguage(args.lang)) then
args.lang = :callParserFunction( "int", "lang" ) -- get user's chosen language
end
local date1 = args['date1'] or args['2'] or args['date'] or ''
local date2 = args['date2'] or args['3'] or ''
local conj = args['conj'] or args['1'] or ''
local adj1 = args['adj1'] or args['adj'] or ''
local adj2 = args['adj2'] or ''
local units1 = args['precision1'] or args['precision'] or ''
local units2 = args['precision2'] or args['precision'] or ''
local era1 = args['era1'] or args['era'] or ''
local era2 = args['era2'] or args['era'] or ''
local certainty = args['certainty']
local lang = args['lang']  
dateStr = p._complex_date_cer(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, certainty, lang)
if p.Error~=nil then
dateStr = p.Error .. ''
end
return dateStr
end  
return p