Модуль:DaysBetweenDates

Материал из Викиновостей, свободного источника новостей
Документация
--[[
--------------------------------------------
-- DaysBetweenDates
-- Возрващает число дней между двумя датами
--------------------------------------------
]]

local DaysBetweenDates = {}

-- Wrapper for invoke
-- Usage:
-- {{#invoke:DaysBetweenDates|goInvoke|firstISODate|secondISODate}}
-- firstISODate > secondISODate
-- Example:
-- {{#invoke:DaysBetweenDates|goInvoke|2021-01-04|2021-01-01}}
function DaysBetweenDates.goInvoke(arg)
 if(arg.args[1] == nil) then return "Logical error DaysBetweenDates.goInvoke(arg.args[1]): expect firstISODate" end
 local firstISODate = arg.args[1]
 if(arg.args[2] == nil) then return "Logical error DaysBetweenDates.goInvoke(arg.args[2]): expect secondISODate" end
 local secondISODate = arg.args[2]
 return DaysBetweenDates.go(firstISODate, secondISODate)
end

-- Возвращает число дней между двумя датами
-- firstISODate > secondISODate
-- Example:
-- DaysBetweenDates.go(2021-01-04, 2021-01-01)
function DaysBetweenDates.go(firstISODate, secondISODate)
 dayFirstISODate = string.sub(firstISODate, 8)
 monthFirstISODate = string.sub(firstISODate, 6, 7)
 yearFirstISODate = string.sub(firstISODate, 0, 4)
 firstDate = os.time{day=dayFirstISODate, month=monthFirstISODate, year=yearFirstISODate}
 daySecondISODate = string.sub(secondISODate, 8)
 monthSecondISODate = string.sub(secondISODate, 6, 7)
 yearSecondISODate = string.sub(secondISODate, 0, 4)
 secondDate = os.time{day=daySecondISODate, month=monthSecondISODate, year=yearSecondISODate}
 daysbetween = math.floor(os.difftime(firstDate, secondDate)/(24*60*60))
 return daysbetween
end

return DaysBetweenDates