Jump to content

Module:Unsigned: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
 
Fix
Line 3: Line 3:
-- 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.
-- 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 string.endswith(String,End)
function endswith(String,End)
return End=='' or string.sub(String,-string.len(End))==End
return End == '' or string.sub(String,-string.len(End)) == End
end

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


function addUtcToStringIfItDoesNotEndWithUtc(s)
function addUtcToStringIfItDoesNotEndWithUtc(s)
if not s:endswith("(UTC)") then
if not endswith(s, "(UTC)") then
return s .. " (UTC)"
return trim(s) .. " (UTC)"
end
end
return s
return s
Line 15: Line 19:


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

Revision as of 20:30, 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 not endswith(s, "(UTC)") then
		return trim(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(hopefullyTimestamp)
end

return p