1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.orekit.time;
18
19
20 import org.junit.Assert;
21 import org.junit.Test;
22
23
24 public class DateComponentsTest {
25
26 @Test
27 public void testReferenceDates() {
28
29 int[][] reference = {
30 { -4713, 12, 31, -2451546 }, { -4712, 01, 01, -2451545 },
31 { 0000, 12, 31, -730122 }, { 0001, 01, 01, -730121 },
32 { 1500, 02, 28, -182554 }, { 1500, 02, 29, -182553 },
33 { 1500, 03, 01, -182552 }, { 1582, 10, 04, -152385 },
34 { 1582, 10, 15, -152384 }, { 1600, 02, 28, -146039 },
35 { 1600, 02, 29, -146038 }, { 1600, 03, 01, -146037 },
36 { 1700, 02, 28, -109514 }, { 1700, 03, 01, -109513 },
37 { 1800, 02, 28, -72990 }, { 1800, 03, 01, -72989 },
38 { 1858, 11, 15, -51546 }, { 1858, 11, 16, -51545 },
39 { 1999, 12, 31, -1 }, { 2000, 01, 01, 0 },
40 { 2000, 02, 28, 58 }, { 2000, 02, 29, 59 },
41 { 2000, 03, 01, 60 }
42 };
43
44 for (int i = 0; i < reference.length; ++i) {
45 int day = reference[i][3];
46 DateComponents date = new DateComponents(DateComponents.J2000_EPOCH, day);
47 Assert.assertEquals(reference[i][0], date.getYear());
48 Assert.assertEquals(reference[i][1], date.getMonth());
49 Assert.assertEquals(reference[i][2], date.getDay());
50 }
51
52 }
53
54 @Test
55 public void testCalendarWeek() {
56 Assert.assertEquals(52, new DateComponents(1995, 1, 1).getCalendarWeek());
57 Assert.assertEquals(52, new DateComponents(1996, 12, 29).getCalendarWeek());
58 Assert.assertEquals( 1, new DateComponents(1996, 12, 30).getCalendarWeek());
59 Assert.assertEquals( 1, new DateComponents(1997, 1, 5).getCalendarWeek());
60 Assert.assertEquals(52, new DateComponents(1997, 12, 28).getCalendarWeek());
61 Assert.assertEquals( 1, new DateComponents(1997, 12, 29).getCalendarWeek());
62 Assert.assertEquals( 1, new DateComponents(1998, 1, 4).getCalendarWeek());
63 Assert.assertEquals( 2, new DateComponents(1998, 1, 5).getCalendarWeek());
64 Assert.assertEquals(52, new DateComponents(1998, 12, 27).getCalendarWeek());
65 Assert.assertEquals(53, new DateComponents(1998, 12, 28).getCalendarWeek());
66 Assert.assertEquals(53, new DateComponents(1999, 1, 3).getCalendarWeek());
67 Assert.assertEquals( 1, new DateComponents(1999, 1, 4).getCalendarWeek());
68 DateComponents[] firstWeekMonday = new DateComponents[502];
69 for (int i = 0; i < firstWeekMonday.length; ++i) {
70 firstWeekMonday[i] = firstWeekMonday(1599 + i);
71 }
72 int startDay = firstWeekMonday[0].getJ2000Day();
73 int endDay = firstWeekMonday[firstWeekMonday.length - 1].getJ2000Day();
74 int index = 0;
75 for (int day = startDay; day < endDay; ++day) {
76 DateComponents d = new DateComponents(day);
77 if (firstWeekMonday[index + 1].compareTo(d) <= 0) {
78 ++index;
79 }
80 int delta = d.getJ2000Day() - firstWeekMonday[index].getJ2000Day();
81 Assert.assertEquals(1 + delta / 7, d.getCalendarWeek());
82 }
83 }
84
85 @Test
86 public void testWeekComponents() {
87 int[][] reference = {
88 { 1994, 52, 7, 1995, 1, 1 },
89 { 1996, 52, 7, 1996, 12, 29 },
90 { 1997, 1, 1, 1996, 12, 30 },
91 { 1997, 1, 7, 1997, 1, 5 },
92 { 1997, 52, 7, 1997, 12, 28 },
93 { 1998, 1, 1, 1997, 12, 29 },
94 { 1998, 1, 7, 1998, 1, 4 },
95 { 1998, 2, 1, 1998, 1, 5 },
96 { 1998, 52, 7, 1998, 12, 27 },
97 { 1998, 53, 1, 1998, 12, 28 },
98 { 1998, 53, 7, 1999, 1, 3 },
99 { 1999, 1, 1, 1999, 1, 4 },
100 { 1582, 40, 4, 1582, 10, 4 },
101 { 1582, 40, 5, 1582, 10, 15 },
102 { 1582, 51, 5, 1582, 12, 31 },
103 { 1582, 51, 6, 1583, 1, 1 },
104 { 1582, 51, 7, 1583, 1, 2 },
105 { 1583, 1, 1, 1583, 1, 3 }
106 };
107
108 for (int i = 0; i < reference.length; ++i) {
109 int[] refI = reference[i];
110 DateComponents date = DateComponents.createFromWeekComponents(refI[0], refI[1], refI[2]);
111 Assert.assertEquals(refI[3], date.getYear());
112 Assert.assertEquals(refI[4], date.getMonth());
113 Assert.assertEquals(refI[5], date.getDay());
114 }
115
116 }
117
118
119 private DateComponents firstWeekMonday(final int year) {
120 int i = 0;
121 while (true) {
122 DateComponents d = new DateComponents(year, 1, ++i);
123 if (d.getDayOfWeek() == 4) {
124
125 return new DateComponents(d, -3);
126 }
127 }
128 }
129
130 @Test
131 public void testDayOfWeek() {
132 Assert.assertEquals(7, new DateComponents(-4713, 12, 31).getDayOfWeek());
133 Assert.assertEquals(1, new DateComponents(-4712, 01, 01).getDayOfWeek());
134 Assert.assertEquals(4, new DateComponents( 1582, 10, 04).getDayOfWeek());
135 Assert.assertEquals(5, new DateComponents( 1582, 10, 15).getDayOfWeek());
136 Assert.assertEquals(5, new DateComponents( 1999, 12, 31).getDayOfWeek());
137 Assert.assertEquals(6, new DateComponents( 2000, 01, 01).getDayOfWeek());
138 }
139
140 @Test
141 public void testDayOfYear() {
142 Assert.assertEquals( 1, new DateComponents(2003, 1, 1).getDayOfYear());
143 Assert.assertEquals(365, new DateComponents(2003, 12, 31).getDayOfYear());
144 Assert.assertEquals(366, new DateComponents(2004, 12, 31).getDayOfYear());
145 Assert.assertEquals( 59, new DateComponents(2003, 2, 28).getDayOfYear());
146 Assert.assertEquals( 60, new DateComponents(2003, 3, 1).getDayOfYear());
147 Assert.assertEquals( 59, new DateComponents(2004, 2, 28).getDayOfYear());
148 Assert.assertEquals( 60, new DateComponents(2004, 2, 29).getDayOfYear());
149 Assert.assertEquals( 61, new DateComponents(2004, 3, 1).getDayOfYear());
150 Assert.assertEquals(269, new DateComponents(2003, 9, 26).getDayOfYear());
151 }
152
153 @Test
154 public void testParse() {
155
156 Assert.assertEquals(-2451546, DateComponents.parseDate("-47131231").getJ2000Day());
157 Assert.assertEquals(-2451546, DateComponents.parseDate("-4713-12-31").getJ2000Day());
158 Assert.assertEquals(-2451545, DateComponents.parseDate("-47120101").getJ2000Day());
159 Assert.assertEquals(-2451545, DateComponents.parseDate("-4712-01-01").getJ2000Day());
160 Assert.assertEquals( -730122, DateComponents.parseDate("00001231").getJ2000Day());
161 Assert.assertEquals( -730122, DateComponents.parseDate("0000-12-31").getJ2000Day());
162 Assert.assertEquals( -730121, DateComponents.parseDate("00010101").getJ2000Day());
163 Assert.assertEquals( -730121, DateComponents.parseDate("0001-01-01").getJ2000Day());
164 Assert.assertEquals( -182554, DateComponents.parseDate("15000228").getJ2000Day());
165 Assert.assertEquals( -182554, DateComponents.parseDate("1500-02-28").getJ2000Day());
166 Assert.assertEquals( -182553, DateComponents.parseDate("15000229").getJ2000Day());
167 Assert.assertEquals( -182553, DateComponents.parseDate("1500-02-29").getJ2000Day());
168 Assert.assertEquals( -182552, DateComponents.parseDate("15000301").getJ2000Day());
169 Assert.assertEquals( -182552, DateComponents.parseDate("1500-03-01").getJ2000Day());
170 Assert.assertEquals( -152385, DateComponents.parseDate("15821004").getJ2000Day());
171 Assert.assertEquals( -152385, DateComponents.parseDate("1582-10-04").getJ2000Day());
172 Assert.assertEquals( -152385, DateComponents.parseDate("1582W404").getJ2000Day());
173 Assert.assertEquals( -152385, DateComponents.parseDate("1582-W40-4").getJ2000Day());
174 Assert.assertEquals( -152384, DateComponents.parseDate("15821015").getJ2000Day());
175 Assert.assertEquals( -152384, DateComponents.parseDate("1582-10-15").getJ2000Day());
176 Assert.assertEquals( -152384, DateComponents.parseDate("1582W405").getJ2000Day());
177 Assert.assertEquals( -152384, DateComponents.parseDate("1582-W40-5").getJ2000Day());
178 Assert.assertEquals( -146039, DateComponents.parseDate("16000228").getJ2000Day());
179 Assert.assertEquals( -146039, DateComponents.parseDate("1600-02-28").getJ2000Day());
180 Assert.assertEquals( -146038, DateComponents.parseDate("16000229").getJ2000Day());
181 Assert.assertEquals( -146038, DateComponents.parseDate("1600-02-29").getJ2000Day());
182 Assert.assertEquals( -146037, DateComponents.parseDate("1600-03-01").getJ2000Day());
183 Assert.assertEquals( -109514, DateComponents.parseDate("17000228").getJ2000Day());
184 Assert.assertEquals( -109514, DateComponents.parseDate("1700-02-28").getJ2000Day());
185 Assert.assertEquals( -109513, DateComponents.parseDate("17000301").getJ2000Day());
186 Assert.assertEquals( -109513, DateComponents.parseDate("1700-03-01").getJ2000Day());
187 Assert.assertEquals( -72990, DateComponents.parseDate("18000228").getJ2000Day());
188 Assert.assertEquals( -72990, DateComponents.parseDate("1800-02-28").getJ2000Day());
189 Assert.assertEquals( -72989, DateComponents.parseDate("18000301").getJ2000Day());
190 Assert.assertEquals( -72989, DateComponents.parseDate("1800-03-01").getJ2000Day());
191 Assert.assertEquals( -51546, DateComponents.parseDate("18581115").getJ2000Day());
192 Assert.assertEquals( -51546, DateComponents.parseDate("1858-11-15").getJ2000Day());
193 Assert.assertEquals( -51545, DateComponents.parseDate("18581116").getJ2000Day());
194 Assert.assertEquals( -51545, DateComponents.parseDate("1858-11-16").getJ2000Day());
195 Assert.assertEquals( -1, DateComponents.parseDate("19991231").getJ2000Day());
196 Assert.assertEquals( -1, DateComponents.parseDate("1999-12-31").getJ2000Day());
197 Assert.assertEquals( 0, DateComponents.parseDate("20000101").getJ2000Day());
198 Assert.assertEquals( 0, DateComponents.parseDate("2000-01-01").getJ2000Day());
199 Assert.assertEquals( 0, DateComponents.parseDate("2000001").getJ2000Day());
200 Assert.assertEquals( 0, DateComponents.parseDate("2000-001").getJ2000Day());
201 Assert.assertEquals( 0, DateComponents.parseDate("1999-W52-6").getJ2000Day());
202 Assert.assertEquals( 0, DateComponents.parseDate("1999W526").getJ2000Day());
203 Assert.assertEquals( 58, DateComponents.parseDate("20000228").getJ2000Day());
204 Assert.assertEquals( 58, DateComponents.parseDate("2000-02-28").getJ2000Day());
205 Assert.assertEquals( 59, DateComponents.parseDate("20000229").getJ2000Day());
206 Assert.assertEquals( 59, DateComponents.parseDate("2000-02-29").getJ2000Day());
207 Assert.assertEquals( 60, DateComponents.parseDate("20000301").getJ2000Day());
208 Assert.assertEquals( 60, DateComponents.parseDate("2000-03-01").getJ2000Day());
209 }
210
211 @Test
212 public void testMonth() {
213 Assert.assertEquals(-51546, new DateComponents(1858, Month.NOVEMBER, 15).getJ2000Day());
214 Assert.assertEquals(-51546, new DateComponents(1858, Month.parseMonth("Nov"), 15).getJ2000Day());
215 Assert.assertEquals(Month.NOVEMBER, DateComponents.MODIFIED_JULIAN_EPOCH.getMonthEnum());
216 Assert.assertEquals(Month.JANUARY, DateComponents.J2000_EPOCH.getMonthEnum());
217 }
218
219 @Test
220 public void testISO8601Examples() {
221 Assert.assertEquals(-5377, DateComponents.parseDate("19850412").getJ2000Day());
222 Assert.assertEquals(-5377, DateComponents.parseDate("1985-04-12").getJ2000Day());
223 Assert.assertEquals(-5377, DateComponents.parseDate("1985102").getJ2000Day());
224 Assert.assertEquals(-5377, DateComponents.parseDate("1985-102").getJ2000Day());
225 Assert.assertEquals(-5377, DateComponents.parseDate("1985W155").getJ2000Day());
226 Assert.assertEquals(-5377, DateComponents.parseDate("1985-W15-5").getJ2000Day());
227 }
228
229 @SuppressWarnings("unlikely-arg-type")
230 @Test
231 public void testComparisons() {
232 DateComponents[][] dates = {
233 { new DateComponents(2003, 1, 1), new DateComponents(2003, 1) },
234 { new DateComponents(2003, 2, 28), new DateComponents(2003, 59) },
235 { new DateComponents(2003, 3, 1), new DateComponents(2003, 60) },
236 { new DateComponents(2003, 9, 26), new DateComponents(2003, 269) },
237 { new DateComponents(2003, 12, 31), new DateComponents(2003, 365) },
238 { new DateComponents(2004, 2, 28), new DateComponents(2004, 59) },
239 { new DateComponents(2004, 2, 29), new DateComponents(2004, 60) },
240 { new DateComponents(2004, 3, 1), new DateComponents(2004, 61) },
241 { new DateComponents(2004, 12, 31), new DateComponents(2004, 366) }
242 };
243 for (int i = 0; i < dates.length; ++i) {
244 for (int j = 0; j < dates.length; ++j) {
245 if (dates[i][0].compareTo(dates[j][1]) < 0) {
246 Assert.assertTrue(dates[j][1].compareTo(dates[i][0]) > 0);
247 Assert.assertFalse(dates[i][0].equals(dates[j][1]));
248 Assert.assertFalse(dates[j][1].equals(dates[i][0]));
249 Assert.assertTrue(dates[i][0].hashCode() != dates[j][1].hashCode());
250 Assert.assertTrue(i < j);
251 } else if (dates[i][0].compareTo(dates[j][1]) > 0) {
252 Assert.assertTrue(dates[j][1].compareTo(dates[i][0]) < 0);
253 Assert.assertFalse(dates[i][0].equals(dates[j][1]));
254 Assert.assertFalse(dates[j][1].equals(dates[i][0]));
255 Assert.assertTrue(dates[i][0].hashCode() != dates[j][1].hashCode());
256 Assert.assertTrue(i > j);
257 } else {
258 Assert.assertTrue(dates[j][1].compareTo(dates[i][0]) == 0);
259 Assert.assertTrue(dates[i][0].equals(dates[j][1]));
260 Assert.assertTrue(dates[j][1].equals(dates[i][0]));
261 Assert.assertTrue(dates[i][0].hashCode() == dates[j][1].hashCode());
262 Assert.assertTrue(i == j);
263 }
264 }
265 }
266 Assert.assertFalse(dates[0][0].equals(this));
267 }
268
269 @Test
270 public void testSymmetry() {
271 checkSymmetry(-2460000, 20000);
272 checkSymmetry( -740000, 20000);
273 checkSymmetry( -185000, 200000);
274 }
275
276 private void checkSymmetry(int start, int n) {
277 for (int i = start; i < start + n; ++i) {
278 DateComponents date1 = new DateComponents(DateComponents.J2000_EPOCH, i);
279 Assert.assertEquals(i, date1.getJ2000Day());
280 DateComponents date2 = new DateComponents(date1.getYear(), date1.getMonth(), date1.getDay());
281 Assert.assertEquals(i, date2.getJ2000Day());
282 }
283 }
284
285 @Test
286 public void testString() {
287 Assert.assertEquals("2000-01-01", new DateComponents(DateComponents.J2000_EPOCH, 0).toString());
288 Assert.assertEquals("-4713-12-31", new DateComponents(DateComponents.J2000_EPOCH, -2451546).toString());
289 }
290
291 @Test
292 public void testConstructorDoYYearBoundaries() {
293 Assert.assertNotNull(new DateComponents(2003, 1));
294 Assert.assertNotNull(new DateComponents(2003, 365));
295 Assert.assertNotNull(new DateComponents(2004, 1));
296 Assert.assertNotNull(new DateComponents(2004, 366));
297 }
298
299 @Test(expected=IllegalArgumentException.class)
300 public void testConstructorBadDayA() {
301 new DateComponents(2003, 0);
302 }
303
304 @Test(expected=IllegalArgumentException.class)
305 public void testConstructorBadDayB() {
306 new DateComponents(2003, 366);
307 }
308
309 @Test(expected=IllegalArgumentException.class)
310 public void testConstructorBadDayC() {
311 new DateComponents(2004, 0);
312 }
313
314 @Test(expected=IllegalArgumentException.class)
315 public void testConstructorBadDayE() {
316 new DateComponents(2004, 367);
317 }
318
319 @Test(expected=IllegalArgumentException.class)
320 public void testConstructorBadWeek() {
321 DateComponents.createFromWeekComponents(2008, 53, 1);
322 }
323
324 @Test(expected=IllegalArgumentException.class)
325 public void testConstructorBadDayOfWeek1() {
326 DateComponents.createFromWeekComponents(2008, 43, 0);
327 }
328
329 @Test(expected=IllegalArgumentException.class)
330 public void testConstructorBadDayOfWeek2() {
331 DateComponents.createFromWeekComponents(2008, 43, 8);
332 }
333
334 @Test(expected=IllegalArgumentException.class)
335 public void testConstructorBadString() {
336 DateComponents.parseDate("197-05-01");
337 }
338
339 public void testWellFormed() {
340 checkWellFormed(-4800, -4700, -2483687, -2446797);
341 checkWellFormed( -5, 5, -732313, -728296);
342 checkWellFormed( 1580, 1605, -153392, -143906);
343 checkWellFormed( 1695, 1705, -111398, -107382);
344 checkWellFormed( 1795, 1805, -74874, -70858);
345 checkWellFormed( 1895, 1905, -38350, -34334);
346 checkWellFormed( 1995, 2005, -1826, 2191);
347 }
348
349 public void checkWellFormed(int startYear, int endYear, int startJ2000, int endJ2000) {
350
351 int[] commonLength = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
352 int[] leapLength = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
353
354 int k = startJ2000;
355 for (int year = startYear; year <= endYear; ++year) {
356 for (int month = 0; month < 14; ++month) {
357 for (int day = 0; day < 33; ++day) {
358
359
360 boolean expectedIllFormed = false;
361 if ((month < 1) || (month > 12)) {
362 expectedIllFormed = true;
363 } else if ((year == 1582) && (month == 10) && (day > 4) && (day < 15)) {
364 expectedIllFormed = true;
365 } else if ((year < 1582) && (year % 4 == 0)) {
366 if ((day < 1) || (day > leapLength[month])) {
367 expectedIllFormed = true;
368 }
369 } else if ((year >= 1582) && (year % 4 == 0) &&
370 ((year % 100 != 0) || (year % 400 == 0))) {
371 if ((day < 1) || (day > leapLength[month])) {
372 expectedIllFormed = true;
373 }
374 } else {
375 if ((day < 1) || (day > commonLength[month])) {
376 expectedIllFormed = true;
377 }
378 }
379
380 try {
381
382 DateComponents date = new DateComponents(year, month, day);
383 Assert.assertEquals(k++, date.getJ2000Day());
384 Assert.assertTrue(!expectedIllFormed);
385 } catch (IllegalArgumentException iae) {
386 Assert.assertTrue(expectedIllFormed);
387 }
388 }
389 }
390 }
391
392 Assert.assertEquals(endJ2000, --k);
393
394 }
395
396 @Test
397 public void testMJD() {
398 Assert.assertEquals(0, DateComponents.MODIFIED_JULIAN_EPOCH.getMJD());
399 Assert.assertEquals(37665, new DateComponents(1962, 1, 1).getMJD());
400 Assert.assertEquals(54600, new DateComponents(2008, 5, 14).getMJD());
401 }
402
403 @Test
404 public void testMaxDate() {
405 Assert.assertEquals(5881610, DateComponents.MAX_EPOCH.getYear());
406 Assert.assertEquals( 7, DateComponents.MAX_EPOCH.getMonth());
407 Assert.assertEquals( 11, DateComponents.MAX_EPOCH.getDay());
408 Assert.assertEquals(Integer.MAX_VALUE, DateComponents.MAX_EPOCH.getJ2000Day());
409 Assert.assertEquals(5881610, new DateComponents(5881610, 7, 11).getYear());
410 Assert.assertEquals( 7, new DateComponents(5881610, 7, 11).getMonth());
411 Assert.assertEquals( 11, new DateComponents(5881610, 7, 11).getDay());
412 Assert.assertEquals(Integer.MAX_VALUE, new DateComponents(5881610, 7, 11).getJ2000Day());
413 Assert.assertEquals(5881610, new DateComponents(Integer.MAX_VALUE).getYear());
414 Assert.assertEquals( 7, new DateComponents(Integer.MAX_VALUE).getMonth());
415 Assert.assertEquals( 11, new DateComponents(Integer.MAX_VALUE).getDay());
416 Assert.assertEquals(Integer.MAX_VALUE, new DateComponents(Integer.MAX_VALUE).getJ2000Day());
417 }
418
419 @Test
420 public void testMinDate() {
421 Assert.assertEquals(-5877490, DateComponents.MIN_EPOCH.getYear());
422 Assert.assertEquals( 3, DateComponents.MIN_EPOCH.getMonth());
423 Assert.assertEquals( 3, DateComponents.MIN_EPOCH.getDay());
424 Assert.assertEquals(Integer.MIN_VALUE, DateComponents.MIN_EPOCH.getJ2000Day());
425 Assert.assertEquals(-5877490, new DateComponents(-5877490, 3, 3).getYear());
426 Assert.assertEquals( 3, new DateComponents(-5877490, 3, 3).getMonth());
427 Assert.assertEquals( 3, new DateComponents(-5877490, 3, 3).getDay());
428 Assert.assertEquals(Integer.MIN_VALUE, new DateComponents(-5877490, 3, 3).getJ2000Day());
429 Assert.assertEquals(-5877490, new DateComponents(Integer.MIN_VALUE).getYear());
430 Assert.assertEquals( 3, new DateComponents(Integer.MIN_VALUE).getMonth());
431 Assert.assertEquals( 3, new DateComponents(Integer.MIN_VALUE).getDay());
432 Assert.assertEquals(Integer.MIN_VALUE, new DateComponents(Integer.MIN_VALUE).getJ2000Day());
433 }
434
435 }