[logback-dev] svn commit: r971 - in logback/trunk/logback-core/src: main/java/ch/qos/logback/core/util test/java/ch/qos/logback/core/util
noreply.ceki at qos.ch
noreply.ceki at qos.ch
Mon Nov 20 23:11:25 CET 2006
Author: ceki
Date: Mon Nov 20 23:11:25 2006
New Revision: 971
Modified:
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/TimeUtil.java
logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/TimeUtilTest.java
Log:
TimeUtil can correctly compute the millis for the start of next day, next week or next month
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/TimeUtil.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/TimeUtil.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/util/TimeUtil.java Mon Nov 20 23:11:25 2006
@@ -17,27 +17,33 @@
return cal.getTime().getTime();
}
-// static long computeStartOfNextWeek(long now) {
-// Calendar cal = Calendar.getInstance();
-// cal.setTime(new Date(now));
-//
-// cal.add(Calendar.DAY_OF_MONTH, getFirstDayOfWeek());
-//
-//
-// cal.set(Calendar.HOUR_OF_DAY, 0);
-// cal.set(Calendar.MINUTE, 0);
-// cal.set(Calendar.SECOND, 0);
-// cal.set(Calendar.MILLISECOND, 0);
-//
-// return cal.getTime().getTime();
-// }
-
-// this.set(Calendar.DAY_OF_WEEK, getFirstDayOfWeek());
-// this.set(Calendar.HOUR_OF_DAY, 0);
-// this.set(Calendar.MINUTE, 0);
-// this.set(Calendar.SECOND, 0);
-// this.set(Calendar.MILLISECOND, 0);
-// this.add(Calendar.WEEK_OF_YEAR, 1);
+ static long computeStartOfNextWeek(long now) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(new Date(now));
+
+ cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
+ cal.set(Calendar.HOUR_OF_DAY, 0);
+ cal.set(Calendar.MINUTE, 0);
+ cal.set(Calendar.SECOND, 0);
+ cal.set(Calendar.MILLISECOND, 0);
+ cal.add(Calendar.WEEK_OF_YEAR, 1);
+
+ return cal.getTime().getTime();
+ }
+
+ static long computeStartOfNextMonth(long now) {
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(new Date(now));
+ cal.set(Calendar.DATE, 1);
+ cal.set(Calendar.HOUR_OF_DAY, 0);
+ cal.set(Calendar.MINUTE, 0);
+ cal.set(Calendar.SECOND, 0);
+ cal.set(Calendar.MILLISECOND, 0);
+ cal.add(Calendar.MONTH, 1);
+ return cal.getTime().getTime();
+ }
+
+
}
Modified: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/TimeUtilTest.java
==============================================================================
--- logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/TimeUtilTest.java (original)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/util/TimeUtilTest.java Mon Nov 20 23:11:25 2006
@@ -1,5 +1,7 @@
package ch.qos.logback.core.util;
+import java.util.Date;
+
import junit.framework.TestCase;
public class TimeUtilTest extends TestCase {
@@ -26,4 +28,29 @@
assertEquals(expected - now, 1000*(3600*5+60*54+42)+478);
assertEquals(expected, computed);
}
+
+ public void testWeek() {
+ // Mon Nov 20 18:05:17 CET 2006
+ long now = 1164042317522L;
+ // Sun Nov 26 00:00:00 CET 2006
+ long expected = 1164495600000L;
+ long computed = TimeUtil.computeStartOfNextWeek(now);
+ assertEquals(expected - now, 1000*(3600*(5+24*5)+60*54+42)+478);
+ assertEquals(expected, computed);
+ }
+
+ public void testMonth() {
+ // Mon Nov 20 18:05:17 CET 2006
+ long now = 1164042317522L;
+ // Fri Dec 01 00:00:00 CET 2006
+ long expected = 1164927600000L;
+ long computed = TimeUtil.computeStartOfNextMonth(now);
+
+ System.out.println(computed);
+ System.out.println(new Date(computed));
+ assertEquals(expected - now, 1000*(3600*(5+24*10)+60*54+42)+478);
+ assertEquals(expected, computed);
+ }
+
+
}
More information about the logback-dev
mailing list