Module:Month

From Linux Web Expert

Revision as of 07:33, 1 June 2020 by imported>Amire80 (+ month_number_two_digit)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Month/doc

local p = {}

local months = {
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December",
}

local month_numbers = {}

for number, name in pairs(months) do
	month_numbers[name] = number
end

function valid_month(month)
	month_number = tonumber(month)

	if (month_number == nil) then
		month_number = month_numbers[month]
	end

	if (month_number == nil or month_number < 1 or month_number > 12) then
		return nil
	end

	return month_number
end

function invalid_month_message(month)
	return string.format("%s is not a valid month", month)
end

function valid_year(year)
	year_number = tonumber(year)

	if (year_number == nil) then
		return nil
	end

	return year_number
end

function invalid_year_message(year)
	return string.format("%s is not a valid year", year)
end

function next_month(this_month, this_year)
	month = {}

	this_month_number = valid_month(this_month)

	if (not this_month_number) then
		month["month"] = invalid_month_message(this_month)
		month["month_number"] = invalid_month_message(this_month)
	end

	this_year_number = valid_year(this_year)

	if (not this_year_number) then
		month["year"] = invalid_year_message(this_year)
	end

	if (this_month_number == 12) then
		month["month"] = months[1]
		month["month_number"] = 1
		if (this_year_number) then
			month["year"] = this_year_number + 1
		end
	else
		if (this_month_number) then
			next_month_number = this_month_number + 1
			month["month"] = months[next_month_number]
			month["month_number"] = next_month_number
		end
		if (this_year_number) then
			month["year"] = this_year_number
		end
	end

	return month
end

function p.next_month_name(frame)
	this_month = frame.args[1]
	this_year = frame.args[2]
	
	return next_month(this_month, this_year).month
end

function p.next_month_number(frame)
	this_month = frame.args[1]
	this_year = frame.args[2]
	
	return next_month(this_month, this_year).month_number
end

function p.next_month_year(frame)
	this_month = frame.args[1]
	this_year = frame.args[2]
	
	return next_month(this_month, this_year).year
end

function previous_month(this_month, this_year)
	month = {}

	this_month_number = valid_month(this_month)

	if (not this_month_number) then
		month["month"] = invalid_month_message(this_month)
	end

	this_year_number = valid_year(this_year)

	if (not this_year_number) then
		month["year"] = invalid_year_message(this_year)
	end

	if (this_month_number == 1) then
		month["month"] = months[12]
		if (this_year_number) then
			month["year"] = this_year_number - 1
		end
	else
		previous_month_number = this_month_number - 1
		if (this_month_number) then
			month["month"] = months[previous_month_number]
			month["month_number"] = previous_month_number
		end
		if (this_year_number) then
			month["year"] = this_year_number
		end
	end

	return month
end

function p.previous_month_name(frame)
	this_month = frame.args[1]
	this_year = frame.args[2]
	
	return previous_month(this_month, this_year).month
end

function p.previous_month_number(frame)
	this_month = frame.args[1]
	this_year = frame.args[2]
	
	return previous_month(this_month, this_year).month_number
end

function p.previous_month_year(frame)
	this_month = frame.args[1]
	this_year = frame.args[2]
	
	return previous_month(this_month, this_year).year
end

function p.month_number(frame)
	this_month = frame.args[1]

	return month_numbers[this_month]
end

function p.month_number_two_digit(frame)
	this_month = frame.args[1]
	return string.format("%02d", valid_month(this_month))
end

return p