Module:Weekly site information: Difference between revisions

From Our World of Text Wiki
Jump to navigation Jump to search
(Created module.)
 
(Removed padding from date numbers (i.e., Dec 1 instead of Dec 01) and added error handling.)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}


function p.lastReleaseNumber(frame)
local mw_error = require('Module:Error').error
    -- Define the start date of Week 1
    local week1Start = os.time{year=2024, month=9, day=8}


    -- Get current date
local first_date = os.time{year=2024, month=9, day=8}
    local currentDate = os.time()
local curr_date = os.time()
local diff = os.difftime(curr_date, first_date) / (24 * 60 * 60)


    -- Calculate the difference in days from the start of Week 1 to the current date
local function release(offset)
    local daysDifference = os.difftime(currentDate, week1Start) / (24 * 60 * 60)
offset = offset or 0


    -- Return the last Weekly Briefing Release
local release_num = math.floor(diff / 7) + offset
    return math.floor(daysDifference / 7)
 
local end_date = os.date("*t", curr_date - (diff % 7) * 24 * 60 * 60)
end_date.day = end_date.day - (end_date.wday - 1) + offset * 7
 
local start_date = os.date("*t", os.time{year=end_date.year, month=end_date.month, day=end_date.day - 7})
 
return string.format("Weekly Briefing Release %d %s - %s",
release_num,
os.date("%b %d", os.time(start_date)):gsub(" 0", " "),
os.date("%b %d", os.time(end_date)):gsub(" 0", " ")
)
end
 
function p._main(link_text, offset)
if offset then
return "[[" .. release(offset) .. "|" .. link_text .. "]]"
else
for i=1,-math.floor(diff / 7),-1 do
mw.log(release(i))
if mw.title.new(release(i), "").exists then
return "[[" .. release(i) .. "|" .. link_text .. "]]"
end
end
return mw_error{"No Releases found. Something has gone very, very wrong."}
end
end
 
function p.main(frame)
local link_text = frame:getParent().args[1] or "Weekly Site Information"
local offset = tonumber(frame:getParent().args['offset'])
return p._main(link_text, offset)
end
end


return p
return p

Latest revision as of 13:54, 2 December 2024

local p = {}

local mw_error = require('Module:Error').error

local first_date = os.time{year=2024, month=9, day=8}
local curr_date = os.time()
local diff = os.difftime(curr_date, first_date) / (24 * 60 * 60)

local function release(offset)
	offset = offset or 0

	local release_num = math.floor(diff / 7) + offset

	local end_date = os.date("*t", curr_date - (diff % 7) * 24 * 60 * 60)
	end_date.day = end_date.day - (end_date.wday - 1) + offset * 7

	local start_date = os.date("*t", os.time{year=end_date.year, month=end_date.month, day=end_date.day - 7})

	return string.format("Weekly Briefing Release %d %s - %s",
		release_num,
		os.date("%b %d", os.time(start_date)):gsub(" 0", " "),
		os.date("%b %d", os.time(end_date)):gsub(" 0", " ")
	)
end

function p._main(link_text, offset)
	if offset then
		return "[[" .. release(offset) .. "|" .. link_text .. "]]"
	else
		for i=1,-math.floor(diff / 7),-1 do
			mw.log(release(i))
			if mw.title.new(release(i), "").exists then
				return "[[" .. release(i) .. "|" .. link_text .. "]]"
			end
		end
		return mw_error{"No Releases found. Something has gone very, very wrong."}
	end
end

function p.main(frame)
	local link_text = frame:getParent().args[1] or "Weekly Site Information"
	local offset = tonumber(frame:getParent().args['offset'])
	return p._main(link_text, offset)
end

return p