Jump to content

Module:Time Ago

From The Petit Planet Wiki
Revision as of 11:19, 15 November 2025 by ReisuDesign (talk | contribs) (Created page with "local p = {} local lang = mw.language.getContentLanguage() local lib = require('Module:Feature') local search = lib.inArray local ne = lib.isNotEmpty local i18n = require('Module:I18n').loadMessages('Time Ago') local units = { { terms = {'year','y'}, higher = {''}, insec = 31557600 }, { terms = {'month','mth'}, higher = {'year','y'}, insec = 2629800 }, { terms = {'week','w'}, higher = {'year','y','month','mth'}, insec = 604800 }, { terms = {'d...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}
local lang = mw.language.getContentLanguage()
local lib = require('Module:Feature')
local search = lib.inArray
local ne = lib.isNotEmpty
local i18n = require('Module:I18n').loadMessages('Time Ago')

local units = {
	{
		terms = {'year','y'},
		higher = {''},
		insec = 31557600
	},
	{
		terms = {'month','mth'},
		higher = {'year','y'},
		insec = 2629800
	},
	{
		terms = {'week','w'},
		higher = {'year','y','month','mth'},
		insec = 604800
	},
	{
		terms = {'day','d'},
		higher = {'year','y','month','mth','week','w'},
		insec = 86400
	},
	{
		terms = {'hour','h'},
		higher = {'year','y','month','mth','week','w','day','d'},
		insec = 3600
	},
	{
		terms = {'minute','min'},
		higher = {'year','y','month','mth','week','w','day','d','hour','h'},
		insec = 60
	},
	{
		terms = {'second','s'},
		higher = {'year','y','month','mth','week','w','day','d','hour','h','minute','min'},
		insec = 1
	}
}

function p.main( frame )
	local args = require( 'Module:Arguments' ).getArgs( frame, {
		wrappers = {'Template:Time Ago'}
	})

	return p._main(args)
end

function p._main(args)
	-- Check that a timestamp was entered, return blank if it wasn't.
	if not args[1] then return '' end
	
	-- Check that the entered timestamp is valid. If it isn't, then give an error message.
	local success, inputTime = pcall(lang.formatDate, lang, 'xnU', args[1])
	assert(success, i18n:msg('parse-error'))
	
	--Output formats
	if args.include then
		return p.include(args,inputTime)
	else
		return p.last(args,inputTime)
	end
end

function p.include(args,inputTime)
	local output = ''
	local include = lib.split(args.include,',')
	if (type(include) == 'string') then include = {include} end
	local hidelabel = tostring(args['hidelabel']) == '1'
	local hidefixes = tostring(args['hidefixes']) == '1'
	
	-- Store the difference between the current time and the inputted time, as well as its absolute value.
	local timeDiff = lang:formatDate('xnU') - inputTime
	local absTimeDiff = math.abs(timeDiff)
	
	local ValueTable = {'','','','','','',''}
	local trueVal = {}
	for k,v in ipairs(units) do
		local Value = 0
		local i,IsIn = false,false
		for K,V in ipairs(include) do
			if (absTimeDiff >= v.insec and (search(v.terms,V))) then i = true end
		end
		while i == true do
			Value=Value+1
			absTimeDiff = absTimeDiff - v.insec
			if absTimeDiff < v.insec then i = false end
		end
		if Value > 0 then
			if hidelabel then
				ValueTable[k] = tostring(Value)
			else
				ValueTable[k] = i18n:msg(v.terms[1], Value, (Value > 1 and 2 or 1))
			end
		end
	end
	for k,v in ipairs(ValueTable) do
		if ne(v) then
			trueVal[#trueVal+1] = v
		end
	end
	if (#trueVal == 0) then
		local start,i = false,true
		for k,v in ipairs(units) do
			if start then
				if (absTimeDiff >= v.insec) then
					local Value = 0
					while i == true do
						Value=Value+1
						absTimeDiff = absTimeDiff - v.insec
						if absTimeDiff < v.insec then i = false end
					end
					if hidelabel then
						output = tostring(Value)
					else
						output = i18n:msg(v.terms[1], Value, (Value > 1 and 2 or 1))
					end
					if (timeDiff < 0 and (not hidefixes) and (not hidelabel) and ne(output)) then output = i18n:msg('future' .. (ne(args.lowercase) and '-low' or ''), output) end
					if (timeDiff > 0 and (not hidefixes) and (not hidelabel) and ne(output)) then output = i18n:msg('past', output) end
					return output
				end
			elseif search(v.terms,include[#include]) then
				start = true
			end
		end
	end
	output = table.concat(trueVal,', ')
	if (timeDiff < 0 and (not hidefixes) and (not hidelabel) and ne(output)) then output = i18n:msg('future' .. (ne(args.lowercase) and '-low' or ''), output)  end
	if (timeDiff > 0 and (not hidefixes) and (not hidelabel) and ne(output)) then output = i18n:msg('past', output) end
	return output
end

function p.last(args,inputTime)
	local output = ''
	local last = args.last or 'sweet potato'
	local hidelabel = tostring(args['hidelabel']) == '1'
	local hidefixes = tostring(args['hidefixes']) == '1'
	
	-- Store the difference between the current time and the inputted time, as well as its absolute value.
	local timeDiff = lang:formatDate('xnU') - inputTime
	local absTimeDiff = math.abs(timeDiff)
	
	local ValueTable = {'','','','','','',''}
	local trueVal = {}
	for k,v in ipairs(units) do
		local Value = 0
		local i = false
		if (absTimeDiff >= v.insec and (not search(v.higher,last))) then i = true end
		for iter=1,k do
			if (lib.isEmpty(ValueTable[iter]) and absTimeDiff >= v.insec) then
				i = true
			else 
				i = false
			end
		end
		while i == true do
			Value=Value+1
			absTimeDiff = absTimeDiff - v.insec
			if absTimeDiff < v.insec then i = false end
		end
		if Value > 0 then
			if hidelabel then
				ValueTable[k] = tostring(Value)
			else
				ValueTable[k] = i18n:msg(v.terms[1], Value, (Value > 1 and 2 or 1))
			end
		end
	end
	for k,v in ipairs(ValueTable) do
		if (((not search(units[k].higher,last)) or search(units[k].terms,last)) and ne(v)) then
			trueVal[#trueVal+1] = v
		elseif ((#trueVal == 0) and ne(v)) then
			trueVal[1] = v
		end
	end
	output = table.concat(trueVal,', ')
	if (timeDiff < 0 and (not hidefixes) and (not hidelabel) and ne(output)) then output = i18n:msg('future' .. (ne(args.lowercase) and '-low' or ''), output)  end
	if (timeDiff > 0 and (not hidefixes) and (not hidelabel) and ne(output)) then output = i18n:msg('past', output) end
	return output
end

return p