FANDOM


-- Calculates the number of years from a given date.
-- Written by Dser
 
local p = {}
local dateHelper = require("Dev:Date")
local tValidMonths = {january=1,february=2,march=3,april=4,
        may=5,june=6,july=7,august=8,september=9,october=10,november=11,december=12}
 
--% Entry point
--@ frame (table) A scribunto frame
--: (string) A string containing the number of years from a given date
function p.main(frame)
    local sYear = tonumber(frame.args[1])
    local sMonth = string.lower(frame.args[2] or "")
    local sDay = tonumber(frame.args[3])
 
    sMonth = tonumber(sMonth) or string.lower(sMonth)
    sMonth = tonumber(sMonth) or tValidMonths[sMonth]
 
    local isValidDate = p.isValidDate(sYear, sMonth, sDay)
    local result = nil
 
    if isValidDate then
        result = p.calculateYears(sYear, sMonth, sDay)
    else
        result = error("The date " .. sMonth .. " " .. sDay .. ", " ..sYear .. " is not valid")
    end
 
    return result
end
 
--% Calculates the number of years from a date
-- @ year (string) The year in the date
-- @ month (string) The month in the date
-- @ day (string) The day in the date
-- (number) The number of years between now and the date
function p.calculateYears(year, month, day)
    local currentYear = tonumber(os.date("%Y"))
    local result = nil
 
    local currMonthDay = os.date("%m%d")
    local oMonthDay = string.format("%02d%02d", month, day)
 
    if currentYear > tonumber(year) then
        if currMonthDay >= oMonthDay then
            result = currentYear - year
        else
            result = currentYear - year - 1
        end
 
    elseif currentYear == tonumber(year) then
        if currMonthDay >= oMonthDay then
            result = 0
        else
            result = error("The date '" .. month .. "/" .. day .. "/" .. year .. "' has not occurred yet!")
        end
 
    else
        result = error("The date '" .. month .. "/" .. day .. "/" .. year .. "' has not occurred yet!")
    end
 
    return result
end
 
--% Checks if the date is valid
--  Modified from Module:Asof (by Dessamator) on dev.wikia
--@ year (string) The required year
--@ month (string) The required month 
--@ day (string) The required day
--: (boolean) True if valid date
function p.isValidDate(year,month,day)
    local tValidDays = {31,28,31,30,31,30,31,30,31,30,31,30}
    local bYear,bMonth,bDay
 
    if tonumber(year) and tonumber(year)>-1 then 
        if dateHelper.isleapyear(year) then
             tValidDays[2]=29
        end
        bYear = true
    end
    if tValidMonths[month] or tValidDays[month] then
        bMonth =true
        if tonumber(month) and tonumber(day) and
            0<day and tValidDays[month]>=tonumber(day) then
            bDay = true
        end
    end
 
    return (bYear and bMonth and bDay)
end
 
return p

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.