end
return dateStr
end  
-- =======================================================================
local function checkAliases(str1, str2, sType)
-- some inputs have many aliases - reconcile them and ensure string is playing a proper role
local out = ''
if str1 and str1~='' then
local a = i18n.Synonyms[str1] -- look up synonyms of "str1"
if a then
out = a[1]
else
p.Error = string.format('<span>Error in Module:Complex date: %s is not recognized.</span>', str1)
end
elseif str2 and str2~='' then -- if "str1" of type "sType" is empty than maybe ...
local a = i18n.Synonyms[str2] -- ..."str2" is of the same type and is not empty
if a and a[2]==sType then
out = a[1]
str2 = ''
end
end
return out, str2
end  
-- =======================================================================
local function datePrecision(dateStr, units)
-- "in this module "Units" is a string like millennium, century, or decade
-- "precision" is wikibase compatible date precision number: 6=millennium, 7=century, 8=decade, 9=year, 10=month, 11=day
-- based on string or numeric input calculate "Units" and "precision"
local precision
if type(units)=='number' then
precision = units
if precision>11 then precision=11 end -- clip the range of precision values
if precision==6 then units='millennium'
elseif precision==7 then units='century'
elseif precision==8 then units='decade'
else units = ''
end
elseif type(units)=='string' then
units = string.lower(units)
if units=='millennium' then precision=6
elseif units=='century' then precision=7
elseif units=='decade' then precision=8
else precision=9
end
end
if units=='' or precision==9 then
local sLen = mw.ustring.len(dateStr)
if sLen<= 4 then precision=9
elseif sLen== 7 then precision=10
elseif sLen>=10 then precision=11
end
units=''
end
if precision==6 and dateStr.match( dateStr, '%d000' )~=nil then
dateStr = tostring(math.floor(tonumber(dateStr)/1000) +1)
elseif precision==7 and mw.ustring.match( dateStr, '%d%d00' )~=nil then
dateStr = tostring(math.floor(tonumber(dateStr)/100) +1)
end  
return dateStr, units, precision
end  
-- =======================================================================
local function isodate2timestamp(dateStr, precision, era)
-- convert date string to timestamps used by Quick Statements
local tStamp = nil
if era == 'ah' or precision<6 then
return nil
elseif era ~= '' then
local eraLUT = {ad='+', bc='-', bp='-' }
era = eraLUT[era]
else
era='+'
end  
-- convert isodate to timestamp used by quick statements
if precision>=9 then
if string.match(dateStr,"^%d%d%d%d$") then -- if YYYY format
tStamp = era .. dateStr .. '-00-00T00:00:00Z/9'
elseif string.match(dateStr,"^%d%d%d%d%-%d%d$") then -- if YYYY-MM format
tStamp = era .. dateStr .. '-00T00:00:00Z/10'
elseif string.match(dateStr,"^%d%d%d%d%-%d%d%-%d%d$") then -- if YYYY-MM-DD format
tStamp = era .. dateStr .. 'T00:00:00Z/11'
end
elseif precision==8 then -- decade
tStamp = era .. dateStr .. '-00-00T00:00:00Z/8'
elseif precision==7 then -- century
local d = tostring(tonumber(dateStr)-1)
tStamp = era .. d .. '50-00-00T00:00:00Z/7'
elseif precision==6 then
local d = tostring(tonumber(dateStr)-1)
tStamp = era .. d .. '500-00-00T00:00:00Z/6'
end  
return tStamp
end  
-- =======================================================================
local function oneDateQScode(dateStr, adj, era, precision)
-- create QuickStatements string for "one date" dates
local outputStr = ''  
local d = isodate2timestamp(dateStr, precision, era)
if not d then
return ''
end
local rLUT = { early='Q40719727' , mid='Q40719748', late='Q40719766',
['1quarter']='Q40690303' , ['2quarter']='Q40719649' , ['3quarter']='Q40719662', ['4quarter']='Q40719674',
spring='Q40720559' , summer='Q40720564' , autumn='Q40720568' , winter='Q40720553',
firsthalf='Q40719687', secondhalf='Q40719707' }
local qLUT = {['from']='P580', ['until']='P582', ['after']='P1319', ['before']='P1326', ['by']='P1326'}  
local refine = rLUT[adj]
local qualitier = qLUT[adj]  
if adj=='' then
outputStr = d
elseif adj=='circa' then
outputStr = d..",P1480,Q5727902"
elseif refine then
outputStr = d..",P4241,"..refine
elseif precision>7 and qualitier then
local century = string.gsub(d, 'Z%/%d+', 'Z/7')
outputStr = century ..",".. qualitier ..","..d
end
return outputStr
end  
-- =======================================================================
local function twoDateQScode(date1, date2, state)
-- create QuickStatements string for "two date" dates
if state.adj1~=* or state.adj2~=* or state.era1~=state.era2 then
return '' -- QuickStatements string are not generated for two date phrases with adjectives
end
local outputStr = ''
local d1 = isodate2timestamp(date1, state.precision1, state.era1)
local d2 = isodate2timestamp(date2, state.precision2, state.era2)
if (not d1) or (not d2) then
return ''
end
-- find date with lower precision in common to both dates
local cd
local year1 = tonumber(string.sub(d1,2,5))
local year2 = tonumber(string.sub(d2,2,5))
local k = 0
for i = 1,10,1 do
if string.sub(d1,1,i)==string.sub(d2,1,i) then
k = i -- find last matching letter
end
end
if k>=9 then -- same month, since "+YYYY-MM-" is in common
cd = isodate2timestamp(string.sub(d1,2,8), 10, state.era1)
elseif k>=6 and k<9 then -- same year, since "+YYYY-" is in common
cd = isodate2timestamp(tostring(year1), 9, state.era1)
elseif k==4 then -- same decade(k=4, precision=8), since "+YYY" is in common
cd = isodate2timestamp(tostring(year1), 8, state.era1)
elseif k==3 then -- same century(k=3, precision=7) since "+YY" is in common
local d = tostring(math.floor(year1/100) +1) -- convert 1999 -> 20
cd = isodate2timestamp( d, 7, state.era1)
elseif k==2 then -- same millennium (k=2, precision=6), since "+Y" is in common
local d = tostring(math.floor(year1/1000) +1) -- convert 1999 -> 2
cd = isodate2timestamp( d, 6, state.era1)
end
if not cd then
return ''
end
--if not cd then
-- return '
error: ' .. d1.." / " .. d2.." / ".. (cd or '') .." / ".. string.sub(d1,2,5).." / " .. string.sub(d2,2,5).." / " .. tostring(k)
--end  
---
if (state.conj=='from-until') or (state.conj=='and' and year1==year2-1) then
outputStr = cd ..",P580,".. d1 ..",P582,".. d2
elseif (state.conj=='between') or (state.conj=='or' and year1==year2-1) then
outputStr = cd ..",P1319,".. d1 ..",P1326,".. d2
elseif state.conj=='circa2' then
outputStr = cd ..",P1319,".. d1 ..",P1326,".. d2 ..",P1480,Q5727902"
end  
return outputStr
end  
-- =======================================================================
local function processInputParams(conj, adj1, date1, units1, era1, adj2, date2, units2, era2, lang, passNr)  
-- process inputs and save date in state array
local state = {}
state.conj = string.lower(conj or '')
state.adj1 = string.lower(adj1 or '')
state.adj2 = string.lower(adj2 or '')
state.era1 = string.lower(era1 or '')
state.era2 = string.lower(era2 or '')
state.units1 = string.lower(units1 or '')
state.units2 = string.lower(units2 or '')  
-- if date 1 is missing but date 2 is provided than swap them
if date1 == * and date2 ~= * then
date1 = date2
date2 = ''
state = {adj1 = state.adj2, era1 = state.era2, units1 = state.units2,
adj2 = *, era2 = *, units2 = '', conj=state.conj, num=1}
end
if date2 ~= '' then state.nDates = 2
elseif date1 ~= '' then state.nDates = 1
else state.nDates = 0
end  
-- reconcile alternative names for text inputs
local conj = checkAliases(state.conj ,'' ,'j')
state.adj1 ,conj = checkAliases(state.adj1 ,conj,'a')
state.units1,conj = checkAliases(state.units1,conj,'p')
state.era1 ,conj = checkAliases(state.era1 ,conj,'e')
state.special,conj = checkAliases('',conj,'c')
state.adj2 = checkAliases(state.adj2 ,'','a')
state.units2 = checkAliases(state.units2,'','p')
state.era2 = checkAliases(state.era2 ,'','e')
state.conj = conj
state.lang = lang
if p.Error~=nil then
return nil
end  
-- calculate date precision value
date1, state.units1, state.precision1 = datePrecision(date1, state.units1)
date2, state.units2, state.precision2 = datePrecision(date2, state.units2)