Jump to content

Module:Unsigned/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by HouseBlaster (talk | contribs) at 22:53, 21 May 2025 (test). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

-- There's probably a way to use strptime or some other more sophisticated way, but you're not supposed to be using a non-timestamp as input anyway.

local function endswith(String,End)
	return End == '' or string.sub(String,-string.len(End)) == End
end

local function trim(s)
	return s:gsub("^%s+", ""):gsub("%s+$", ""):gsub("\226\128\142", "")
end

local function addUtcToStringIfItDoesNotEndWithUtc(s)
	if s == "" or endswith(s, "~~~~") then return s end
	if not endswith(s, "(UTC)") then
		return s .. " (UTC)"
	end
	return s
end

local function _main(args)
	local hopefullyTimestamp = args[1] or os.date('%H:%M, %d %B %Y (%Z)')
	return addUtcToStringIfItDoesNotEndWithUtc(trim(hopefullyTimestamp))
end

function p.getTimestamp(s)
	--gets the timestamp in the input
	--if nothing is found, returns nil
	return mw.ustring.match( s, '%d%d:%d%d, %d%d? %u%l+ %d%d%d%d %(UTC%)', 0 )
end

function p.getUsername(s)
	--gets the username in the input
	--------WARNING--------
	--this method assumes that *everything* besides the timestamp is, in fact, part of the username
	--it does no further username validation
	--------YOU HAVE BEEN WARNED--------
	return trim(mw.ustring.gsub( s, '%d%d:%d%d, %d%d? %u%l+ %d%d%d%d %(UTC%)', '', 1 ))
end

function p.main(frame)
	local args
	if type(frame.args) == 'table' then
		-- We're being called via #invoke. The args are passed through to the module
		-- from the template page, so use the args that were passed into the template.
		args = frame.args
	else
		-- We're being called from another module or from the debug console, so assume
		-- the args are passed in directly.
		args = frame
	end
	return _main(args)
end

return p