1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * Copyright (c) 2012, Stephen Colebourne & Michael Nascimento Santos
28  *
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions are met:
33  *
34  *  * Redistributions of source code must retain the above copyright notice,
35  *    this list of conditions and the following disclaimer.
36  *
37  *  * Redistributions in binary form must reproduce the above copyright notice,
38  *    this list of conditions and the following disclaimer in the documentation
39  *    and/or other materials provided with the distribution.
40  *
41  *  * Neither the name of JSR-310 nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 package test.java.time.temporal;
58 
59 import static java.time.Month.AUGUST;
60 import static java.time.Month.FEBRUARY;
61 import static java.time.Month.JULY;
62 import static java.time.Month.JUNE;
63 import static java.time.Month.MARCH;
64 import static java.time.Month.OCTOBER;
65 import static java.time.Month.SEPTEMBER;
66 import static java.time.temporal.ChronoUnit.DAYS;
67 import static java.time.temporal.ChronoUnit.MONTHS;
68 import static java.time.temporal.ChronoUnit.WEEKS;
69 import static java.time.temporal.ChronoUnit.YEARS;
70 import static org.testng.Assert.assertEquals;
71 
72 import java.time.LocalDate;
73 import java.time.Month;
74 import java.time.ZoneOffset;
75 
76 import org.testng.annotations.DataProvider;
77 import org.testng.annotations.Test;
78 
79 /**
80  * Test.
81  */
82 @Test
83 public class TestChronoUnit {
84 
85     //-----------------------------------------------------------------------
86     @DataProvider(name = "yearsBetween")
data_yearsBetween()87     Object[][] data_yearsBetween() {
88         return new Object[][] {
89             {date(1939, SEPTEMBER, 2), date(1939, SEPTEMBER, 1), 0},
90             {date(1939, SEPTEMBER, 2), date(1939, SEPTEMBER, 2), 0},
91             {date(1939, SEPTEMBER, 2), date(1939, SEPTEMBER, 3), 0},
92 
93             {date(1939, SEPTEMBER, 2), date(1940, SEPTEMBER, 1), 0},
94             {date(1939, SEPTEMBER, 2), date(1940, SEPTEMBER, 2), 1},
95             {date(1939, SEPTEMBER, 2), date(1940, SEPTEMBER, 3), 1},
96 
97             {date(1939, SEPTEMBER, 2), date(1938, SEPTEMBER, 1), -1},
98             {date(1939, SEPTEMBER, 2), date(1938, SEPTEMBER, 2), -1},
99             {date(1939, SEPTEMBER, 2), date(1938, SEPTEMBER, 3), 0},
100 
101             {date(1939, SEPTEMBER, 2), date(1945, SEPTEMBER, 3), 6},
102             {date(1939, SEPTEMBER, 2), date(1945, OCTOBER, 3), 6},
103             {date(1939, SEPTEMBER, 2), date(1945, AUGUST, 3), 5},
104         };
105     }
106 
107     @Test(dataProvider = "yearsBetween")
test_yearsBetween(LocalDate start, LocalDate end, long expected)108     public void test_yearsBetween(LocalDate start, LocalDate end, long expected) {
109         assertEquals(YEARS.between(start, end), expected);
110     }
111 
112     @Test(dataProvider = "yearsBetween")
test_yearsBetweenReversed(LocalDate start, LocalDate end, long expected)113     public void test_yearsBetweenReversed(LocalDate start, LocalDate end, long expected) {
114         assertEquals(YEARS.between(end, start), -expected);
115     }
116 
117     @Test(dataProvider = "yearsBetween")
test_yearsBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected)118     public void test_yearsBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected) {
119         assertEquals(YEARS.between(start.atTime(12, 30), end.atTime(12, 30)), expected);
120     }
121 
122     @Test(dataProvider = "yearsBetween")
test_yearsBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected)123     public void test_yearsBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected) {
124         if (expected >= 0) {
125             assertEquals(YEARS.between(start.atTime(12, 30), end.atTime(12, 31)), expected);
126         } else {
127             assertEquals(YEARS.between(start.atTime(12, 30), end.atTime(12, 29)), expected);
128         }
129     }
130 
131     @Test(dataProvider = "yearsBetween")
test_yearsBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected)132     public void test_yearsBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected) {
133         assertEquals(YEARS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(2))), expected);
134     }
135 
136     @Test(dataProvider = "yearsBetween")
test_yearsBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected)137     public void test_yearsBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected) {
138         // +01:00 is later than +02:00
139         if (expected >= 0) {
140             assertEquals(YEARS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(1))), expected);
141         } else {
142             assertEquals(YEARS.between(start.atStartOfDay(ZoneOffset.ofHours(1)), end.atStartOfDay(ZoneOffset.ofHours(2))), expected);
143         }
144     }
145 
146     //-----------------------------------------------------------------------
147     @DataProvider(name = "monthsBetween")
data_monthsBetween()148     Object[][] data_monthsBetween() {
149         return new Object[][] {
150             {date(2012, JULY, 2), date(2012, JULY, 1), 0},
151             {date(2012, JULY, 2), date(2012, JULY, 2), 0},
152             {date(2012, JULY, 2), date(2012, JULY, 3), 0},
153 
154             {date(2012, JULY, 2), date(2012, AUGUST, 1), 0},
155             {date(2012, JULY, 2), date(2012, AUGUST, 2), 1},
156             {date(2012, JULY, 2), date(2012, AUGUST, 3), 1},
157 
158             {date(2012, JULY, 2), date(2012, SEPTEMBER, 1), 1},
159             {date(2012, JULY, 2), date(2012, SEPTEMBER, 2), 2},
160             {date(2012, JULY, 2), date(2012, SEPTEMBER, 3), 2},
161 
162             {date(2012, JULY, 2), date(2012, JUNE, 1), -1},
163             {date(2012, JULY, 2), date(2012, JUNE, 2), -1},
164             {date(2012, JULY, 2), date(2012, JUNE, 3), 0},
165 
166             {date(2012, FEBRUARY, 27), date(2012, MARCH, 26), 0},
167             {date(2012, FEBRUARY, 27), date(2012, MARCH, 27), 1},
168             {date(2012, FEBRUARY, 27), date(2012, MARCH, 28), 1},
169 
170             {date(2012, FEBRUARY, 28), date(2012, MARCH, 27), 0},
171             {date(2012, FEBRUARY, 28), date(2012, MARCH, 28), 1},
172             {date(2012, FEBRUARY, 28), date(2012, MARCH, 29), 1},
173 
174             {date(2012, FEBRUARY, 29), date(2012, MARCH, 28), 0},
175             {date(2012, FEBRUARY, 29), date(2012, MARCH, 29), 1},
176             {date(2012, FEBRUARY, 29), date(2012, MARCH, 30), 1},
177         };
178     }
179 
180     @Test(dataProvider = "monthsBetween")
test_monthsBetween(LocalDate start, LocalDate end, long expected)181     public void test_monthsBetween(LocalDate start, LocalDate end, long expected) {
182         assertEquals(MONTHS.between(start, end), expected);
183     }
184 
185     @Test(dataProvider = "monthsBetween")
test_monthsBetweenReversed(LocalDate start, LocalDate end, long expected)186     public void test_monthsBetweenReversed(LocalDate start, LocalDate end, long expected) {
187         assertEquals(MONTHS.between(end, start), -expected);
188     }
189 
190     @Test(dataProvider = "monthsBetween")
test_monthsBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected)191     public void test_monthsBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected) {
192         assertEquals(MONTHS.between(start.atTime(12, 30), end.atTime(12, 30)), expected);
193     }
194 
195     @Test(dataProvider = "monthsBetween")
test_monthsBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected)196     public void test_monthsBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected) {
197         if (expected >= 0) {
198             assertEquals(MONTHS.between(start.atTime(12, 30), end.atTime(12, 31)), expected);
199         } else {
200             assertEquals(MONTHS.between(start.atTime(12, 30), end.atTime(12, 29)), expected);
201         }
202     }
203 
204     @Test(dataProvider = "monthsBetween")
test_monthsBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected)205     public void test_monthsBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected) {
206         assertEquals(MONTHS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(2))), expected);
207     }
208 
209     @Test(dataProvider = "monthsBetween")
test_monthsBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected)210     public void test_monthsBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected) {
211         // +01:00 is later than +02:00
212         if (expected >= 0) {
213             assertEquals(MONTHS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(1))), expected);
214         } else {
215             assertEquals(MONTHS.between(start.atStartOfDay(ZoneOffset.ofHours(1)), end.atStartOfDay(ZoneOffset.ofHours(2))), expected);
216         }
217     }
218 
219     //-----------------------------------------------------------------------
220     @DataProvider(name = "weeksBetween")
data_weeksBetween()221     Object[][] data_weeksBetween() {
222         return new Object[][] {
223             {date(2012, JULY, 2), date(2012, JUNE, 25), -1},
224             {date(2012, JULY, 2), date(2012, JUNE, 26), 0},
225             {date(2012, JULY, 2), date(2012, JULY, 2), 0},
226             {date(2012, JULY, 2), date(2012, JULY, 8), 0},
227             {date(2012, JULY, 2), date(2012, JULY, 9), 1},
228 
229             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 21), -1},
230             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 22), 0},
231             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 28), 0},
232             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 29), 0},
233             {date(2012, FEBRUARY, 28), date(2012, MARCH, 1), 0},
234             {date(2012, FEBRUARY, 28), date(2012, MARCH, 5), 0},
235             {date(2012, FEBRUARY, 28), date(2012, MARCH, 6), 1},
236 
237             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 22), -1},
238             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 23), 0},
239             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 28), 0},
240             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 29), 0},
241             {date(2012, FEBRUARY, 29), date(2012, MARCH, 1), 0},
242             {date(2012, FEBRUARY, 29), date(2012, MARCH, 6), 0},
243             {date(2012, FEBRUARY, 29), date(2012, MARCH, 7), 1},
244         };
245     }
246 
247     @Test(dataProvider = "weeksBetween")
test_weeksBetween(LocalDate start, LocalDate end, long expected)248     public void test_weeksBetween(LocalDate start, LocalDate end, long expected) {
249         assertEquals(WEEKS.between(start, end), expected);
250     }
251 
252     @Test(dataProvider = "weeksBetween")
test_weeksBetweenReversed(LocalDate start, LocalDate end, long expected)253     public void test_weeksBetweenReversed(LocalDate start, LocalDate end, long expected) {
254         assertEquals(WEEKS.between(end, start), -expected);
255     }
256 
257     //-----------------------------------------------------------------------
258     @DataProvider(name = "daysBetween")
data_daysBetween()259     Object[][] data_daysBetween() {
260         return new Object[][] {
261             {date(2012, JULY, 2), date(2012, JULY, 1), -1},
262             {date(2012, JULY, 2), date(2012, JULY, 2), 0},
263             {date(2012, JULY, 2), date(2012, JULY, 3), 1},
264 
265             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 27), -1},
266             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 28), 0},
267             {date(2012, FEBRUARY, 28), date(2012, FEBRUARY, 29), 1},
268             {date(2012, FEBRUARY, 28), date(2012, MARCH, 1), 2},
269 
270             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 27), -2},
271             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 28), -1},
272             {date(2012, FEBRUARY, 29), date(2012, FEBRUARY, 29), 0},
273             {date(2012, FEBRUARY, 29), date(2012, MARCH, 1), 1},
274 
275             {date(2012, MARCH, 1), date(2012, FEBRUARY, 27), -3},
276             {date(2012, MARCH, 1), date(2012, FEBRUARY, 28), -2},
277             {date(2012, MARCH, 1), date(2012, FEBRUARY, 29), -1},
278             {date(2012, MARCH, 1), date(2012, MARCH, 1), 0},
279             {date(2012, MARCH, 1), date(2012, MARCH, 2), 1},
280 
281             {date(2012, MARCH, 1), date(2013, FEBRUARY, 28), 364},
282             {date(2012, MARCH, 1), date(2013, MARCH, 1), 365},
283 
284             {date(2011, MARCH, 1), date(2012, FEBRUARY, 28), 364},
285             {date(2011, MARCH, 1), date(2012, FEBRUARY, 29), 365},
286             {date(2011, MARCH, 1), date(2012, MARCH, 1), 366},
287         };
288     }
289 
290     @Test(dataProvider = "daysBetween")
test_daysBetween(LocalDate start, LocalDate end, long expected)291     public void test_daysBetween(LocalDate start, LocalDate end, long expected) {
292         assertEquals(DAYS.between(start, end), expected);
293     }
294 
295     @Test(dataProvider = "daysBetween")
test_daysBetweenReversed(LocalDate start, LocalDate end, long expected)296     public void test_daysBetweenReversed(LocalDate start, LocalDate end, long expected) {
297         assertEquals(DAYS.between(end, start), -expected);
298     }
299 
300     @Test(dataProvider = "daysBetween")
test_daysBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected)301     public void test_daysBetween_LocalDateTimeSameTime(LocalDate start, LocalDate end, long expected) {
302         assertEquals(DAYS.between(start.atTime(12, 30), end.atTime(12, 30)), expected);
303     }
304 
305     @Test(dataProvider = "daysBetween")
test_daysBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected)306     public void test_daysBetween_LocalDateTimeLaterTime(LocalDate start, LocalDate end, long expected) {
307         if (expected >= 0) {
308             assertEquals(DAYS.between(start.atTime(12, 30), end.atTime(12, 31)), expected);
309         } else {
310             assertEquals(DAYS.between(start.atTime(12, 30), end.atTime(12, 29)), expected);
311         }
312     }
313 
314     @Test(dataProvider = "daysBetween")
test_daysBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected)315     public void test_daysBetween_ZonedDateSameOffset(LocalDate start, LocalDate end, long expected) {
316         assertEquals(DAYS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(2))), expected);
317     }
318 
319     @Test(dataProvider = "daysBetween")
test_daysBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected)320     public void test_daysBetween_ZonedDateLaterOffset(LocalDate start, LocalDate end, long expected) {
321         // +01:00 is later than +02:00
322         if (expected >= 0) {
323             assertEquals(DAYS.between(start.atStartOfDay(ZoneOffset.ofHours(2)), end.atStartOfDay(ZoneOffset.ofHours(1))), expected);
324         } else {
325             assertEquals(DAYS.between(start.atStartOfDay(ZoneOffset.ofHours(1)), end.atStartOfDay(ZoneOffset.ofHours(2))), expected);
326         }
327     }
328 
329     //-----------------------------------------------------------------------
date(int year, Month month, int dom)330     private static LocalDate date(int year, Month month, int dom) {
331         return LocalDate.of(year, month, dom);
332     }
333 
334 }
335