View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import static org.junit.Assert.assertEquals;
17  
18  import java.util.Calendar;
19  import java.util.Date;
20  import java.util.TimeZone;
21  
22  import org.junit.Test;
23  
24  public class TimeUtilTest {
25  
26      @Test
27      public void testSecond() {
28          // Mon Nov 20 18:05:17,522 CET 2006
29          long now = 1164042317522L;
30          // Mon Nov 20 18:05:18,000 CET 2006
31          long expected = 1164042318000L;
32          long computed = TimeUtil.computeStartOfNextSecond(now);
33          assertEquals(expected - now, 478);
34          assertEquals(expected, computed);
35      }
36  
37      @Test
38      public void testMinute() {
39          // Mon Nov 20 18:05:17,522 CET 2006
40          long now = 1164042317522L;
41          // Mon Nov 20 18:06:00 CET 2006
42          long expected = 1164042360000L;
43  
44          long computed = TimeUtil.computeStartOfNextMinute(now);
45          assertEquals(expected - now, 1000 * 42 + 478);
46          assertEquals(expected, computed);
47      }
48  
49      @Test
50      public void testHour() {
51          // Mon Nov 20 18:05:17,522 GMT 2006
52          long now = 1164045917522L;
53          now = correctBasedOnTimeZone(now);
54          // Mon Nov 20 19:00:00 GMT 2006
55          long expected = 1164049200000L;
56          expected = correctBasedOnTimeZone(expected);
57  
58          long computed = TimeUtil.computeStartOfNextHour(now);
59          assertEquals(expected - now, 1000 * (42 + 60 * 54) + 478);
60          assertEquals(expected, computed);
61      }
62  
63      @Test
64      public void testDay() {
65          // Mon Nov 20 18:05:17 GMT 2006
66          long now = 1164045917522L;
67          now = correctBasedOnTimeZone(now);
68          // Tue Nov 21 00:00:00 GMT 2006
69          long expected = 1164067200000L;
70          expected = correctBasedOnTimeZone(expected);
71          long computed = TimeUtil.computeStartOfNextDay(now);
72  
73          assertEquals(expected - now, 1000 * (3600 * 5 + 60 * 54 + 42) + 478);
74          assertEquals(expected, computed);
75      }
76  
77      @Test
78      public void testWeek() {
79          // Mon Nov 20 18:05:17 GMT 2006
80          long now = 1164045917522L;
81          now = correctBasedOnTimeZone(now);
82          // Sun Nov 26 00:00:00 GMT 2006
83          long expected = 1164499200000L;
84          expected = correctBasedOnTimeZone(expected);
85  
86          Calendar cal = Calendar.getInstance();
87          cal.setTime(new Date(now));
88  
89          int dayOffset = cal.getFirstDayOfWeek() - Calendar.SUNDAY;
90          if (dayOffset != 0) {
91              expected += 24L * 3600 * 1000 * (cal.getFirstDayOfWeek() - Calendar.SUNDAY);
92          }
93  
94          long computed = TimeUtil.computeStartOfNextWeek(now);
95          // System.out.println("now "+new Date(now));
96          // System.out.println("computed "+new Date(computed));
97          // System.out.println("expected "+new Date(expected));
98          assertEquals(expected - now, 1000 * (3600 * (5 + 24 * (5 + dayOffset)) + 60 * 54 + 42) + 478);
99          assertEquals(expected, computed);
100     }
101 
102     @Test
103     public void testMonth() {
104         // Mon Nov 20 18:05:17 GMT 2006
105         long now = 1164045917522L;
106         now = correctBasedOnTimeZone(now);
107         // Fri Dec 01 00:00:00 GMT 2006
108         long expected = 1164931200000L;
109         expected = correctBasedOnTimeZone(expected);
110 
111         long computed = TimeUtil.computeStartOfNextMonth(now);
112         assertEquals(expected - now, 1000 * (3600 * (5 + 24 * 10) + 60 * 54 + 42) + 478);
113         assertEquals(expected, computed);
114     }
115 
116     private long correctBasedOnTimeZone(long gmtLong) {
117         int offset = TimeZone.getDefault().getRawOffset();
118         return gmtLong - offset;
119     }
120 
121 }