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