changeset 119:e6592d8305cb

mod_muc_log_http: support next, previous day calculations from one month/year to the next or visa versa
author Thilo Cestonaro <thilo@cestona.ro>
date Fri, 15 Jan 2010 09:58:21 +0100
parents 7eadcf0a0190
children 7a2d33e8ad1f
files mod_muc_log_http/muc_log_http/mod_muc_log_http.lua
diffstat 1 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_log_http/muc_log_http/mod_muc_log_http.lua	Fri Jan 15 09:12:55 2010 +0100
+++ b/mod_muc_log_http/muc_log_http/mod_muc_log_http.lua	Fri Jan 15 09:58:21 2010 +0100
@@ -465,8 +465,11 @@
 	then
 		if month + 1 > 12 then
 			year = year + 1;
+			month = 1;
+			day = 1;
 		else
 			month = month + 1;
+			day = 1;
 		end
 	else
 		day = day + 1;
@@ -496,16 +499,35 @@
 
 local function decrementDay(bare_day)
 	local year, month, day = bare_day:match("^(%d%d)(%d%d)(%d%d)");
+	local leapyear = false;
 	module:log("debug", tostring(day).."/"..tostring(month).."/"..tostring(year))
+
 	day = tonumber(day);
 	month = tonumber(month);
 	year = tonumber(year);
-	
+
+	if year%4 == 0 and year%100 == 0 then
+		if year%400 == 0 then
+			leapyear = true;
+		else
+			leapyear = false; -- turn of the century but not a leapyear
+		end
+	elseif year%4 == 0 then
+		leapyear = true;
+	end	
+		
 	if day - 1 == 0 then
 		if month - 1 == 0 then
 			year = year - 1;
+			month = 12;
+			day = 31;
 		else
 			month = month - 1;
+			if (month == 2 and leapyear) then day = 29 
+			elseif (month == 2 and not leapyear) then day = 28
+			elseif (month < 8 and month%2 == 1) or (month >= 8 and month%2 == 0) then day = 31
+			else day = 30
+			end
 		end
 	else
 		day = day - 1;