Documentation for this module may be created at ሞድዩል:Wikidata/Date/doc

local p = {}
local Times = require( 'Module:Times')
local Romans = require('Module:Numbers').Roman
local link

local function toLink(tag)
    if link == 'no' then
        return tag
    else
        return '[['.. tag ..']]'
    end
end

function toformat(datehour, options)
	if options['dateFormat'] == 'timestamp' then
		return datehour.time
	end

	if datehour == nil then
		return 'ዘይተፈልጠ'
	end

    local tag  = datehour.label
    local timestamp = datehour.time
    local precision = datehour.precision    

    -- Decode the timestamp.
    --if true then return timestamp end
    local sign, yearWithoutSign, month, day = timestamp:match("(%W)(%d+)%-(%d+)%-(%d+)")
    
    yearWithoutSign = tonumber(yearWithoutSign)
    
     -- Get the era
    local era = ''
    
    if sign == '-' then
        era = ' ቅ.ክ.'
        yearWithSign = - yearWithoutSign
    elseif yearWithoutSign == 0 then -- To check if it is still like this.
        yearWithoutSign         = 1
        yearWithSign = -1
        era = ' ቅ.ክ.'
    else
        yearWithSign = yearWithoutSign
    end
    
    -- Inform the calendar
    local calendar 
    local showCalendar = ''
    
    if datehour.calendarmodel == 'http://www.wikidata.org/entity/Q1985786' then
        calendar = 'julian'
        showCalendar = '<sup>julian</sup>'
    else
        calendar = 'gregorian'
    end

    -- Report the link used in the link function. NOTE: It should not be defined as local
    link    = options.link

    if precision == 11 then      -- Date of which the year, month and day are known
        if options['dateFormat'] == 'ዓመት' then
            return toLink(yearWithoutSign) .. showCalendar .. era -- Return only the year
        else
           return Times.Time({day,month,yearWithSign;link=options.link;calendar=calendar;['uppercase']=options['uppercase']})
        end
    elseif precision == 10 then  -- Date of which the year and month are known
        if options['dateFormat'] == 'ዓመት' then
            return toLink(yearWithoutSign) .. showCalendar .. era -- Return only the year
        else
            return Times.Time({'',month,yearWithSign;link=options.link;calendar=calendar;['uppercase']=options['uppercase']})        
        end
    elseif precision == 9  then  -- Date of which the year is known
        return Times.Time({'','',yearWithSign;link=options.link;calendar=calendar;['uppercase']=options['uppercase']})        
    elseif precision == 8  then  -- Date of which the decade is known
        local decade = math.floor(yearWithoutSign/10)*10
        return toLink('ዓመታት '.. decade .. era) .. showCalendar            
    elseif precision == 7  then  -- Date of which the century is known
    	
        local century = Romans(math.floor((yearWithoutSign - 1)/100) + 1)
        
        if options['uppercase'] == 'yes' then
            return toLink('ዘመን ' .. century .. era)    .. showCalendar
        else
            return toLink('ዘመን ' .. century .. era)    .. showCalendar        
        end
    elseif precision == 6  then  -- Date of which the millennium is known
        local millennium = Romans(math.floor(yearWithoutSign/1000))
        return toLink(millennium .. ' ሽሕ ዓመት'.. era).. showCalendar
    elseif precision == 5  then  -- Date of which 10,000 years is known
         local millennium =  Romans(math.floor(yearWithoutSign/1000))
        return toLink(millennium .. ' ሽሕ ዓመት'.. era)
    elseif precision == 4  then  -- Date of which 100,000 years is known 
          local millennium =  Romans(math.floor(yearWithoutSign/1000))
        return toLink(millennium.. ' ሽሕ ዓመት'.. era)       
    elseif precision == 3  then  -- Date of which the millions of years are known
        return math.floor(yearWithoutSign/1000000) .. ' ሚልዮናት ዓመታት' .. era  
    elseif precision == 2  then  -- Date of which 10 million years is known
        return math.floor(yearWithoutSign/1000000) .. ' ሚልዮናት ዓመታት' .. era 
    elseif precision == 1  then  -- Date of which 100 million years is known
        return math.floor(yearWithoutSign/1000000) .. ' ሚልዮናት ዓመታት' .. era 
    else
        return "<span class=error>ዘይተፈልጠ ዓይነት ዳታ።</span>"        
    end    
end

function p.FormatDateTime(datehour, options, qualifyings)
    local idCircumstance
        
    if qualifyings                          and
       qualifyings["P1480"]                 and -- reference circumstances
       qualifyings["P1480"][1]              and
       qualifyings["P1480"][1]['datavalue'] and
       qualifyings["P1480"][1]['datavalue']['value'] then
               
        idCircumstance = qualifyings["P1480"][1]['datavalue']['value']['numeric-id']
               
        if idCircumstance == 5727902 then -- circa
            return '[[ኣስታት]]&nbsp;' .. toformat(datehour, options)
        end
    end
    
    -- Default value if formatting has not been known using the qualifying
    return toformat(datehour, options)
end

return p