Module:TimeAgo

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

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

Code

-- This module is meant to replace Template:Time ago and give it better i18n support
-- This module is in beta, some functionality is still missing.
local p = {}

function p.main(frame)
	local pframe = frame:getParent()
	local config = frame.args
	local args = pframe.args
    
	local acceptedintervals = {
		[1] = "years",
		[2] = "weeks",
		[3] = "days",
		[4] = "hours",
		[5] = "minutes", 
		[6] = "seconds",
	}
	local intervals = {
		years = 5,
		weeks = 4,
		days = 3,
		hours = 2,
		minutes = 1, 
		seconds = 0,
	}
	local magnitude = {}
	if config[1] and string.find(config[1], "{{") then
		time = frame:preprocess( config[1] )
	else
		time = config[1] or os.date()
	end
	if mw.language.isKnownLanguageTag(mw.title.getCurrentTitle().subpageText) == true then
		lang = mw.title.getCurrentTitle().subpageText
	else
		lang = frame:preprocess("{{int:lang}}")
	end
	local seconds = mw.getLanguage(lang):formatDate("U") -  mw.getLanguage(lang):formatDate("U", time)
	if config.magnitude == "months" then 
		magnitude1 = "weeks"
	else
		magnitude1 = config.magnitude
	end
	if config.magnitude and intervals[magnitude1] then
		table.insert(magnitude, magnitude1)
		duration = mw.getLanguage(lang):formatDuration(seconds, magnitude)
	elseif config["min_magnitude"] and intervals[config["min_magnitude"]] then
		local k = 6
		for i = 1, intervals[config["min_magnitude"]] do
			table.remove(acceptedintervals, k)
			k = k - 1
		end
		duration = mw.getLanguage(lang):formatDuration(seconds, acceptedintervals)
	else
		duration = mw.getLanguage(lang):formatDuration(seconds, acceptedintervals)
	end
	dayfirst, dayend = mw.ustring.find(duration, "%d+ day")
	weekfirst, weekend = mw.ustring.find(duration, "%d+ week")
	if weekfirst then
		local monthsn = math.floor(mw.ustring.sub(duration, weekfirst, weekend - 5)/4)
		local months = mw.message.new("Months", monthsn):inLanguage(lang):plain()
		local weeksn = mw.ustring.sub(duration, weekfirst, weekend - 5) - monthsn*4
		local weeks = mw.message.new("Weeks", weeksn ):inLanguage(lang):plain()
		if monthsn ~= 0 and weeksn ~= 0 then
			timeago = mw.ustring.gsub(duration, "%d+ weeks", frame:preprocess(months) .. frame:preprocess(weeks))
		elseif weeksn == 0 then
			timeago = mw.ustring.gsub(duration, "%d+ weeks", frame:preprocess(months) )
		else
			timeago = duration
		end
	elseif dayfirst then
		local monthsn = math.floor(mw.ustring.sub(duration, dayfirst, dayend - 4)/31)
		local months = mw.message.new("Months", monthsn ):inLanguage(lang):plain()
		local daysn = mw.ustring.sub(duration, dayfirst, dayend - 4) - monthsn*31
		local days = mw.message.new("Days", daysn ):inLanguage(lang):plain()
		if monthsn ~= 0 and daysn ~= 0 then
			timeago = mw.ustring.gsub(duration, "%d+ days", frame:preprocess(months) .. frame:preprocess(days))
		elseif days == 0 then
			timeago = mw.ustring.gsub(duration, "%d+ days", frame:preprocess(months) )
		else
			timeago = duration
		end
	else
		timeago = duration
	end
	return mw.message.new( 'Ago', timeago ):inLanguage(lang):plain()
end

return p