001/** 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v1.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.core.util; 015 016import static org.junit.Assert.assertEquals; 017 018import java.util.Calendar; 019import java.util.Date; 020import java.util.TimeZone; 021 022import org.junit.Test; 023 024public class TimeUtilTest { 025 026 @Test 027 public void testSecond() { 028 // Mon Nov 20 18:05:17,522 CET 2006 029 long now = 1164042317522L; 030 // Mon Nov 20 18:05:18,000 CET 2006 031 long expected = 1164042318000L; 032 long computed = TimeUtil.computeStartOfNextSecond(now); 033 assertEquals(expected - now, 478); 034 assertEquals(expected, computed); 035 } 036 037 @Test 038 public void testMinute() { 039 // Mon Nov 20 18:05:17,522 CET 2006 040 long now = 1164042317522L; 041 // Mon Nov 20 18:06:00 CET 2006 042 long expected = 1164042360000L; 043 044 long computed = TimeUtil.computeStartOfNextMinute(now); 045 assertEquals(expected - now, 1000 * 42 + 478); 046 assertEquals(expected, computed); 047 } 048 049 @Test 050 public void testHour() { 051 // Mon Nov 20 18:05:17,522 GMT 2006 052 long now = 1164045917522L; 053 now = correctBasedOnTimeZone(now); 054 // Mon Nov 20 19:00:00 GMT 2006 055 long expected = 1164049200000L; 056 expected = correctBasedOnTimeZone(expected); 057 058 long computed = TimeUtil.computeStartOfNextHour(now); 059 assertEquals(expected - now, 1000 * (42 + 60 * 54) + 478); 060 assertEquals(expected, computed); 061 } 062 063 @Test 064 public void testDay() { 065 // Mon Nov 20 18:05:17 GMT 2006 066 long now = 1164045917522L; 067 now = correctBasedOnTimeZone(now); 068 // Tue Nov 21 00:00:00 GMT 2006 069 long expected = 1164067200000L; 070 expected = correctBasedOnTimeZone(expected); 071 long computed = TimeUtil.computeStartOfNextDay(now); 072 073 assertEquals(expected - now, 1000 * (3600 * 5 + 60 * 54 + 42) + 478); 074 assertEquals(expected, computed); 075 } 076 077 @Test 078 public void testWeek() { 079 // Mon Nov 20 18:05:17 GMT 2006 080 long now = 1164045917522L; 081 now = correctBasedOnTimeZone(now); 082 // Sun Nov 26 00:00:00 GMT 2006 083 long expected = 1164499200000L; 084 expected = correctBasedOnTimeZone(expected); 085 086 Calendar cal = Calendar.getInstance(); 087 cal.setTime(new Date(now)); 088 089 int dayOffset = cal.getFirstDayOfWeek() - Calendar.SUNDAY; 090 if (dayOffset != 0) { 091 expected += 24L * 3600 * 1000 * (cal.getFirstDayOfWeek() - Calendar.SUNDAY); 092 } 093 094 long computed = TimeUtil.computeStartOfNextWeek(now); 095 // System.out.println("now "+new Date(now)); 096 // System.out.println("computed "+new Date(computed)); 097 // System.out.println("expected "+new Date(expected)); 098 assertEquals(expected - now, 1000 * (3600 * (5 + 24 * (5 + dayOffset)) + 60 * 54 + 42) + 478); 099 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}