Home
Random
Log in
Settings
About the Petit Planet Wiki
Search
Editing
Module
:
Time Ago
Warning:
You are not logged in. Once you make an edit, a temporary account will be created for you.
Learn more
.
Log in
or
create an account
to continue receiving notifications after this account expires, and to access other features.
Anti-spam check. Do
not
fill this in!
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
Summary:
Please note that all contributions to The Petit Planet Wiki are considered to be released under the Creative Commons Attribution-ShareAlike (see
Petit Planet Wiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Preview page with this template
Below are commonly used wikitext from
MediaWiki:Edittools
. Click on any to insert them in the edit box.
File Pages
==Summary==
==Licensing==
{{Fairuse}}
{{File|}}
Dynamic Page List
¦
²
«
»
²{¦}²
«»
«/»
«!----»
Array in DPL
¹
^2{
}^2
^2{¹}^2
Parser Functions
{{#if:||}}
{{#if:<condition>|<result if true>|<result if false>}}
{{#ifeq:|||}}
{{#ifeq:<text1>|<text2>|<result if text1=text2>|<result if text1≠text2>}}
{{#expr:}}
{{#expr:<mathematical expression>}}
{{#switch:||#default=}}
{{#switch:<text1>|<text2>=<result if text1=text2>|<text3>=<result if text1=text3>|#default=<result if no valid match>}}
{{#replace:||}}
{{#replace:<text1>|<plain text to find in text1>|<plain text to insert in place of the text found>}}
{{#titleparts:}}
{{#replace:<page name>}}
{{#tag:|}}
{{#tag:<name of html tag>|<content inside html tag>}}
Markup
{{}}
|
[]
[[]]
[[Category:]]
#REDIRECT [[]] [[Category:Redirect Pages]]
<code></code>
<includeonly></includeonly>
<noinclude></noinclude>
<nowiki></nowiki>
<!---->
<br>
§
Notices
{{Stub|}}
{{Stub Dialogue}}
{{Upcoming}}
{{Under Construction}}
{{Placeholder|}}
Magic Words
{{!}}
{{formatnum:}}
{{lc:}}
{{uc:}}
{{PAGENAME}}
{{FULLPAGENAME}}
{{ROOTPAGENAME}}
{{BASEPAGENAME}}
{{SUBPAGENAME}}
{{DISPLAYTITLE:|noreplace}}
__TOC__
__NOTOC__
__EXPECTUNUSEDCATEGORY__
__HIDDENCAT__
HTML Entities
—
,
­
Page included on this page:
Module:Time Ago/doc
(
view source
)