Modul:DateTime

Aus OberpfalzWiki
Zur Navigation springen Zur Suche springen

Die Dokumentation für dieses Modul kann unter Modul:DateTime/Doku erstellt werden

DateTime = {}

months = {}
months["de"] = {}
months["de"]["01"] = "Januar"
months["de"]["02"] = "Februar"
months["de"]["03"] = "März"
months["de"]["04"] = "April"
months["de"]["05"] = "Mai"
months["de"]["06"] = "Juni"
months["de"]["07"] = "Juli"
months["de"]["08"] = "August"
months["de"]["09"] = "September"
months["de"]["10"] = "Oktober"
months["de"]["11"] = "November"
months["de"]["12"] = "Dezember"

DateTime.getDate = function(frame)
	return DateTime._getDate(frame.args[1], frame.args[2], frame.args[3])
end

DateTime._getDate = function(year, month, day)
	local yearf = year
	local monthf = month or ""
	if(string.len(month) == 1) then
		monthf = "0"..month
	end
	local dayf = day or ""
	if(string.len(day) == 1) then
		dayf = "0"..day
	end
	local date = yearf
	if(monthf ~= "") then
		date = date.."-"..monthf
		if(dayf ~= "") then
			date = date.."-"..dayf
		end
	end
	return date
end

DateTime.addDays = function(frame)
	return DateTime._addDays(frame.args[1], frame.args[2])
end

DateTime._addDays = function(date, days)
	local ostime = os.time{year=tonumber(DateTime._getYear(date)), month=tonumber(DateTime._getMonth(date)), day=tonumber(DateTime._getDay(date)), hour=0}
	ostime = ostime + tonumber(days) * 86400
	return os.date("%Y-%m-%d", ostime)
end

DateTime.getMonthNrByName = function(frame)
	return DateTime._getMonthNrByName(frame.args[1])
end

DateTime._getMonthNrByName = function(name)
	for m = 1, 12 do
		if(m < 10) then
			mo = "0"..tostring(m)
		else
			mo = tostring(m)
		end
		if(months["de"][mo] == name) then
			return mo
		end
	end
	return ""
end

DateTime.parseDate = function(frame)
	return DateTime._parseDate(frame.args[1])
end

DateTime._parseDate = function(date)
	local day
	local month
	if string.match(date, "^%d%. ") then
		day = tonumber(string.sub(date, 1, 1))
		month = string.sub(date, 4, 15)
	end
	if string.match(date, "^%d%d%. ") then
		day = tonumber(string.sub(date, 1, 2))
		month = string.sub(date, 5, 15)
	end
	if(day < 10) then
		day = "0"..tostring(day)
	else
		day = tostring(day)
	end
	return DateTime._getMonthNrByName(month).."-"..day
end


DateTime.formatDate = function(frame)
	local date = tostring(frame.args[1])
	local linkdateparts = tonumber(frame.args[2]) or 0
	return DateTime._formatDate(date, linkdateparts)
end

DateTime.getYear = function(frame)
	return DateTime._getYear(frame.args[1])
end

DateTime.getMonth = function(frame)
	return DateTime._getMonth(frame.args[1])
end

DateTime.getDay = function(frame)
	return DateTime._getDay(frame.args[1])
end

DateTime._getYear = function(date)
	return string.sub(date, 1, 4)
end

DateTime._getMonth = function(date)
	return string.sub(date, 6, 7)
end

DateTime._getDay = function(date)
	return string.sub(date, 9, 10)
end

DateTime._formatDate = function(date, linkdateparts)
	if string.match(date, "^%d%d%d%d%-%d%d%-%d%d %d%d:%d%d") then
		local year = string.sub(date, 1, 4)
		local month = string.sub(date, 6, 7)
		local day = tonumber(string.sub(date, 9, 10))
		local hour = string.sub(date, 12, 13)
		local minute = string.sub(date, 15, 16)		
		return day..". "..months["de"][month].." "..year.." "..hour..":"..minute.." Uhr"
	end
	if string.match(date, "^%d%d%d%d%-%d%d%-%d%d") then
		local year = string.sub(date, 1, 4)
		local month = string.sub(date, 6, 7)
		local day = tonumber(string.sub(date, 9, 10))
		if(linkdateparts == 1) then
			return "[["..day..". "..months["de"][month].."]] [["..year.."]]"
		end
		return day..". "..months["de"][month].." "..year
	end
	if string.match(date, "^%d%d%d%d%-%d%d") then
		local year = string.sub(date, 1, 4)
		local month = string.sub(date, 6, 7)
		return months["de"][month].." "..year
	end
	if string.match(date, "^%d%d%-%d%d") then
		local month = string.sub(date, 1, 2)
		local day = tonumber(string.sub(date, 4, 5))		
		return day..". "..months["de"][month]
	end
	if string.match(date, "^%d%d%d%d") then
		return date
	end
	return date
end

DateTime.formatTimespan = function(frame)
	return DateTime._formatTimespan(frame.args[1], frame.args[2])
end

DateTime._formatTimespan = function(from, to)
	if not string.match(from, "^%d%d%d%d%-%d%d%-%d%d") then
		error("invalid input: from")
	end

	if not string.match(to, "^%d%d%d%d%-%d%d%-%d%d") then
		error("invalid input: to")
	end

	local yearfrom = string.sub(from, 1, 4)
	local monthfrom = string.sub(from, 6, 7)
	local dayfrom = string.sub(from, 9, 10)
	local yearto = string.sub(to, 1, 4)
	local monthto = string.sub(to, 6, 7)
	local dayto = string.sub(to, 9, 10)
	local result = dayfrom..". "
	if(yearfrom ~= yearto or monthfrom ~= monthto) then
		result = result..months["de"][monthfrom].." "
	end
	if(yearfrom ~= yearto) then
		result = result..yearfrom
	end
	result = result.." - "
	result = result..DateTime._formatDate(to)
	return result

end

return DateTime