Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatProlepticGregorianCalendar

Z Wikipedii, wolnej encyklopedii

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Brudnopis/Paweł Ziemian/Wikidane/formatProlepticGregorianCalendar/opis

local moduleData = mw.loadData("Module:Wikidane/data")
 
return {
 
scope = "snak",
 
format = function(snak, options)
	local value = snak.datavalue.value
	local timeFormats = (options.linkDate and moduleData.linkTimeFormats or moduleData.plainTimeFormats)[value.precision]
	if not timeFormats then
		mw.log(string.format(moduleData.warnNotSupportedPrecision, value.precision))
		return nil
	end
 
	if (value.before ~= 0) or (value.after ~= 0) then
		mw.log(string.format(moduleData.warnNotSupportedTimeArgs, value.before, value.after))
		-- I do not understand this values yet
		return nil
	end
 
	local sign_year, month, day, hour, minute, second = string.match(value.time, "^([+-]%d%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)Z$", 1)
	if not sign_year or not month or not day or not hour or not minute or not second then
		mw.log(string.format(moduleData.warnUnrecognizedTime, value.time))
		return nil
	end
 
	local year = tonumber(sign_year)
	local format = timeFormats[1]
	if year <= 0 then
		year = 1 - year
		format = timeFormats[2]
	end
 
	assert(format)
	assert(year >= 1)
	if year > 9999 then
		mw.log(string.format(moduleData.warnYearOutOfRange, sign_year))
		return nil
	end
 
	local timezone = value.timezone
	local offset = ""
	if timezone < -1 then
		offset = string.format(" %d minutes", timezone)
	elseif timezone == -1 then
		offset = " -1 minute"
	elseif timezone == 1 then
		offset = " +1 minute"
	elseif timezone > 1 then
		offset = string.format(" +%d minutes", timezone)
	end
 
 	if month == "00" then
 		month = "01"
 	end
 	if day == "00" then
 		day = "01"
 	end
 	
	local timestamp = string.format("%04d%s%s %s%s%s%s", year, month, day, hour, minute, second, offset)
	local result0 = mw.getContentLanguage():formatDate(format,timestamp)
	local result1, c1 = string.gsub(result0, "%f[0-9+%-]000([1-9])%f[^0-9]", "%1")
	local result2, c2 = string.gsub(result0, "%f[0-9+%-]00([1-9]%d)%f[^0-9]", "%1")
	local result3, c3 = string.gsub(result0, "%f[0-9+%-]0([1-9]%d%d)%f[^0-9]", "%1")
	mw.logObject(result0, "result0")
	mw.logObject({result1, c1}, "result1")
	mw.logObject({result2, c2}, "result2")
	mw.logObject({result3, c3}, "result3")
	if c1 > 0 then return result1 end
	if c2 > 0 then return result2 end
	if c3 > 0 then return result3 end
	return result0
end,
 
}