Moduł:Dino

Z Wikipedii, wolnej encyklopedii
 Dokumentacja modułu [zobacz] [edytuj] [historia] [odśwież]

Użycie[edytuj kod]

Dostępne są 3 funkcje eksportowe:

  • dateToAge – Data na wiek (zaokrąglone w dół).
  • dateToImage – Data na obrazek 😁. Specyficzne dla Szablon:User dino.
  • dateToCategory – Data na kategorię (0-3). Specyficzne dla Szablon:User dino.

Zawiera również funkcję do parsowania daty ISO, ale na ten moment nie jest eksportowana. Pewnie mogłaby trafić do jakiegoś modułu DateTime.

Opis parametrów[edytuj kod]

Parametrem jest zawsze data. Vide: Szablon:User dino lub opis funkcji w module.

Przykład[edytuj kod]

Od ponad {{#invoke:Dino|dateToAge| 2008-12-31 }} lat.

Od ponad 15 lat.

Od ponad {{#invoke:Dino|dateToAge| {{#time: Y-m-d | 5 years 1 day ago }} }} lat.

Od ponad 5 lat.

Błędy[edytuj kod]

Błędy należy zgłaszać na stronie Wikipedia:Kawiarenka/Kwestie techniczne.

Zobacz też[edytuj kod]


local p = {}

--[[
	Data na wiek (zaokrąglone w dół).
	
	Formaty daty:
		Y-m-d -- data ISO (rok 4 cyfrowy)
		Y-m -- tu dopisze 1-go
		Y -- tu dopisze 1 stycznia
]] 
function p.dateToAge(frame)
	local strDate = trim(frame.args[1])
	local years = age(strDate)
	return math.floor(years)
end

--[[
	Data na obrazek 😁.
]] 
function p.dateToImage(frame)
	local strDate = trim(frame.args[1])
	local cat = dateToCategory(strDate)
	-- default 😉
	local img = "Blue Butterfly Barnstar 2.svg"
	if cat >= 3 then
		img = "Gold Barnstar of Dino.png"
	elseif cat >= 2 then
		img = "Silver Barnstar of Dino.png"
	elseif cat >= 1 then
		img = "Bronze Barnstar of Dino.png"
    end	
	return img
end
--[[
	Data na kategorię.
]] 
function p.dateToCategory(frame)
	local strDate = trim(frame.args[1])
	return dateToCategory(strDate)
end
--[[
	Data na kategorię.
	0 -- nie dino
	...
	3 -- złoto
]] 
function dateToCategory(strDate)
	local years = age(strDate)
	local cat = 0
	if years > 16 then
		cat = 3
	elseif years > 13 then
		cat = 2
	elseif years > 10 then
		cat = 1
    end	
	return cat
end

--[[
	Parse date given in any of supported forms.

	Note! For unrecognised format will return now.
	
	@param str ISO date. Formats:
		Y-m-d
		Y-m -- this will assume 1st
		Y -- this will assume 1st January
]]
function parseDate(str)
	local y, m, d = str:match("(%d%d%d%d)-?(%d?%d?)-?(%d?%d?)$")
	-- fallback to now
	if y == nil then
		return os.time()
	end
	-- defaults
	if m == '' then
		m = 1
	end
	if d == '' then
		d = 1
	end
	-- create time
	return os.time{year=y, month=m, day=d, hour=0}
end
--[[
--Tests:
print(  os.date( "%Y-%m-%d", parseDate("2019-12-28") )  )
print(  os.date( "%Y-%m-%d", parseDate("2019-12") )  )
print(  os.date( "%Y-%m-%d", parseDate("2019") )  )
]]

--[[
	Number of days from now.
	
	Returns a positive number if the date is in future.
	
	@param str ISO date.
]]
function daysFromNow(str)
	return os.difftime( parseDate(str), os.time() ) / 3600 / 24
end

--[[
	Age (in years).
	
	@param str ISO date.
]]
function age(str)
	local days = daysFromNow(str)
	-- future
	if days > 0 then
		return 0
	end
	local age = math.abs(days) / 360
	return age
end

--[[
--Tests:
print( age("2019-12-28") )
print( age("2018-12-28") )
print( age("2018-11-26") )
print( age("2018-11-25") )
]]

--[[
	trim string
	
	note:
	`(s:gsub(...))` returns only a string
	`s:gsub(...)` returns a string and a number
]]
function trim(s)
	return (s:gsub("^%s+", ""):gsub("%s+$", ""))
end

return p