Module:TimeAgo

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Edit] [Purge] Documentation

This module formats a date as an elapsed date from the current date. It was adapted from Commons:Module:TimeAgo, which has better support for a multilingual project than the English Wikipedia equivalent, w:Module:Time ago.

Usage

It is intended for use with Template:Time ago.

It is not ready for use as long as the module miscalculates the duration!

Localization

This module relies on the following interface messages for localization:

If your language is not yet supported, go to the talk page of one of these messages and add the {{Edit request}} template to request that an administrator add a translation in your language. Please specify the ISO 639 language code and the translation in wikitext format, including any required PLURAL: or GENDER: format.

See also

-- 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 = {}
local languages = require("Module:Languages")

function p.main(frame)
	local args = require( 'Module:Arguments' ).getArgs( frame, {
		valueFunc = function( k, v )
			if v then
				v = v:match( '^%s*(.-)%s*$' ) -- Trim whitespace.
				if k == 'ago' or v ~= '' then
					return v
				end
			end
			return nil
		end,
		wrappers = 'Template:Time ago'
	})
	
	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 = {}
	local time = args[1] or os.date()
	local lang = languages.currentPageLanguage(frame)
	if not mw.language.isKnownLanguageTag(lang) then
		lang = frame:preprocess("{{int:lang}}")
	end
	local seconds = os.difftime(os.time(), mw.getContentLanguage():formatDate("U", time))
	local msg = 'Ago'
	if seconds < 0 then
		msg = 'Osm-time-from-now'
		seconds = seconds * -1
	end
	if args.magnitude == "months" then 
		magnitude1 = "weeks"
	else
		magnitude1 = args.magnitude
	end
	if args.magnitude and intervals[magnitude1] then
		table.insert(magnitude, magnitude1)
		duration = mw.getLanguage(lang):getDurationIntervals(seconds, magnitude)
	elseif args["min_magnitude"] and intervals[args["min_magnitude"]] then
		local k = 6
		for i = 1, intervals[args["min_magnitude"]] do
			table.remove(acceptedintervals, k)
			k = k - 1
		end
		duration = mw.getLanguage(lang):getDurationIntervals(seconds, acceptedintervals)
	else
		duration = mw.getLanguage(lang):getDurationIntervals(seconds, acceptedintervals)
	end
	
	-- Derive months from weeks
	if duration.weeks then
		duration.months = math.floor(duration.weeks/4)
		duration.weeks = duration.weeks - duration.months*4
		if duration.months == 0 then
			duration.months = nil
		end
		if duration.weeks == 0 then
			duration.weeks = nil
		end
		if duration.months == 12 then
			duration.years = (duration.years or 0) + 1
			duration.months = nil
		end
	end
	
	local timeago = {}
	local lastResort
	for i, interval in ipairs(acceptedintervals) do
		local msg = mw.message.new(interval, duration[interval] or 0):inLanguage(lang):plain()
		if duration[interval] and duration[interval] > 0 then
			table.insert(timeago, frame:preprocess(msg))
		else
			lastResort = frame:preprocess(msg)
		end
	end
	
	if #timeago == 0 then
		table.insert(timeago, lastResort)
	end
	
	return mw.message.new( msg, mw.text.listToText(timeago) ):inLanguage(lang):plain()
end

return p