Jump to content

Module:Unsigned: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Empty strings
~~~~ is sent to the template before becoming a signature
Line 12: Line 12:


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

Revision as of 21:08, 7 December 2013

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.

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

function trim(s)
	return s:gsub("^%s+", ""):gsub("%s+$", "")
end

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

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

return p