Module:Sandbox/Hellknowz/Test

This is an old revision of this page, as edited by Hellknowz (talk | contribs) at 08:25, 28 August 2013 (no upper year limit). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local main = {};

local monthIndices = {
    ['january'] = 1,
    ['february'] = 2,
    ['march'] = 3,
    ['april'] = 4,
    ['may'] = 5,
    ['june'] = 6,
    ['july'] = 7,
    ['august'] = 8,
    ['september'] = 9,
    ['october'] = 10,
    ['november'] = 11,
    ['december'] = 12
}

local monthDays = {
    [1] = 31,
    [2] = 29, -- will check below
    [3] = 31,
    [4] = 30,
    [5] = 31,
    [6] = 30,
    [7] = 31,
    [8] = 31,
    [9] = 30,
    [10] = 31,
    [11] = 30,
    [12] = 31
}

function tryParseDate(input)

    local matchDay, matchMonth, matchYear

    -- Try Y

    matchYear = input:match('^([0-9][0-9][0-9][0-9])$')

    if (matchYear ~= nil) then
        return nil, nil, tonumber(matchYear)
    end

    -- Try My
    matchMonth, matchYear = input:match('^([A-Za-z]+) ([0-9][0-9][0-9][0-9])$')

    if (matchMonth ~= nil) then
        local month = monthIndices[matchMonth:lower()]
        if (month == nil) then return nil end

        return nil, month, tonumber(matchYear)
    end

    -- Try dMy
    matchDay, matchMonth, matchYear = input:match('^([0-9][0-9]?) ([A-Za-z]+) ([0-9][0-9][0-9][0-9])$')

    if (matchDay ~= nil) then
        local month = monthIndices[matchMonth:lower()]
        if (month == nil) then return nil end

        return tonumber(matchDay), month, tonumber(matchYear)
    end

    -- Try Mdy
    matchMonth, matchDay, matchYear = input:match('^([A-Za-z]+) ([0-9][0-9]?), ([0-9][0-9][0-9][0-9])$')

    if (matchDay ~= nil) then
        local month = monthIndices[matchMonth:lower()]
        if (month == nil) then return nil end

        return tonumber(matchDay), month, tonumber(matchYear)
    end

    -- Try ymd
    matchYear, matchMonth, matchDay = input:match('^([0-9][0-9][0-9][0-9])-([0-9][0-9])-([0-9][0-9])$')

    if (matchDay ~= nil) then
        return tonumber(matchDay), tonumber(matchMonth), tonumber(matchYear)
    end

    -- Well, we ran out of valid date formats
    return nil

end

function main.parseDate(frame)

    local args = frame.args
    local input = args[1]

    local day, month, year = tryParseDate(input)

    if (year == nil) then
        return nil end --input .. ' -> is not a recognized pattern match' end

    if (year ~= nil and month == nil) then

        -- year only

        if (year < 1760) then -- TODO: make this a validateDate or something function
            return nil end --input .. ' -> year is too early (calendar discrepancy)' end

        --if (year > 2100) then
        --    return nil end --input .. ' -> year is too far into future' end

        return year

    end

    if (year ~= nil and month ~= nil and day == nil) then

        -- month and year only

        if (year < 1760) then
            return nil end --input .. ' -> year is too early (calendar discrepancy)' end

        --if (year > 2100) then
        --    return nil end --input .. ' -> year is too far into future' end

        -- Month comes from month name string, so always valid, can skip the check

        return string.format("%d-%02d", year, month)

    end

    if (year < 1760) then
        return nil end --input .. ' -> year is too early (calendar discrepancy)' end

    --if (year > 2100) then
    --    return nil end --input .. ' -> year is too far into future' end

    if (month == 0) then
        return nil end --input .. ' -> month is invalid == 0' end

    if (month > 12) then
        return nil end --input .. ' -> month is invalid > 12' end

    if (day == 0) then
        return nil end --input .. ' -> day is invalid == 0' end

    if (day > monthDays[month]) then
        return nil end --input .. ' -> day is invalid > ' .. monthDays[month] end

    -- February leap year check
    if (month == 2) then
        if (day == 29 and not ((year % 4 == 0) and (year % 100 ~= 0) or (year % 400 == 0))) then
             return nil --input .. ' -> day is invalid > 28'
        end
    end

    --parent stuff (infobox itself)
    --local parent = frame:getParent()
    --local parentArgs = parent.args

    return string.format("%d-%02d-%02d", year, month, day)

end
 
return main