1   /* Copyright 2002-2025 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.time;
18  
19  import java.sql.Timestamp;
20  import java.time.Instant;
21  import java.time.LocalDateTime;
22  import java.time.OffsetDateTime;
23  import java.time.ZoneOffset;
24  import java.time.ZonedDateTime;
25  import java.time.format.DateTimeFormatter;
26  import java.util.Date;
27  import java.util.TimeZone;
28  
29  import java.util.concurrent.TimeUnit;
30  import org.hamcrest.CoreMatchers;
31  import org.hamcrest.MatcherAssert;
32  import org.hipparchus.util.FastMath;
33  import org.hipparchus.util.Precision;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  import org.orekit.OrekitMatchers;
38  import org.orekit.Utils;
39  import org.orekit.annotation.DefaultDataContext;
40  import org.orekit.data.DataContext;
41  import org.orekit.errors.OrekitException;
42  import org.orekit.errors.OrekitIllegalArgumentException;
43  import org.orekit.errors.OrekitMessages;
44  import org.orekit.utils.Constants;
45  
46  class AbsoluteDateTest {
47  
48      @Test
49      @DefaultDataContext
50      void testStandardEpoch() {
51          TimeScale tai = TimeScalesFactory.getTAI();
52          TimeScale tt  = TimeScalesFactory.getTT();
53          Assertions.assertEquals(-210866760000000L, AbsoluteDate.JULIAN_EPOCH.toDate(tt).getTime());
54          Assertions.assertEquals(-3506716800000L,   AbsoluteDate.MODIFIED_JULIAN_EPOCH.toDate(tt).getTime());
55          Assertions.assertEquals(-631152000000L,    AbsoluteDate.FIFTIES_EPOCH.toDate(tt).getTime());
56          Assertions.assertEquals(-378691200000L,    AbsoluteDate.CCSDS_EPOCH.toDate(tai).getTime());
57          Assertions.assertEquals(935280019000L,     AbsoluteDate.GALILEO_EPOCH.toDate(tai).getTime());
58          Assertions.assertEquals(315964819000L,     AbsoluteDate.GPS_EPOCH.toDate(tai).getTime());
59          Assertions.assertEquals(315964819000L,     AbsoluteDate.QZSS_EPOCH.toDate(tai).getTime());
60          Assertions.assertEquals(1136073633000L,    AbsoluteDate.BEIDOU_EPOCH.toDate(tai).getTime());
61          Assertions.assertEquals(820443629000L,     AbsoluteDate.GLONASS_EPOCH.toDate(tai).getTime());
62          Assertions.assertEquals(935280019000L,     AbsoluteDate.NAVIC_EPOCH.toDate(tai).getTime());
63          Assertions.assertEquals(946728000000L,     AbsoluteDate.J2000_EPOCH.toDate(tt).getTime());
64      }
65  
66      @Test
67      @DefaultDataContext
68      void testStandardEpochStrings() {
69          Assertions.assertEquals("-4712-01-01T12:00:00.000",
70                       AbsoluteDate.JULIAN_EPOCH.toString(TimeScalesFactory.getTT()));
71          Assertions.assertEquals("1858-11-17T00:00:00.000",
72                       AbsoluteDate.MODIFIED_JULIAN_EPOCH.toString(TimeScalesFactory.getTT()));
73          Assertions.assertEquals("1950-01-01T00:00:00.000",
74                              AbsoluteDate.FIFTIES_EPOCH.toString(TimeScalesFactory.getTT()));
75          Assertions.assertEquals("1958-01-01T00:00:00.000",
76                              AbsoluteDate.CCSDS_EPOCH.toString(TimeScalesFactory.getTAI()));
77          Assertions.assertEquals("1999-08-21T23:59:47.000",
78                              AbsoluteDate.GALILEO_EPOCH.toString(TimeScalesFactory.getUTC()));
79          Assertions.assertEquals("1980-01-06T00:00:00.000",
80                              AbsoluteDate.GPS_EPOCH.toString(TimeScalesFactory.getUTC()));
81          Assertions.assertEquals("1980-01-06T00:00:00.000",
82                              AbsoluteDate.QZSS_EPOCH.toString(TimeScalesFactory.getUTC()));
83          Assertions.assertEquals("2006-01-01T00:00:00.000",
84                              AbsoluteDate.BEIDOU_EPOCH.toString(TimeScalesFactory.getUTC()));
85          Assertions.assertEquals("1995-12-31T21:00:00.000",
86                              AbsoluteDate.GLONASS_EPOCH.toString(TimeScalesFactory.getUTC()));
87          Assertions.assertEquals("1999-08-21T23:59:47.000",
88                  AbsoluteDate.NAVIC_EPOCH.toString(TimeScalesFactory.getUTC()));
89          Assertions.assertEquals("2000-01-01T12:00:00.000",
90                       AbsoluteDate.J2000_EPOCH.toString(TimeScalesFactory.getTT()));
91          Assertions.assertEquals("1970-01-01T00:00:00.000",
92                       AbsoluteDate.JAVA_EPOCH.toString(TimeScalesFactory.getUTC()));
93      }
94  
95      @Test
96      void testJulianEpochRate() {
97  
98          for (int i = 0; i < 10; ++i) {
99              AbsoluteDate j200i = AbsoluteDate.createJulianEpoch(2000.0 + i);
100             AbsoluteDate j2000 = AbsoluteDate.J2000_EPOCH;
101             double expected    = i * Constants.JULIAN_YEAR;
102             Assertions.assertEquals(expected, j200i.durationFrom(j2000), 4.0e-15 * expected);
103         }
104 
105     }
106 
107     @Test
108     void testBesselianEpochRate() {
109 
110         for (int i = 0; i < 10; ++i) {
111             AbsoluteDate b195i = AbsoluteDate.createBesselianEpoch(1950.0 + i);
112             AbsoluteDate b1950 = AbsoluteDate.createBesselianEpoch(1950.0);
113             double expected    = i * Constants.BESSELIAN_YEAR;
114             Assertions.assertEquals(expected, b195i.durationFrom(b1950), 4.0e-15 * expected);
115         }
116 
117     }
118 
119     @Test
120     void testLieske() {
121 
122         // the following test values correspond to table 1 in the paper:
123         // Precession Matrix Based on IAU (1976) System of Astronomical Constants,
124         // Jay H. Lieske, Astronomy and Astrophysics, vol. 73, no. 3, Mar. 1979, p. 282-284
125         // http://articles.adsabs.harvard.edu/cgi-bin/nph-iarticle_query?1979A%26A....73..282L&defaultprint=YES&filetype=.pdf
126 
127         // published table, with limited accuracy
128         final double publishedEpsilon = 1.0e-6 * Constants.JULIAN_YEAR;
129         checkEpochs(1899.999142, 1900.000000, publishedEpsilon);
130         checkEpochs(1900.000000, 1900.000858, publishedEpsilon);
131         checkEpochs(1950.000000, 1949.999790, publishedEpsilon);
132         checkEpochs(1950.000210, 1950.000000, publishedEpsilon);
133         checkEpochs(2000.000000, 1999.998722, publishedEpsilon);
134         checkEpochs(2000.001278, 2000.000000, publishedEpsilon);
135 
136         // recomputed table, using directly Lieske formulas (i.e. *not* Orekit implementation) with high accuracy
137         final double accurateEpsilon = 1.2e-13 * Constants.JULIAN_YEAR;
138         checkEpochs(1899.99914161068724704, 1900.00000000000000000, accurateEpsilon);
139         checkEpochs(1900.00000000000000000, 1900.00085837097878165, accurateEpsilon);
140         checkEpochs(1950.00000000000000000, 1949.99979044229979466, accurateEpsilon);
141         checkEpochs(1950.00020956217615449, 1950.00000000000000000, accurateEpsilon);
142         checkEpochs(2000.00000000000000000, 1999.99872251362080766, accurateEpsilon);
143         checkEpochs(2000.00127751366506194, 2000.00000000000000000, accurateEpsilon);
144 
145     }
146 
147     private void checkEpochs(final double besselianEpoch, final double julianEpoch, final double epsilon) {
148         final AbsoluteDate b = AbsoluteDate.createBesselianEpoch(besselianEpoch);
149         final AbsoluteDate j = AbsoluteDate.createJulianEpoch(julianEpoch);
150         Assertions.assertEquals(0.0, b.durationFrom(j), epsilon);
151     }
152 
153     @Test
154     void testParse() {
155         Assertions.assertEquals(AbsoluteDate.MODIFIED_JULIAN_EPOCH,
156                             new AbsoluteDate("1858-W46-3", TimeScalesFactory.getTT()));
157         Assertions.assertEquals(AbsoluteDate.JULIAN_EPOCH,
158                             new AbsoluteDate("-4712-01-01T12:00:00.000", TimeScalesFactory.getTT()));
159         Assertions.assertEquals(AbsoluteDate.FIFTIES_EPOCH,
160                             new AbsoluteDate("1950-01-01", TimeScalesFactory.getTT()));
161         Assertions.assertEquals(AbsoluteDate.CCSDS_EPOCH,
162                             new AbsoluteDate("1958-001", TimeScalesFactory.getTAI()));
163     }
164 
165     @Test
166     void testLocalTimeParsing() {
167         TimeScale utc = TimeScalesFactory.getUTC();
168         Assertions.assertEquals(new AbsoluteDate("2011-12-31T23:00:00",       utc),
169                             new AbsoluteDate("2012-01-01T03:30:00+04:30", utc));
170         Assertions.assertEquals(new AbsoluteDate("2011-12-31T23:00:00",       utc),
171                             new AbsoluteDate("2012-01-01T03:30:00+0430",  utc));
172         Assertions.assertEquals(new AbsoluteDate("2011-12-31T23:30:00",       utc),
173                             new AbsoluteDate("2012-01-01T03:30:00+04",    utc));
174         Assertions.assertEquals(new AbsoluteDate("2012-01-01T05:17:00",       utc),
175                             new AbsoluteDate("2011-12-31T22:17:00-07:00", utc));
176         Assertions.assertEquals(new AbsoluteDate("2012-01-01T05:17:00",       utc),
177                             new AbsoluteDate("2011-12-31T22:17:00-0700",  utc));
178         Assertions.assertEquals(new AbsoluteDate("2012-01-01T05:17:00",       utc),
179                             new AbsoluteDate("2011-12-31T22:17:00-07",    utc));
180     }
181 
182     @Test
183     void testTimeZoneDisplay() {
184         final TimeScale utc = TimeScalesFactory.getUTC();
185         final AbsoluteDate date = new AbsoluteDate("2000-01-01T01:01:01.000", utc);
186         Assertions.assertEquals("2000-01-01T01:01:01.000Z",      date.toString());
187         Assertions.assertEquals("2000-01-01T11:01:01.000+10:00", date.toString( 600));
188         Assertions.assertEquals("1999-12-31T23:01:01.000-02:00", date.toString(-120));
189         Assertions.assertEquals("2000-01-01T01:01:01.000+00:00", date.toString(0));
190 
191         // winter time, Europe is one hour ahead of UTC
192         TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
193         Assertions.assertEquals("2001-01-22T11:30:00.000+01:00",
194                             new AbsoluteDate("2001-01-22T10:30:00", utc).toString(tz));
195 
196         // summer time, Europe is two hours ahead of UTC
197         Assertions.assertEquals("2001-06-23T11:30:00.000+02:00",
198                             new AbsoluteDate("2001-06-23T09:30:00", utc).toString(tz));
199 
200         // check with UTC
201         tz = TimeZone.getTimeZone("UTC");
202         Assertions.assertEquals("2001-06-23T09:30:00.000+00:00",
203                 new AbsoluteDate("2001-06-23T09:30:00", utc).toString(tz));
204 
205     }
206 
207     @Test
208     void testLocalTimeLeapSecond() {
209 
210         TimeScale utc = TimeScalesFactory.getUTC();
211         AbsoluteDate beforeLeap = new AbsoluteDate("2012-06-30T23:59:59.8", utc);
212         AbsoluteDate inLeap     = new AbsoluteDate("2012-06-30T23:59:60.5", utc);
213         Assertions.assertEquals(0.7, inLeap.durationFrom(beforeLeap), 1.0e-12);
214         for (int minutesFromUTC = -1500; minutesFromUTC < 1500; ++minutesFromUTC) {
215             DateTimeComponents dtcBeforeLeap = beforeLeap.getComponents(minutesFromUTC);
216             DateTimeComponents dtcInsideLeap = inLeap.getComponents(minutesFromUTC);
217             Assertions.assertEquals(dtcBeforeLeap.getDate(), dtcInsideLeap.getDate());
218             Assertions.assertEquals(dtcBeforeLeap.getTime().getHour(), dtcInsideLeap.getTime().getHour());
219             Assertions.assertEquals(dtcBeforeLeap.getTime().getMinute(), dtcInsideLeap.getTime().getMinute());
220             Assertions.assertEquals(minutesFromUTC, dtcBeforeLeap.getTime().getMinutesFromUTC());
221             Assertions.assertEquals(minutesFromUTC, dtcInsideLeap.getTime().getMinutesFromUTC());
222             Assertions.assertEquals(59.8, dtcBeforeLeap.getTime().getSecond(), 1.0e-10);
223             Assertions.assertEquals(60.5, dtcInsideLeap.getTime().getSecond(), 1.0e-10);
224         }
225 
226     }
227 
228     @Test
229     void testTimeZoneLeapSecond() {
230 
231         TimeScale utc = TimeScalesFactory.getUTC();
232         final TimeZone tz = TimeZone.getTimeZone("Europe/Paris");
233         AbsoluteDate localBeforeMidnight = new AbsoluteDate("2012-06-30T21:59:59.800", utc);
234         Assertions.assertEquals("2012-06-30T23:59:59.800+02:00",
235                             localBeforeMidnight.toString(tz));
236         Assertions.assertEquals("2012-07-01T00:00:00.800+02:00",
237                             localBeforeMidnight.shiftedBy(1.0).toString(tz));
238 
239         AbsoluteDate beforeLeap = new AbsoluteDate("2012-06-30T23:59:59.8", utc);
240         AbsoluteDate inLeap     = new AbsoluteDate("2012-06-30T23:59:60.5", utc);
241         Assertions.assertEquals(0.7, inLeap.durationFrom(beforeLeap), 1.0e-12);
242         Assertions.assertEquals("2012-07-01T01:59:59.800+02:00", beforeLeap.toString(tz));
243         Assertions.assertEquals("2012-07-01T01:59:60.500+02:00", inLeap.toString(tz));
244 
245     }
246 
247     @Test
248     void testParseLeap() {
249         TimeScale utc = TimeScalesFactory.getUTC();
250         AbsoluteDate beforeLeap = new AbsoluteDate("2012-06-30T23:59:59.8", utc);
251         AbsoluteDate inLeap     = new AbsoluteDate("2012-06-30T23:59:60.5", utc);
252         Assertions.assertEquals(0.7, inLeap.durationFrom(beforeLeap), 1.0e-12);
253         Assertions.assertEquals("2012-06-30T23:59:60.500", inLeap.toString(utc));
254     }
255 
256     @Test
257     void testOutput() {
258         TimeScale tt = TimeScalesFactory.getTT();
259         Assertions.assertEquals("1950-01-01T01:01:01.000",
260                             AbsoluteDate.FIFTIES_EPOCH.shiftedBy(3661.0).toString(tt));
261         Assertions.assertEquals("2000-01-01T13:01:01.000",
262                             AbsoluteDate.J2000_EPOCH.shiftedBy(3661.0).toString(tt));
263     }
264 
265     @Test
266     void testJ2000() {
267         Assertions.assertEquals("2000-01-01T12:00:00.000",
268                      AbsoluteDate.J2000_EPOCH.toString(TimeScalesFactory.getTT()));
269         Assertions.assertEquals("2000-01-01T11:59:27.816",
270                      AbsoluteDate.J2000_EPOCH.toString(TimeScalesFactory.getTAI()));
271         Assertions.assertEquals("2000-01-01T11:58:55.816",
272                      AbsoluteDate.J2000_EPOCH.toString(utc));
273     }
274 
275     @Test
276     void testFraction() {
277         AbsoluteDate d =
278             new AbsoluteDate(new DateComponents(2000, 1, 1), new TimeComponents(11, 59, 27.816),
279                              TimeScalesFactory.getTAI());
280         Assertions.assertEquals(0, d.durationFrom(AbsoluteDate.J2000_EPOCH), 1.0e-10);
281     }
282 
283     @Test
284     void testScalesOffset() {
285         AbsoluteDate date = new AbsoluteDate(new DateComponents(2006, 2, 24),
286                                              new TimeComponents(15, 38, 0),
287                                              utc);
288         Assertions.assertEquals(33,
289                      date.timeScalesOffset(TimeScalesFactory.getTAI(), utc),
290                      1.0e-10);
291     }
292 
293     @Test
294     void testUTC() {
295         AbsoluteDate date = new AbsoluteDate(new DateComponents(2002, 1, 1),
296                                              new TimeComponents(0, 0, 1),
297                                              utc);
298         Assertions.assertEquals("2002-01-01T00:00:01.000Z", date.toString());
299     }
300 
301     @Test
302     void test1970() {
303         AbsoluteDate date = new AbsoluteDate(new Date(0L), utc);
304         Assertions.assertEquals("1970-01-01T00:00:00.000Z", date.toString());
305     }
306 
307     @Test
308     void test1970Instant() {
309         Assertions.assertEquals("1970-01-01T00:00:00.000Z", new AbsoluteDate(Instant.EPOCH, utc).toString());
310         Assertions.assertEquals("1970-01-01T00:00:00.000Z", new AbsoluteDate(Instant.ofEpochMilli(0L), utc).toString());
311         Assertions.assertEquals("1970-01-01T00:00:00.000Z", new AbsoluteDate(Instant.EPOCH, (UTCScale) utc).toString());
312         Assertions.assertEquals("1970-01-01T00:00:00.000Z", new AbsoluteDate(Instant.ofEpochMilli(0L), (UTCScale) utc).toString());
313     }
314 
315     @Test
316     void testInstantAccuracy() {
317         Assertions.assertEquals("1970-01-02T00:16:40.123456789Z", new AbsoluteDate(Instant.ofEpochSecond(87400, 123456789), utc).toString());
318         Assertions.assertEquals("1970-01-07T00:10:00.123456789Z", new AbsoluteDate(Instant.ofEpochSecond(519000, 123456789), utc).toString());
319         Assertions.assertEquals("1970-01-02T00:16:40.123456789Z", new AbsoluteDate(Instant.ofEpochSecond(87400, 123456789), (UTCScale) utc).toString());
320         Assertions.assertEquals("1970-01-07T00:10:00.123456789Z", new AbsoluteDate(Instant.ofEpochSecond(519000, 123456789), (UTCScale) utc).toString());
321     }
322 
323     @Test
324     void testToInstant() {
325         Assertions.assertEquals(Instant.ofEpochSecond(0), new AbsoluteDate("1970-01-01T00:00:00.000Z", utc).toInstant());
326         Assertions.assertEquals(Instant.ofEpochSecond(0), new AbsoluteDate("1970-01-01T00:00:00.000Z", utc).toInstant(TimeScalesFactory.getTimeScales()));
327 
328         Instant expectedInstant = Instant.ofEpochSecond(519000, 123456789);
329         Assertions.assertEquals(expectedInstant, new AbsoluteDate("1970-01-07T00:10:00.123456789Z", utc).toInstant());
330         Assertions.assertEquals(expectedInstant, new AbsoluteDate("1970-01-07T00:10:00.123456789Z", utc).toInstant(TimeScalesFactory.getTimeScales()));
331 
332         Assertions.assertEquals(OffsetDateTime.parse("2024-05-15T09:32:36.123456789Z", DateTimeFormatter.ISO_DATE_TIME).toInstant(),
333             new AbsoluteDate("2024-05-15T09:32:36.123456789Z", utc).toInstant());
334         Assertions.assertEquals(OffsetDateTime.parse("2024-05-15T09:32:36.123456789Z", DateTimeFormatter.ISO_DATE_TIME).toInstant(),
335             new AbsoluteDate("2024-05-15T09:32:36.123456789Z", utc).toInstant(TimeScalesFactory.getTimeScales()));
336     }
337 
338     @Test
339     void testUtcGpsOffset() {
340         AbsoluteDate date1   = new AbsoluteDate(new DateComponents(2005, 8, 9),
341                                                 new TimeComponents(16, 31, 17),
342                                                 utc);
343         AbsoluteDate date2   = new AbsoluteDate(new DateComponents(2006, 8, 9),
344                                                 new TimeComponents(16, 31, 17),
345                                                 utc);
346         AbsoluteDate dateRef = new AbsoluteDate(new DateComponents(1980, 1, 6),
347                                                 TimeComponents.H00,
348                                                 utc);
349 
350         // 13 seconds offset between GPS time and UTC in 2005
351         long noLeapGap = ((9347 * 24 + 16) * 60 + 31) * 60 + 17;
352         long realGap   = (long) date1.durationFrom(dateRef);
353         Assertions.assertEquals(13L, realGap - noLeapGap);
354 
355         // 14 seconds offset between GPS time and UTC in 2006
356         noLeapGap = ((9712 * 24 + 16) * 60 + 31) * 60 + 17;
357         realGap   = (long) date2.durationFrom(dateRef);
358         Assertions.assertEquals(14L, realGap - noLeapGap);
359 
360     }
361 
362     @Test
363     void testMJDDate() {
364         AbsoluteDate dateA = AbsoluteDate.createMJDDate(51544, 0.5 * Constants.JULIAN_DAY,
365                                                              TimeScalesFactory.getTT());
366         Assertions.assertEquals(0.0, AbsoluteDate.J2000_EPOCH.durationFrom(dateA), 1.0e-15);
367         AbsoluteDate dateB = AbsoluteDate.createMJDDate(53774, 0.0, TimeScalesFactory.getUTC());
368         AbsoluteDate dateC = new AbsoluteDate("2006-02-08T00:00:00", TimeScalesFactory.getUTC());
369         Assertions.assertEquals(0.0, dateC.durationFrom(dateB), 1.0e-15);
370     }
371 
372     @Test
373     void testJDDate() {
374         final AbsoluteDate date = AbsoluteDate.createJDDate(2400000, 0.5 * Constants.JULIAN_DAY,
375                                                             TimeScalesFactory.getTT());
376         Assertions.assertEquals(0.0, AbsoluteDate.MODIFIED_JULIAN_EPOCH.durationFrom(date), 1.0e-15);
377     }
378 
379     /** Test issue 1310: get a date from a JD using a pivot timescale. */
380     @Test
381     void testIssue1310JDDateInTDB() {
382         // Given
383         // -----
384         final TDBScale TDBscale = TimeScalesFactory.getTDB();
385         final AbsoluteDate refDate = new AbsoluteDate("2023-08-01T00:00:00.000", TDBscale);
386 
387         // When
388         // ----
389         final AbsoluteDate wrongDate  = AbsoluteDate.createJDDate(2460157,
390                 Constants.JULIAN_DAY / 2.0d, TDBscale);
391         final AbsoluteDate properDate = AbsoluteDate.createJDDate(2460157,
392                 Constants.JULIAN_DAY/2.0d, TDBscale, TimeScalesFactory.getTT());
393 
394         // Then
395         // ----
396 
397         // Wrong date is too far from reference date
398         Assertions.assertEquals(0.0, wrongDate.durationFrom(refDate), 1.270e-05);
399 
400         // Proper date is close enough from reference date
401         Assertions.assertEquals(0.0, properDate.durationFrom(refDate), 2.132e-13);
402     }
403 
404     @Test
405     void testMedian() {
406         final AbsoluteDate date1 = new AbsoluteDate(2003, 6, 13, 14, 15,
407                                                     new TimeOffset(53, TimeOffset.SECOND, 12, TimeOffset.ATTOSECOND),
408                                                     TimeScalesFactory.getTT());
409         final AbsoluteDate date2 = new AbsoluteDate(2003, 6, 13, 14, 17,
410                                                     new TimeOffset(25, TimeOffset.SECOND, 120, TimeOffset.ATTOSECOND),
411                                                     TimeScalesFactory.getTT());
412         final AbsoluteDate dateM = new AbsoluteDate(2003, 6, 13, 14, 16,
413                                                  new TimeOffset(39, TimeOffset.SECOND, 66, TimeOffset.ATTOSECOND),
414                                                  TimeScalesFactory.getTT());
415         Assertions.assertEquals(dateM, AbsoluteDate.createMedian(date1, date2));
416         Assertions.assertEquals(dateM, AbsoluteDate.createMedian(date2, date1));
417     }
418 
419     @Test
420     void testMedianInfinite() {
421         Assertions.assertEquals(AbsoluteDate.FUTURE_INFINITY,
422                                 AbsoluteDate.createMedian(AbsoluteDate.FUTURE_INFINITY,
423                                                           AbsoluteDate.ARBITRARY_EPOCH));
424         Assertions.assertEquals(AbsoluteDate.PAST_INFINITY,
425                                 AbsoluteDate.createMedian(AbsoluteDate.PAST_INFINITY,
426                                                           AbsoluteDate.ARBITRARY_EPOCH));
427     }
428 
429     @Test
430     void testOffsets() {
431         final TimeScale tai = TimeScalesFactory.getTAI();
432         AbsoluteDate leapStartUTC = new AbsoluteDate(1976, 12, 31, 23, 59, 59, utc);
433         AbsoluteDate leapEndUTC   = new AbsoluteDate(1977,  1,  1,  0,  0,  0, utc);
434         AbsoluteDate leapStartTAI = new AbsoluteDate(1977,  1,  1,  0,  0, 14, tai);
435         AbsoluteDate leapEndTAI   = new AbsoluteDate(1977,  1,  1,  0,  0, 16, tai);
436         Assertions.assertEquals(leapStartUTC, leapStartTAI);
437         Assertions.assertEquals(leapEndUTC, leapEndTAI);
438         Assertions.assertEquals(1, leapEndUTC.offsetFrom(leapStartUTC, utc), 1.0e-10);
439         Assertions.assertEquals(1, leapEndTAI.offsetFrom(leapStartTAI, utc), 1.0e-10);
440         Assertions.assertEquals(2, leapEndUTC.offsetFrom(leapStartUTC, tai), 1.0e-10);
441         Assertions.assertEquals(2, leapEndTAI.offsetFrom(leapStartTAI, tai), 1.0e-10);
442         Assertions.assertEquals(2, leapEndUTC.durationFrom(leapStartUTC),    1.0e-10);
443         Assertions.assertEquals(2, leapEndTAI.durationFrom(leapStartTAI),    1.0e-10);
444     }
445 
446     @Test
447     void testBeforeAndAfterLeap() {
448         final TimeScale tai = TimeScalesFactory.getTAI();
449         AbsoluteDate leapStart = new AbsoluteDate(1977,  1,  1,  0,  0, 14, tai);
450         AbsoluteDate leapEnd   = new AbsoluteDate(1977,  1,  1,  0,  0, 16, tai);
451         for (int i = -10; i < 10; ++i) {
452             final double dt = 1.1 * (2 * i - 1);
453             AbsoluteDate d1 = leapStart.shiftedBy(dt);
454             AbsoluteDate d2 = new AbsoluteDate(leapStart, dt, tai);
455             AbsoluteDate d3 = new AbsoluteDate(leapStart, dt, utc);
456             AbsoluteDate d4 = new AbsoluteDate(leapEnd,   dt, tai);
457             AbsoluteDate d5 = new AbsoluteDate(leapEnd,   dt, utc);
458             Assertions.assertTrue(FastMath.abs(d1.durationFrom(d2)) < 1.0e-10);
459             if (dt < 0) {
460                 Assertions.assertTrue(FastMath.abs(d2.durationFrom(d3)) < 1.0e-10);
461                 Assertions.assertTrue(d4.durationFrom(d5) > (1.0 - 1.0e-10));
462             } else {
463                 Assertions.assertTrue(d2.durationFrom(d3) < (-1.0 + 1.0e-10));
464                 Assertions.assertTrue(FastMath.abs(d4.durationFrom(d5)) < 1.0e-10);
465             }
466         }
467     }
468 
469     @Test
470     void testSymmetry() {
471         final TimeScale tai = TimeScalesFactory.getTAI();
472         AbsoluteDate leapStart = new AbsoluteDate(1977,  1,  1,  0,  0, 14, tai);
473         for (int i = -10; i < 10; ++i) {
474             final double dt = 1.1 * (2 * i - 1);
475             Assertions.assertEquals(dt, new AbsoluteDate(leapStart, dt, utc).offsetFrom(leapStart, utc), 1.0e-10);
476             Assertions.assertEquals(dt, new AbsoluteDate(leapStart, dt, tai).offsetFrom(leapStart, tai), 1.0e-10);
477             Assertions.assertEquals(dt, leapStart.shiftedBy(dt).durationFrom(leapStart), 1.0e-10);
478         }
479     }
480 
481     @SuppressWarnings("unlikely-arg-type")
482     @Test
483     void testEquals() {
484         AbsoluteDate d1 =
485             new AbsoluteDate(new DateComponents(2006, 2, 25),
486                              new TimeComponents(17, 10, 34),
487                              utc);
488         AbsoluteDate d2 = new AbsoluteDate(new DateComponents(2006, 2, 25),
489                                            new TimeComponents(17, 10, 0),
490                                            utc).shiftedBy(34);
491         Assertions.assertEquals(d1, d2);
492         Assertions.assertNotEquals(d1, this);
493     }
494 
495     @Test
496     void testComponents() {
497         // this is NOT J2000.0,
498         // it is either a few seconds before or after depending on time scale
499         DateComponents date = new DateComponents(2000, 1, 1);
500         TimeComponents time = new TimeComponents(11, 59, 10);
501         TimeScale[] scales = {
502             TimeScalesFactory.getTAI(), TimeScalesFactory.getUTC(),
503             TimeScalesFactory.getTT(), TimeScalesFactory.getTCG()
504         };
505         for (int i = 0; i < scales.length; ++i) {
506             AbsoluteDate in = new AbsoluteDate(date, time, scales[i]);
507             for (int j = 0; j < scales.length; ++j) {
508                 DateTimeComponents pair = in.getComponents(scales[j]);
509                 if (i == j) {
510                     Assertions.assertEquals(date, pair.getDate());
511                     Assertions.assertEquals(time, pair.getTime());
512                 } else {
513                     Assertions.assertNotSame(date, pair.getDate());
514                     Assertions.assertNotSame(time, pair.getTime());
515                 }
516             }
517         }
518     }
519 
520     @Test
521     void testMonth() {
522         TimeScale utc = TimeScalesFactory.getUTC();
523         Assertions.assertEquals(new AbsoluteDate(2011, 2, 23, utc),
524                             new AbsoluteDate(2011, Month.FEBRUARY, 23, utc));
525         Assertions.assertEquals(new AbsoluteDate(2011, 2, 23, 1, 2, 3.4, utc),
526                             new AbsoluteDate(2011, Month.FEBRUARY, 23, 1, 2, 3.4, utc));
527     }
528 
529     @Test
530     void testCCSDSUnsegmentedNoExtension() {
531 
532         AbsoluteDate reference = new AbsoluteDate("2002-05-23T12:34:56.789", utc);
533         double lsb = FastMath.pow(2.0, -24);
534 
535         byte[] timeCCSDSEpoch = new byte[] { 0x53, 0x7F, 0x40, -0x70, -0x37, -0x05, -0x19 };
536         for (int preamble = 0x00; preamble < 0x80; ++preamble) {
537             if (preamble == 0x1F) {
538                 // using CCSDS reference epoch
539                 AbsoluteDate ccsds1 =
540                     AbsoluteDate.parseCCSDSUnsegmentedTimeCode((byte) preamble, (byte) 0x0, timeCCSDSEpoch, null);
541                 Assertions.assertEquals(0, ccsds1.durationFrom(reference), lsb / 2);
542             } else {
543                 try {
544                     AbsoluteDate.parseCCSDSUnsegmentedTimeCode((byte) preamble, (byte) 0x0, timeCCSDSEpoch, null);
545                     Assertions.fail("an exception should have been thrown");
546                 } catch (OrekitException iae) {
547                     // expected
548                 }
549 
550             }
551         }
552 
553         // missing epoch
554         byte[] timeJ2000Epoch = new byte[] { 0x04, 0x7E, -0x0B, -0x10, -0x07, 0x16, -0x79 };
555         try {
556             AbsoluteDate.parseCCSDSUnsegmentedTimeCode((byte) 0x2F, (byte) 0x0, timeJ2000Epoch, null);
557             Assertions.fail("an exception should have been thrown");
558         } catch (OrekitException iae) {
559             // expected
560         }
561 
562         // using J2000.0 epoch
563         AbsoluteDate ccsds3 =
564             AbsoluteDate.parseCCSDSUnsegmentedTimeCode((byte) 0x2F, (byte) 0x0, timeJ2000Epoch, AbsoluteDate.J2000_EPOCH);
565         Assertions.assertEquals(0, ccsds3.durationFrom(reference), lsb / 2);
566 
567     }
568 
569     @Test
570     void testCCSDSUnsegmentedWithExtendedPreamble() {
571 
572         AbsoluteDate reference = new AbsoluteDate("2095-03-03T22:02:45.789012345678901", utc);
573         int leap = (int) FastMath.rint(utc.offsetFromTAI(reference).toDouble());
574 
575         byte extendedPreamble = (byte) -0x80;
576         byte identification   = (byte)  0x10;
577         byte coarseLength1    = (byte)  0x0C; // four (3 + 1) bytes
578         byte fineLength1      = (byte)  0x03; // 3 bytes
579         byte coarseLength2    = (byte)  0x20; // 1 additional byte for coarse time
580         byte fineLength2      = (byte)  0x10; // 4 additional bytes for fine time
581         byte[] timeCCSDSEpoch = new byte[] {
582              0x01,  0x02,  0x03,  0x04,  (byte)(0x05 - leap), // 5 bytes for coarse time (seconds)
583             -0x37, -0x04, -0x4A, -0x74, -0x2C, -0x3C, -0x48   // 7 bytes for fine time (sub-seconds)
584         };
585         byte preamble1 = (byte) (extendedPreamble | identification | coarseLength1 | fineLength1);
586         byte preamble2 = (byte) (coarseLength2 | fineLength2);
587         AbsoluteDate ccsds1 =
588                 AbsoluteDate.parseCCSDSUnsegmentedTimeCode(preamble1, preamble2, timeCCSDSEpoch, null);
589 
590         // The 8 attoseconds difference comes from the fact unsegmented time is
591         // in powers of 1/256 s, so it is not a whole number of attoseconds
592         Assertions.assertEquals(-8.0e-18, ccsds1.durationFrom(reference), 1.0e-18);
593 
594     }
595 
596     @Test
597     void testCCSDSDaySegmented() {
598 
599         AbsoluteDate reference = new AbsoluteDate("2002-05-23T12:34:56.789012345678", TimeScalesFactory.getUTC());
600         double lsb = 1.0e-13;
601         byte[] timeCCSDSEpoch = new byte[] { 0x3F, 0x55, 0x02, -0x4D, 0x2C, -0x6B, 0x00, -0x44, 0x61, 0x4E };
602 
603         for (int preamble = 0x00; preamble < 0x100; ++preamble) {
604             if (preamble == 0x42) {
605                 // using CCSDS reference epoch
606                 AbsoluteDate ccsds1 =
607                     AbsoluteDate.parseCCSDSDaySegmentedTimeCode((byte) preamble, timeCCSDSEpoch, null);
608                 Assertions.assertEquals(0, ccsds1.durationFrom(reference), lsb / 2);
609             } else {
610                 try {
611                     AbsoluteDate.parseCCSDSDaySegmentedTimeCode((byte) preamble, timeCCSDSEpoch, null);
612                     Assertions.fail("an exception should have been thrown");
613                 } catch (OrekitException iae) {
614                     // expected
615                 }
616 
617             }
618         }
619 
620         // missing epoch
621         byte[] timeJ2000Epoch = new byte[] { 0x03, 0x69, 0x02, -0x4D, 0x2C, -0x6B, 0x00, -0x44, 0x61, 0x4E };
622         try {
623             AbsoluteDate.parseCCSDSDaySegmentedTimeCode((byte) 0x4A, timeJ2000Epoch, null);
624             Assertions.fail("an exception should have been thrown");
625         } catch (OrekitException iae) {
626             // expected
627         }
628 
629         // using J2000.0 epoch
630         AbsoluteDate ccsds3 =
631             AbsoluteDate.parseCCSDSDaySegmentedTimeCode((byte) 0x4A, timeJ2000Epoch, DateComponents.J2000_EPOCH);
632         Assertions.assertEquals(0, ccsds3.durationFrom(reference), lsb / 2);
633 
634         // limit to microsecond
635         byte[] timeMicrosecond = new byte[] { 0x03, 0x69, 0x02, -0x4D, 0x2C, -0x6B, 0x00, 0x0C };
636         AbsoluteDate ccsds4 =
637             AbsoluteDate.parseCCSDSDaySegmentedTimeCode((byte) 0x49, timeMicrosecond, DateComponents.J2000_EPOCH);
638         Assertions.assertEquals(-0.345678e-6, ccsds4.durationFrom(reference), lsb / 2);
639 
640     }
641 
642     @Test
643     void testCCSDSCalendarSegmented() {
644 
645         AbsoluteDate reference = new AbsoluteDate("2002-05-23T12:34:56.789012345678", TimeScalesFactory.getUTC());
646         double lsb = 1.0e-13;
647 
648         // month of year / day of month variation
649         byte[] timeMonthDay = new byte[] { 0x07, -0x2E, 0x05, 0x17, 0x0C, 0x22, 0x38, 0x4E, 0x5A, 0x0C, 0x22, 0x38, 0x4E };
650         for (int preamble = 0x00; preamble < 0x100; ++preamble) {
651             if (preamble == 0x56) {
652                 AbsoluteDate ccsds1 =
653                     AbsoluteDate.parseCCSDSCalendarSegmentedTimeCode((byte) preamble, timeMonthDay);
654                 Assertions.assertEquals(0, ccsds1.durationFrom(reference), lsb / 2);
655             } else {
656                 try {
657                     AbsoluteDate.parseCCSDSCalendarSegmentedTimeCode((byte) preamble, timeMonthDay);
658                     Assertions.fail("an exception should have been thrown");
659                 } catch (OrekitException iae) {
660                     // expected
661                 } catch (IllegalArgumentException iae) {
662                     // should happen when preamble specifies day of year variation
663                     // since there is no day 1303 (= 5 * 256 + 23) in any year ...
664                     Assertions.assertEquals(preamble & 0x08, 0x08);
665                 }
666 
667             }
668         }
669 
670         // day of year variation
671         byte[] timeDay = new byte[] { 0x07, -0x2E, 0x00, -0x71, 0x0C, 0x22, 0x38, 0x4E, 0x5A, 0x0C, 0x22, 0x38, 0x4E };
672         for (int preamble = 0x00; preamble < 0x100; ++preamble) {
673             if (preamble == 0x5E) {
674                 AbsoluteDate ccsds1 =
675                     AbsoluteDate.parseCCSDSCalendarSegmentedTimeCode((byte) preamble, timeDay);
676                 Assertions.assertEquals(0, ccsds1.durationFrom(reference), lsb / 2);
677             } else {
678                 try {
679                     AbsoluteDate.parseCCSDSCalendarSegmentedTimeCode((byte) preamble, timeDay);
680                     Assertions.fail("an exception should have been thrown");
681                 } catch (OrekitException iae) {
682                     // expected
683                 } catch (IllegalArgumentException iae) {
684                     // should happen when preamble specifies month of year / day of month variation
685                     // since there is no month 0 in any year ...
686                     Assertions.assertEquals(preamble & 0x08, 0x00);
687                 }
688 
689             }
690         }
691 
692         // limit to microsecond
693         byte[] timeMicrosecond = new byte[] { 0x07, -0x2E, 0x00, -0x71, 0x0C, 0x22, 0x38, 0x4E, 0x5A, 0x0C };
694         AbsoluteDate ccsds4 =
695             AbsoluteDate.parseCCSDSCalendarSegmentedTimeCode((byte) 0x5B, timeMicrosecond);
696         Assertions.assertEquals(-0.345678e-6, ccsds4.durationFrom(reference), lsb / 2);
697 
698     }
699 
700     @Test
701     void testExpandedConstructors() {
702         Assertions.assertThrows(IllegalArgumentException.class, () -> {
703             Assertions.assertEquals(new AbsoluteDate(new DateComponents(2002, 5, 28),
704                             new TimeComponents(15, 30, 0),
705                             TimeScalesFactory.getUTC()),
706                     new AbsoluteDate(2002, 5, 28, 15, 30, 0, TimeScalesFactory.getUTC()));
707             Assertions.assertEquals(new AbsoluteDate(new DateComponents(2002, 5, 28), TimeComponents.H00,
708                             TimeScalesFactory.getUTC()),
709                     new AbsoluteDate(2002, 5, 28, TimeScalesFactory.getUTC()));
710             new AbsoluteDate(2002, 5, 28, 25, 30, 0, TimeScalesFactory.getUTC());
711         });
712     }
713 
714     @Test
715     void testHashcode() {
716         AbsoluteDate d1 =
717             new AbsoluteDate(new DateComponents(2006, 2, 25),
718                              new TimeComponents(17, 10, 34),
719                              utc);
720         AbsoluteDate d2 = new AbsoluteDate(new DateComponents(2006, 2, 25),
721                                            new TimeComponents(17, 10, 0),
722                                            utc).shiftedBy(34);
723         Assertions.assertEquals(d1.hashCode(), d2.hashCode());
724         Assertions.assertTrue(d1.hashCode() != d1.shiftedBy(1.0e-3).hashCode());
725     }
726 
727     @Test
728     void testInfinity() {
729         Assertions.assertTrue(AbsoluteDate.JULIAN_EPOCH.compareTo(AbsoluteDate.PAST_INFINITY) > 0);
730         Assertions.assertTrue(AbsoluteDate.JULIAN_EPOCH.compareTo(AbsoluteDate.FUTURE_INFINITY) < 0);
731         Assertions.assertTrue(AbsoluteDate.J2000_EPOCH.compareTo(AbsoluteDate.PAST_INFINITY) > 0);
732         Assertions.assertTrue(AbsoluteDate.J2000_EPOCH.compareTo(AbsoluteDate.FUTURE_INFINITY) < 0);
733         Assertions.assertEquals(0, AbsoluteDate.PAST_INFINITY.compareTo(AbsoluteDate.PAST_INFINITY));
734         Assertions.assertTrue(AbsoluteDate.PAST_INFINITY.compareTo(AbsoluteDate.JULIAN_EPOCH) < 0);
735         Assertions.assertTrue(AbsoluteDate.PAST_INFINITY.compareTo(AbsoluteDate.J2000_EPOCH) < 0);
736         Assertions.assertTrue(AbsoluteDate.PAST_INFINITY.compareTo(AbsoluteDate.FUTURE_INFINITY) < 0);
737         Assertions.assertTrue(AbsoluteDate.FUTURE_INFINITY.compareTo(AbsoluteDate.JULIAN_EPOCH) > 0);
738         Assertions.assertTrue(AbsoluteDate.FUTURE_INFINITY.compareTo(AbsoluteDate.J2000_EPOCH) > 0);
739         Assertions.assertTrue(AbsoluteDate.FUTURE_INFINITY.compareTo(AbsoluteDate.PAST_INFINITY) > 0);
740         Assertions.assertEquals(0, AbsoluteDate.FUTURE_INFINITY.compareTo(AbsoluteDate.FUTURE_INFINITY));
741         Assertions.assertTrue(Double.isInfinite(AbsoluteDate.FUTURE_INFINITY.durationFrom(AbsoluteDate.J2000_EPOCH)));
742         Assertions.assertTrue(Double.isInfinite(AbsoluteDate.FUTURE_INFINITY.durationFrom(AbsoluteDate.PAST_INFINITY)));
743         Assertions.assertTrue(Double.isInfinite(AbsoluteDate.PAST_INFINITY.durationFrom(AbsoluteDate.J2000_EPOCH)));
744         Assertions.assertTrue(Double.isNaN(AbsoluteDate.FUTURE_INFINITY.durationFrom(AbsoluteDate.FUTURE_INFINITY)));
745         Assertions.assertTrue(Double.isNaN(AbsoluteDate.PAST_INFINITY.durationFrom(AbsoluteDate.PAST_INFINITY)));
746         Assertions.assertEquals("5881610-07-11T23:59:59.999Z",  AbsoluteDate.FUTURE_INFINITY.toString());
747         Assertions.assertEquals("-5877490-03-03T00:00:00.000Z", AbsoluteDate.PAST_INFINITY.toString());
748         Assertions.assertTrue(AbsoluteDate.FUTURE_INFINITY.equals(AbsoluteDate.FUTURE_INFINITY));
749         Assertions.assertTrue(AbsoluteDate.PAST_INFINITY.equals(AbsoluteDate.PAST_INFINITY));
750         Assertions.assertFalse(AbsoluteDate.PAST_INFINITY.equals(AbsoluteDate.FUTURE_INFINITY));
751         Assertions.assertFalse(AbsoluteDate.FUTURE_INFINITY.equals(AbsoluteDate.PAST_INFINITY));
752 
753         Assertions.assertEquals(Double.POSITIVE_INFINITY, AbsoluteDate.J2000_EPOCH.durationFrom(AbsoluteDate.ARBITRARY_EPOCH.shiftedBy(Double.NEGATIVE_INFINITY)));
754         Assertions.assertEquals(Double.NEGATIVE_INFINITY, AbsoluteDate.J2000_EPOCH.durationFrom(AbsoluteDate.ARBITRARY_EPOCH.shiftedBy(Double.POSITIVE_INFINITY)));
755 
756     }
757 
758     @Test
759     void testCompareTo() {
760         // check long time spans
761         AbsoluteDate epoch =
762                 new AbsoluteDate(2000, 1, 1, 12, 0, 0, TimeScalesFactory.getTAI());
763         Assertions.assertTrue(AbsoluteDate.JULIAN_EPOCH.compareTo(epoch) < 0);
764         Assertions.assertTrue(epoch.compareTo(AbsoluteDate.JULIAN_EPOCH) > 0);
765         // check short time spans
766         AbsoluteDate d = epoch;
767         double epsilon = 1.0 - FastMath.nextDown(1.0);
768         Assertions.assertTrue(d.compareTo(d.shiftedBy(epsilon)) < 0);
769         Assertions.assertEquals(0, d.compareTo(d.shiftedBy(0)));
770         Assertions.assertTrue(d.compareTo(d.shiftedBy(-epsilon)) > 0);
771         // check date with negative offset
772         d = epoch.shiftedBy(496891466)
773                 .shiftedBy(0.7320114066633323)
774                 .shiftedBy(-19730.732011406664);
775         // offset is 0 in d1
776         AbsoluteDate d1 = epoch.shiftedBy(496891466 - 19730);
777         Assertions.assertTrue(d.compareTo(d1) < 0);
778         // decrement epoch, now offset is 0.999... in d1
779         d1 = d1.shiftedBy(-1e-16);
780         Assertions.assertTrue(d.compareTo(d1) < 0,"" + d.durationFrom(d1));
781         // check large dates
782         // these tests fail due to long overflow in durationFrom() Bug #584
783         // d = new AbsoluteDate(epoch, Long.MAX_VALUE);
784         // Assertions.assertEquals(-1, epoch.compareTo(d));
785         // Assertions.assertTrue(d.compareTo(AbsoluteDate.FUTURE_INFINITY) < 0);
786         // d = new AbsoluteDate(epoch, Long.MIN_VALUE);
787         // Assertions.assertTrue(epoch.compareTo(d) > 0);
788         // Assertions.assertTrue(d.compareTo(AbsoluteDate.PAST_INFINITY) > 0);
789     }
790 
791     @Test
792     void testIsEqualTo() {
793         Assertions.assertTrue(present.isEqualTo(present));
794         Assertions.assertTrue(present.isEqualTo(presentToo));
795         Assertions.assertFalse(present.isEqualTo(past));
796         Assertions.assertFalse(present.isEqualTo(future));
797     }
798 
799     @Test
800     void testIsCloseTo() {
801         double tolerance = 10;
802         TimeStamped closeToPresent = new AnyTimeStamped(present.shiftedBy(5));
803         Assertions.assertTrue(present.isCloseTo(present, tolerance));
804         Assertions.assertTrue(present.isCloseTo(presentToo, tolerance));
805         Assertions.assertTrue(present.isCloseTo(closeToPresent, tolerance));
806         Assertions.assertFalse(present.isCloseTo(past, tolerance));
807         Assertions.assertFalse(present.isCloseTo(future, tolerance));
808     }
809 
810     @Test
811     void testIsBefore() {
812         Assertions.assertFalse(present.isBefore(past));
813         Assertions.assertFalse(present.isBefore(present));
814         Assertions.assertFalse(present.isBefore(presentToo));
815         Assertions.assertTrue(present.isBefore(future));
816     }
817 
818     @Test
819     void testIsAfter() {
820         Assertions.assertTrue(present.isAfter(past));
821         Assertions.assertFalse(present.isAfter(present));
822         Assertions.assertFalse(present.isAfter(presentToo));
823         Assertions.assertFalse(present.isAfter(future));
824     }
825 
826     @Test
827     void testIsBeforeOrEqualTo() {
828         Assertions.assertFalse(present.isBeforeOrEqualTo(past));
829         Assertions.assertTrue(present.isBeforeOrEqualTo(present));
830         Assertions.assertTrue(present.isBeforeOrEqualTo(presentToo));
831         Assertions.assertTrue(present.isBeforeOrEqualTo(future));
832     }
833 
834     @Test
835     void testIsAfterOrEqualTo() {
836         Assertions.assertTrue(present.isAfterOrEqualTo(past));
837         Assertions.assertTrue(present.isAfterOrEqualTo(present));
838         Assertions.assertTrue(present.isAfterOrEqualTo(presentToo));
839         Assertions.assertFalse(present.isAfterOrEqualTo(future));
840     }
841 
842     @Test
843     void testIsBetween() {
844         Assertions.assertTrue(present.isBetween(past, future));
845         Assertions.assertTrue(present.isBetween(future, past));
846         Assertions.assertFalse(past.getDate().isBetween(present, future));
847         Assertions.assertFalse(past.getDate().isBetween(future, present));
848         Assertions.assertFalse(future.getDate().isBetween(past, present));
849         Assertions.assertFalse(future.getDate().isBetween(present, past));
850         Assertions.assertFalse(present.isBetween(present, future));
851         Assertions.assertFalse(present.isBetween(past, present));
852         Assertions.assertFalse(present.isBetween(past, past));
853         Assertions.assertFalse(present.isBetween(present, present));
854         Assertions.assertFalse(present.isBetween(present, presentToo));
855     }
856 
857     @Test
858     void testIsBetweenOrEqualTo() {
859         Assertions.assertTrue(present.isBetweenOrEqualTo(past, future));
860         Assertions.assertTrue(present.isBetweenOrEqualTo(future, past));
861         Assertions.assertFalse(past.getDate().isBetweenOrEqualTo(present, future));
862         Assertions.assertFalse(past.getDate().isBetweenOrEqualTo(future, present));
863         Assertions.assertFalse(future.getDate().isBetweenOrEqualTo(past, present));
864         Assertions.assertFalse(future.getDate().isBetweenOrEqualTo(present, past));
865         Assertions.assertTrue(present.isBetweenOrEqualTo(present, future));
866         Assertions.assertTrue(present.isBetweenOrEqualTo(past, present));
867         Assertions.assertFalse(present.isBetweenOrEqualTo(past, past));
868         Assertions.assertTrue(present.isBetweenOrEqualTo(present, present));
869         Assertions.assertTrue(present.isBetweenOrEqualTo(present, presentToo));
870     }
871 
872     @Test
873     void testAccuracy() {
874         TimeScale tai = TimeScalesFactory.getTAI();
875         double sec = 0.281;
876         AbsoluteDate t = new AbsoluteDate(2010, 6, 21, 18, 42, sec, tai);
877         double recomputedSec = t.getComponents(tai).getTime().getSecond();
878         Assertions.assertEquals(sec, recomputedSec, FastMath.ulp(sec));
879     }
880 
881     @Test
882     void testShiftPastInfinity() {
883         AbsoluteDate shifted = AbsoluteDate.PAST_INFINITY.shiftedBy(Constants.JULIAN_DAY);
884         Assertions.assertEquals(AbsoluteDate.PAST_INFINITY.getSeconds(), shifted.getSeconds());
885         Assertions.assertEquals(AbsoluteDate.PAST_INFINITY.getAttoSeconds(), shifted.getAttoSeconds());
886     }
887 
888     @Test
889     void testShiftFutureInfinity() {
890         AbsoluteDate shifted = AbsoluteDate.FUTURE_INFINITY.shiftedBy(Constants.JULIAN_DAY);
891         Assertions.assertEquals(AbsoluteDate.FUTURE_INFINITY.getSeconds(), shifted.getSeconds());
892         Assertions.assertEquals(AbsoluteDate.FUTURE_INFINITY.getAttoSeconds(), shifted.getAttoSeconds());
893     }
894 
895     @Test
896     void testSubFemtoSecondPositiveShift() {
897         TimeScale tai = TimeScalesFactory.getTAI();
898         AbsoluteDate since = new AbsoluteDate(2008, 4, 7, 0, 53, 0.0078125, tai);
899         double deltaT = 1.0e-17;
900         AbsoluteDate shifted = since.shiftedBy(deltaT);
901         Assertions.assertEquals(deltaT, shifted.durationFrom(since), 1.0e-25);
902     }
903 
904     @Test
905     void testSubFemtoSecondNegativeShift() {
906         TimeScale tai = TimeScalesFactory.getTAI();
907         AbsoluteDate since = new AbsoluteDate(2008, 4, 7, 0, 53, 0.0078125, tai);
908         double deltaT = -1.0e-17;
909         AbsoluteDate shifted = since.shiftedBy(deltaT);
910         Assertions.assertEquals(deltaT, shifted.durationFrom(since), 1.0e-25);
911     }
912 
913     @Test
914     void testIterationAccuracy() {
915 
916         final TimeScale tai = TimeScalesFactory.getTAI();
917         final AbsoluteDate t0 = new AbsoluteDate(2010, 6, 21, 18, 42, 0.281, tai);
918 
919         // 0.1 is not representable exactly in double precision
920         // we will accumulate error, between -0.5ULP and -3ULP at each iteration
921         checkIteration(0.1, t0, 10000, 3.0, -0.3874, 1.0e-4);
922 
923         // 0.125 is representable exactly in double precision
924         // error will be null
925         checkIteration(0.125, t0, 10000, 1.0e-15, 0.0, 1.0e-15);
926 
927     }
928 
929     private void checkIteration(final double step, final AbsoluteDate t0, final int nMax,
930                                 final double maxErrorFactor,
931                                 final double expectedMean, final double meanTolerance) {
932         final double epsilon = FastMath.ulp(step);
933         AbsoluteDate iteratedDate = t0;
934         double mean = 0;
935         for (int i = 1; i < nMax; ++i) {
936             iteratedDate = iteratedDate.shiftedBy(step);
937             AbsoluteDate directDate = t0.shiftedBy(i * step);
938             final double error = iteratedDate.durationFrom(directDate);
939             mean += error / (i * epsilon);
940             Assertions.assertEquals(0.0, iteratedDate.durationFrom(directDate), maxErrorFactor * i * epsilon);
941         }
942         mean /= nMax;
943         Assertions.assertEquals(expectedMean, mean, meanTolerance);
944     }
945 
946     @Test
947     void testIssue142() {
948 
949         final AbsoluteDate epoch = AbsoluteDate.JAVA_EPOCH;
950         final TimeScale utc = TimeScalesFactory.getUTC();
951 
952         Assertions.assertEquals("1970-01-01T00:00:00.000", epoch.toString(utc));
953         Assertions.assertEquals(0.0, epoch.durationFrom(new AbsoluteDate(1970, 1, 1, utc)), 1.0e-15);
954         Assertions.assertEquals(8.000082,
955                             epoch.durationFrom(new AbsoluteDate(DateComponents.JAVA_EPOCH, TimeScalesFactory.getTAI())),
956                             1.0e-15);
957 
958         // April 1, 2006, in UTC
959         final TimeOffset offset = new TimeOffset(1143849600L, 0L);
960         final AbsoluteDate ad = new AbsoluteDate(epoch, offset, TimeScalesFactory.getUTC());
961         Assertions.assertEquals("2006-04-01T00:00:00.000", ad.toString(utc));
962 
963     }
964 
965     @Test
966     void testIssue148() {
967         final TimeScale utc = TimeScalesFactory.getUTC();
968         AbsoluteDate t0 = new AbsoluteDate(2012, 6, 30, 23, 59, 50.0, utc);
969         DateTimeComponents components = t0.shiftedBy(11.0 - 200 * Precision.EPSILON).getComponents(utc);
970         Assertions.assertEquals(2012, components.getDate().getYear());
971         Assertions.assertEquals(   6, components.getDate().getMonth());
972         Assertions.assertEquals(  30, components.getDate().getDay());
973         Assertions.assertEquals(  23, components.getTime().getHour());
974         Assertions.assertEquals(  59, components.getTime().getMinute());
975         Assertions.assertEquals(  61 - 200 * Precision.EPSILON,
976                             components.getTime().getSecond(), 1.0e-15);
977     }
978 
979     @Test
980     void testIssue149() {
981         final TimeScale utc = TimeScalesFactory.getUTC();
982         AbsoluteDate t0 = new AbsoluteDate(2012, 6, 30, 23, 59, 59, utc);
983         DateTimeComponents components = t0.shiftedBy(1.0 - Precision.EPSILON).getComponents(utc);
984         Assertions.assertEquals(2012, components.getDate().getYear());
985         Assertions.assertEquals(   6, components.getDate().getMonth());
986         Assertions.assertEquals(  30, components.getDate().getDay());
987         Assertions.assertEquals(  23, components.getTime().getHour());
988         Assertions.assertEquals(  59, components.getTime().getMinute());
989         Assertions.assertEquals(  60 - Precision.EPSILON,  // misleading as 60.0 - eps = 60.0
990                             components.getTime().getSecond(), 1.0e-15);
991     }
992 
993     @Test
994     void testWrapAtMinuteEnd() {
995         TimeScale tai = TimeScalesFactory.getTAI();
996         TimeScale utc = TimeScalesFactory.getUTC();
997         AbsoluteDate date0 = new AbsoluteDate(DateComponents.J2000_EPOCH, TimeComponents.H12, tai);
998         AbsoluteDate ref = date0.shiftedBy(new TimeOffset(496891466L, 732011406663332300L));
999         AbsoluteDate date = ref.shiftedBy(new TimeOffset(597L, 900970042626200000L).negate().multiply(33));
1000         DateTimeComponents dtc = date.getComponents(utc);
1001         Assertions.assertEquals(                2015,  dtc.getDate().getYear());
1002         Assertions.assertEquals(                   9,  dtc.getDate().getMonth());
1003         Assertions.assertEquals(                  30,  dtc.getDate().getDay());
1004         Assertions.assertEquals(                   7,  dtc.getTime().getHour());
1005         Assertions.assertEquals(                  54,  dtc.getTime().getMinute());
1006         Assertions.assertEquals(                  59L, dtc.getTime().getSplitSecond().getSeconds());
1007         Assertions.assertEquals(  999999999998732300L, dtc.getTime().getSplitSecond().getAttoSeconds());
1008         Assertions.assertEquals("2015-09-30T07:54:59.9999999999987323", date.toString(utc));
1009         AbsoluteDate beforeMidnight = new AbsoluteDate(2008, 2, 29, 23, 59, new TimeOffset(59L, 999400000000000000L), utc);
1010         AbsoluteDate stillBeforeMidnight = beforeMidnight.shiftedBy(new TimeOffset(0L, 200000000000000L));
1011         Assertions.assertEquals(59.9994, beforeMidnight.getComponents(utc).getTime().getSecond(), 1.0e-15);
1012         Assertions.assertEquals(59.9996, stillBeforeMidnight.getComponents(utc).getTime().getSecond(), 1.0e-15);
1013         Assertions.assertEquals("2008-02-29T23:59:59.9994", beforeMidnight.toString(utc));
1014         Assertions.assertEquals("2008-02-29T23:59:59.9996", stillBeforeMidnight.toString(utc));
1015     }
1016 
1017 
1018     @Test
1019     void testLastLeapOutput() {
1020         UTCScale utc = TimeScalesFactory.getUTC();
1021         AbsoluteDate t = utc.getLastKnownLeapSecond();
1022         Assertions.assertEquals("23:59:59.500", t.shiftedBy(-0.5).toString(utc).substring(11));
1023         Assertions.assertEquals("23:59:60.000", t.shiftedBy( 0.0).toString(utc).substring(11));
1024         Assertions.assertEquals("23:59:60.500", t.shiftedBy(+0.5).toString(utc).substring(11));
1025     }
1026 
1027     @Test
1028     void testWrapBeforeLeap() {
1029         UTCScale utc = TimeScalesFactory.getUTC();
1030         AbsoluteDate t = new AbsoluteDate("2015-06-30T23:59:59.999999", utc);
1031         Assertions.assertEquals(2015,        t.getComponents(utc).getDate().getYear());
1032         Assertions.assertEquals(   6,        t.getComponents(utc).getDate().getMonth());
1033         Assertions.assertEquals(  30,        t.getComponents(utc).getDate().getDay());
1034         Assertions.assertEquals(  23,        t.getComponents(utc).getTime().getHour());
1035         Assertions.assertEquals(  59,        t.getComponents(utc).getTime().getMinute());
1036         Assertions.assertEquals(  59.999999, t.getComponents(utc).getTime().getSecond(), 1.0e-6);
1037         Assertions.assertEquals("2015-06-30T23:59:59.999999", t.toStringWithoutUtcOffset(utc, 6));
1038         Assertions.assertEquals("2015-07-01T02:59:59.999999", t.toStringWithoutUtcOffset(TimeScalesFactory.getGLONASS(), 6));
1039     }
1040 
1041     @Test
1042     void testMjdInLeap() {
1043         // inside a leap second
1044         AbsoluteDate date1 = new AbsoluteDate(2008, 12, 31, 23, 59, 60.5, utc);
1045 
1046         // check date to MJD conversion
1047         DateTimeComponents date1Components = date1.getComponents(utc);
1048         int mjd = date1Components.getDate().getMJD();
1049         double seconds = date1Components.getTime().getSecondsInUTCDay();
1050         Assertions.assertEquals(54831, mjd);
1051         Assertions.assertEquals(86400.5, seconds, 0);
1052 
1053         // check MJD to date conversion
1054         AbsoluteDate date2 = AbsoluteDate.createMJDDate(mjd, seconds, utc);
1055         Assertions.assertEquals(date1, date2);
1056 
1057         // check we still detect seconds overflow
1058         try {
1059             AbsoluteDate.createMJDDate(mjd, seconds + 1.0, utc);
1060             Assertions.fail("an exception should have been thrown");
1061         } catch (OrekitIllegalArgumentException oiae) {
1062             Assertions.assertEquals(OrekitMessages.OUT_OF_RANGE_SECONDS_NUMBER_DETAIL, oiae.getSpecifier());
1063             Assertions.assertEquals(86401.5, (Double) oiae.getParts()[0], 0);
1064             Assertions.assertEquals(0, ((Number) oiae.getParts()[1]).doubleValue(), 0);
1065             Assertions.assertEquals(86401, ((Number) oiae.getParts()[2]).doubleValue(), 0);
1066         }
1067 
1068     }
1069 
1070     @Test
1071     void testIssueTimesStampAccuracy() {
1072         String testString = "2019-02-01T13:06:03.115";
1073         TimeScale timeScale=TimeScalesFactory.getUTC();
1074 
1075         DateTimeComponents expectedComponent = DateTimeComponents.parseDateTime(testString);
1076         AbsoluteDate expectedDate = new AbsoluteDate(expectedComponent, timeScale);
1077 
1078         ZonedDateTime actualComponent = LocalDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(testString)).atZone(ZoneOffset.UTC);
1079         AbsoluteDate actualDate = new AbsoluteDate(Timestamp.from(actualComponent.toInstant()), timeScale);
1080         Assertions.assertEquals(0.0, expectedDate.durationFrom(actualDate), 1.0e-15);
1081 
1082     }
1083 
1084     @Test
1085     void testGetComponentsIssue681and676and694() {
1086         // setup
1087         AbsoluteDate date = new AbsoluteDate(2009, 1, 1, utc);
1088         double attoSecond = 1.0e-18;
1089         double zeroUlp = FastMath.nextUp(0.0);
1090         double oneUlp = FastMath.ulp(1.0);
1091         double sixtyUlp = FastMath.ulp(60.0);
1092         double one = FastMath.nextDown(1.0);
1093         double sixty = FastMath.nextDown(60.0);
1094         double sixtyOne = FastMath.nextDown(61.0);
1095 
1096         // actions + verify
1097         // translate back to AbsoluteDate has up to half an ULP of error,
1098         // except when truncated when the error can be up to 1 ULP.
1099         check(date, 2009, 1, 1, 0, 0, 0, 1, 0, 0);
1100         check(date.shiftedBy(attoSecond), 2009, 1, 1, 0, 0, attoSecond, 0.5, 0, 0);
1101         check(date.shiftedBy(one), 2009, 1, 1, 0, 0, one, 0.5, 0, 0);
1102         // I could also see rounding to a valid time as being reasonable here
1103         check(date.shiftedBy(59).shiftedBy(one), 2009, 1, 1, 0, 0, sixty, 1, 1, 0);
1104         check(date.shiftedBy(86399).shiftedBy(one), 2009, 1, 1, 23, 59, sixty, 1, 1, 0);
1105         check(date.shiftedBy(-zeroUlp), 2009, 1, 1, 0, 0, 0, 0.5, 0, 0);
1106         check(date.shiftedBy(-oneUlp), 2008, 12, 31, 23, 59, sixtyOne, 1, 1, 0);
1107         check(date.shiftedBy(-1).shiftedBy(zeroUlp), 2008, 12, 31, 23, 59, 60.0, 0.5, 0, 0);
1108         check(date.shiftedBy(-1).shiftedBy(-zeroUlp), 2008, 12, 31, 23, 59, 60.0, 0.5, 0, 0);
1109         check(date.shiftedBy(-1).shiftedBy(-oneUlp), 2008, 12, 31, 23, 59, 60.0, 0.5, 0, 0);
1110         check(date.shiftedBy(-1).shiftedBy(-sixtyUlp), 2008, 12, 31, 23, 59, sixty, 0.5, 0, 0);
1111         check(date.shiftedBy(-61).shiftedBy(attoSecond), 2008, 12, 31, 23, 59, attoSecond, 0.5, 0, 0);
1112 
1113         // check UTC weirdness.
1114         // These have more error because of additional multiplications and additions
1115         // up to 2 ULPs or ulp(60.0) of error.
1116         AbsoluteDate d = new AbsoluteDate(1966, 1, 1, utc);
1117         double ratePost = 0.0025920 / Constants.JULIAN_DAY;
1118         double factorPost = ratePost / (1 + ratePost);
1119         double ratePre = 0.0012960 / Constants.JULIAN_DAY;
1120         double factorPre = ratePre / (1 + ratePre);
1121         check(d, 1966, 1, 1, 0, 0, 0, 1, 0, 0);
1122         check(d.shiftedBy(zeroUlp), 1966, 1, 1, 0, 0, 0, 0.5, 0, 0);
1123         check(d.shiftedBy(attoSecond), 1966, 1, 1, 0, 0, attoSecond, 0.5, 0, 0);
1124         check(d.shiftedBy(one), 1966, 1, 1, 0, 0, one * (1 - factorPost), 1, 3, 0);
1125         check(d.shiftedBy(59).shiftedBy(one), 1966, 1, 1, 0, 0, sixty * (1 - factorPost), 1, 2, 0);
1126         check(d.shiftedBy(86399).shiftedBy(one), 1966, 1, 1, 23, 59, sixty - 86400 * factorPost, 1, 1, 0);
1127         check(d.shiftedBy(-zeroUlp), 1966, 1, 1, 0, 0, 0, 0.5, 0, 0);
1128         // actual leap is small ~1e-16, but during a leap rounding up to 60.0 is ok
1129         check(d.shiftedBy(-oneUlp), 1965, 12, 31, 23, 59, 60.0, 1, 0, 0);
1130         check(d.shiftedBy(-1).shiftedBy(zeroUlp), 1965, 12, 31, 23, 59, 59 + factorPre, 0.5, 0, 0);
1131         check(d.shiftedBy(-1).shiftedBy(-zeroUlp), 1965, 12, 31, 23, 59, 59 + factorPre, 0.5, 0, 0);
1132         check(d.shiftedBy(-1).shiftedBy(-oneUlp), 1965, 12, 31, 23, 59, 59 + factorPre, 0.5, 0, 0);
1133         check(d.shiftedBy(-1).shiftedBy(-sixtyUlp), 1965, 12, 31, 23, 59, 59 + (1 + sixtyUlp) * factorPre, 0.5, 1, 0);
1134         // since second ~= 0 there is significant cancellation
1135         check(d.shiftedBy(-60).shiftedBy(zeroUlp), 1965, 12, 31, 23, 59, 60 * factorPre, 0, 0, sixtyUlp);
1136         check(d.shiftedBy(-60).shiftedBy(oneUlp), 1965, 12, 31, 23, 59, (oneUlp - oneUlp * factorPre) + 60 * factorPre, 0.5, 0, sixtyUlp);
1137 
1138         // check first whole second leap
1139         AbsoluteDate d2 = new AbsoluteDate(1972, 7, 1, utc);
1140         check(d2, 1972, 7, 1, 0, 0, 0, 1, 0, 0);
1141         check(d2.shiftedBy(attoSecond), 1972, 7, 1, 0, 0, attoSecond, 0.5, 0, 0);
1142         check(d2.shiftedBy(one), 1972, 7, 1, 0, 0, one, 0.5, 0, 0);
1143         check(d2.shiftedBy(59).shiftedBy(one), 1972, 7, 1, 0, 0, sixty, 1, 1, 0);
1144         check(d2.shiftedBy(86399).shiftedBy(one), 1972, 7, 1, 23, 59, sixty, 1, 1, 0);
1145         check(d2.shiftedBy(-zeroUlp), 1972, 7, 1, 0, 0, 0, 0.5, 0, 0);
1146         check(d2.shiftedBy(-oneUlp), 1972, 6, 30, 23, 59, sixtyOne, 1, 1, 0);
1147         check(d2.shiftedBy(-1).shiftedBy(zeroUlp), 1972, 6, 30, 23, 59, 60.0, 0.5, 0, 0);
1148         check(d2.shiftedBy(-1).shiftedBy(-zeroUlp), 1972, 6, 30, 23, 59, 60.0, 0.5, 0, 0);
1149         check(d2.shiftedBy(-1).shiftedBy(-oneUlp), 1972, 6, 30, 23, 59, 60.0, 0.5, 0, 0);
1150         check(d2.shiftedBy(-1).shiftedBy(-sixtyUlp), 1972, 6, 30, 23, 59, sixty, 0.5, 0, 0);
1151         check(d2.shiftedBy(-61).shiftedBy(attoSecond), 1972, 6, 30, 23, 59, attoSecond, 0.5, 0, 0);
1152 
1153         // check first leap second, which was actually 1.422818 s.
1154         AbsoluteDate d3 = AbsoluteDate.ARBITRARY_EPOCH.shiftedBy(-1230724800);
1155         check(d3, 1960, 12, 31, 23, 59, 60, 0.5, 0, 0);
1156         AbsoluteDate d4 = new AbsoluteDate(1961, 1, 1, utc);
1157         check(d4, 1961, 1, 1, 0, 0, 0, 0.5, 0, 0);
1158         // FIXME something wrong because a date a smidgen before 1961-01-01 is not in a leap second
1159         //check(d4.shiftedBy(-oneUlp), 1960, 12, 31, 23, 59, 61.422818, 0.5, 0, 0);
1160 
1161         // check NaN, this is weird that NaNs have valid ymdhm, but not second.
1162         DateTimeComponents actual = date.shiftedBy(Double.NaN).getComponents(utc);
1163         DateComponents dc = actual.getDate();
1164         TimeComponents tc = actual.getTime();
1165         MatcherAssert.assertThat(dc.getYear(), CoreMatchers.is(2000));
1166         MatcherAssert.assertThat(dc.getMonth(), CoreMatchers.is(1));
1167         MatcherAssert.assertThat(dc.getDay(), CoreMatchers.is(1));
1168         MatcherAssert.assertThat(tc.getHour(), CoreMatchers.is(0));
1169         MatcherAssert.assertThat(tc.getMinute(), CoreMatchers.is(0));
1170         MatcherAssert.assertThat("second", tc.getSecond(), CoreMatchers.is(Double.NaN));
1171         MatcherAssert.assertThat(tc.getMinutesFromUTC(), CoreMatchers.is(0));
1172         final double difference = new AbsoluteDate(actual, utc).durationFrom(date);
1173         MatcherAssert.assertThat(difference, CoreMatchers.is(Double.NaN));
1174     }
1175 
1176     private void check(AbsoluteDate date,
1177                        int year, int month, int day, int hour, int minute, double second,
1178                        double roundTripUlps, final int secondUlps, final double absTol) {
1179         DateTimeComponents actual = date.getComponents(utc);
1180         DateComponents d = actual.getDate();
1181         TimeComponents t = actual.getTime();
1182         MatcherAssert.assertThat(d.getYear(), CoreMatchers.is(year));
1183         MatcherAssert.assertThat(d.getMonth(), CoreMatchers.is(month));
1184         MatcherAssert.assertThat(d.getDay(), CoreMatchers.is(day));
1185         MatcherAssert.assertThat(t.getHour(), CoreMatchers.is(hour));
1186         MatcherAssert.assertThat(t.getMinute(), CoreMatchers.is(minute));
1187         MatcherAssert.assertThat("second", t.getSecond(),
1188                 OrekitMatchers.numberCloseTo(second, absTol, secondUlps));
1189         MatcherAssert.assertThat(t.getMinutesFromUTC(), CoreMatchers.is(0));
1190         final double tol = FastMath.ulp(second) * roundTripUlps;
1191         final double difference = new AbsoluteDate(actual, utc).durationFrom(date);
1192         MatcherAssert.assertThat(difference,
1193                 OrekitMatchers.closeTo(0, FastMath.max(absTol, tol)));
1194     }
1195 
1196     /** Check {@link AbsoluteDate#toStringRfc3339(TimeScale)}. */
1197     @Test
1198     void testToStringRfc3339() {
1199         // setup
1200         AbsoluteDate date = new AbsoluteDate(2009, 1, 1, utc);
1201         double one = FastMath.nextDown(1.0);
1202         double zeroUlp = FastMath.nextUp(0.0);
1203         double oneUlp = FastMath.ulp(1.0);
1204         //double sixty = FastMath.nextDown(60.0);
1205         double sixtyUlp = FastMath.ulp(60.0);
1206 
1207         // action
1208         // test midnight
1209         check(date, "2009-01-01T00:00:00Z");
1210         check(date.shiftedBy(1), "2009-01-01T00:00:01Z");
1211         // test digits and rounding
1212         check(date.shiftedBy(new TimeOffset(12L, 345678912345678900L)), "2009-01-01T00:00:12.3456789123456789Z");
1213         check(date.shiftedBy(new TimeOffset(0L, 12345678912345678L)), "2009-01-01T00:00:00.012345678912345678Z");
1214         // test min and max values
1215         check(date.shiftedBy(zeroUlp), "2009-01-01T00:00:00Z");
1216         check(date.shiftedBy(59.0).shiftedBy(one), "2009-01-01T00:00:59.999999999999999889Z");
1217         check(date.shiftedBy(86399).shiftedBy(one), "2009-01-01T23:59:59.999999999999999889Z");
1218         check(date.shiftedBy(oneUlp), "2009-01-01T00:00:00.000000000000000222Z");
1219         check(date.shiftedBy(one), "2009-01-01T00:00:00.999999999999999889Z");
1220         check(date.shiftedBy(-zeroUlp), "2009-01-01T00:00:00Z");
1221         // test leap
1222         check(date.shiftedBy(-oneUlp), "2008-12-31T23:59:60.999999999999999778Z");
1223         check(date.shiftedBy(-1).shiftedBy(one), "2008-12-31T23:59:60.999999999999999889Z");
1224         check(date.shiftedBy(-0.5), "2008-12-31T23:59:60.5Z");
1225         check(date.shiftedBy(-1).shiftedBy(zeroUlp), "2008-12-31T23:59:60Z");
1226         check(date.shiftedBy(-1), "2008-12-31T23:59:60Z");
1227         check(date.shiftedBy(-1).shiftedBy(-zeroUlp), "2008-12-31T23:59:60Z");
1228         check(date.shiftedBy(-1).shiftedBy(-oneUlp), "2008-12-31T23:59:59.999999999999999778Z");
1229         check(date.shiftedBy(-2), "2008-12-31T23:59:59Z");
1230         check(date.shiftedBy(-1).shiftedBy(-sixtyUlp), "2008-12-31T23:59:59.999999999999992895Z");
1231         check(date.shiftedBy(-61).shiftedBy(zeroUlp), "2008-12-31T23:59:00Z");
1232         check(date.shiftedBy(-61).shiftedBy(oneUlp), "2008-12-31T23:59:00.000000000000000222Z");
1233         // test UTC weirdness
1234         AbsoluteDate d = new AbsoluteDate(1966, 1, 1, utc);
1235         check(d, "1966-01-01T00:00:00Z");
1236         check(d.shiftedBy(zeroUlp), "1966-01-01T00:00:00Z");
1237         check(d.shiftedBy(oneUlp), "1966-01-01T00:00:00.000000000000000222Z");
1238         // as we are after the 1966 leap, slope is 30ns/s
1239         // decimals should therefore be (1 - 2⁻⁵³) ⨉ 10⁹ / (10⁹ + 30) ≈ 0.9999999700000007889776…
1240         // Orekit 13.0 is accurate to attosecond
1241         check(d.shiftedBy(one), "1966-01-01T00:00:00.999999970000000789Z");
1242         // as we are after the 1966 leap, slope is 30ns/s
1243         // decimals should therefore be [59 + (1 - 2⁻⁵³)] ⨉ 10⁹ / (10⁹ + 30) ≈ 0.9999982000000538889760…
1244         // Orekit 13.0 is accurate to attosecond
1245         check(d.shiftedBy(59).shiftedBy(one), "1966-01-01T00:00:59.999998200000053889Z");
1246         // as we are after the 1966 leap, slope is 30ns/s
1247         // decimals should therefore be [86399 + (1 - 2⁻⁵³)] ⨉ 10⁹ / (10⁹ + 30) ≈ 0.9974080000777598866449…
1248         // Orekit 13.0 is accurate to attosecond
1249         check(d.shiftedBy(86399).shiftedBy(one), "1966-01-01T23:59:59.997408000077759887Z");
1250         check(d.shiftedBy(-zeroUlp), "1966-01-01T00:00:00Z");
1251         // actual leap is small ~1e-16, Orekit 13.0 get it
1252         check(d.shiftedBy(-oneUlp), "1965-12-31T23:59:59.999999999999999779Z");
1253         // as we are before the 1966 leap, slope is 15ns/s
1254         // decimals should therefore be 15 / (10⁹ + 15) ≈ 0.000000014999999775000003375…
1255         // Orekit 13.0 is accurate to attosecond
1256         check(d.shiftedBy(-1).shiftedBy(zeroUlp), "1965-12-31T23:59:59.000000014999999776Z");
1257         check(d.shiftedBy(-1).shiftedBy(-zeroUlp), "1965-12-31T23:59:59.000000014999999776Z");
1258         // we subtract ulp(1) = 2⁻⁵² ≈ 222 as
1259         // Orekit 13.0 is accurate to attosecond
1260         check(d.shiftedBy(-1).shiftedBy(-oneUlp), "1965-12-31T23:59:59.000000014999999554Z");
1261         // we subtract ulp(60) = 2⁻⁴⁷ ≈ 7105 as
1262         // Orekit 13.0 is accurate to attosecond
1263         check(d.shiftedBy(-1).shiftedBy(-sixtyUlp), "1965-12-31T23:59:59.000000014999992671Z");
1264         // since second ~= 0 there is significant cancellation
1265         // 60 ⨉ 15 / (10⁹ + 15) ≈ 0.0000008999999865000002025
1266         check(d.shiftedBy(-60).shiftedBy(zeroUlp), "1965-12-31T23:59:00.000000899999986501Z");
1267         check(d.shiftedBy(-60).shiftedBy(oneUlp), "1965-12-31T23:59:00.000000899999986723Z");
1268 
1269         // check first leap second, which was actually 1.422818 s.
1270         check(new AbsoluteDate(1961, 1, 1, utc), "1961-01-01T00:00:00Z");
1271         AbsoluteDate d3 = AbsoluteDate.ARBITRARY_EPOCH.shiftedBy(-1230724800);
1272         check(d3, "1960-12-31T23:59:60Z");
1273         // FIXME something wrong because a date a smidgen before 1961-01-01 is not in a leap second
1274         //check(d3.shiftedBy(FastMath.nextDown(1.422818)), "1960-12-31T23:59:61.422818Z");
1275 
1276         // test proleptic
1277         check(new AbsoluteDate(123, 4, 5, 6, 7, new TimeOffset(8, TimeOffset.SECOND, 900, TimeOffset.MILLISECOND), utc),
1278               "0123-04-05T06:07:08.9Z");
1279 
1280         // there is no way to produce valid RFC3339 for these cases
1281         // I would rather print something useful than throw an exception
1282         // so these cases don't check for a correct answer, just an informative one
1283         check(new AbsoluteDate(-123, 4, 5, 6, 7, new TimeOffset(8, TimeOffset.SECOND, 900, TimeOffset.MILLISECOND), utc),
1284               "-123-04-05T06:07:08.9Z");
1285         check(new AbsoluteDate(-1230, 4, 5, 6, 7, new TimeOffset(8, TimeOffset.SECOND, 900, TimeOffset.MILLISECOND), utc),
1286               "-1230-04-05T06:07:08.9Z");
1287         // test far future
1288         check(new AbsoluteDate(12300, 4, 5, 6, 7, new TimeOffset(8, TimeOffset.SECOND, 900, TimeOffset.MILLISECOND), utc),
1289               "12300-04-05T06:07:08.9Z");
1290         // test infinity
1291         check(AbsoluteDate.FUTURE_INFINITY, "5881610-07-11T23:59:59.999Z");
1292         check(AbsoluteDate.PAST_INFINITY, "-5877490-03-03T00:00:00Z");
1293         // test NaN
1294         check(date.shiftedBy(Double.NaN), "2000-01-01T00:00:NaNZ");
1295     }
1296 
1297     private void check(final AbsoluteDate d, final String s) {
1298         MatcherAssert.assertThat(d.toStringRfc3339(utc),
1299                 CoreMatchers.is(s));
1300         MatcherAssert.assertThat(d.getComponents(utc).toStringRfc3339(),
1301                 CoreMatchers.is(s));
1302     }
1303 
1304 
1305     /** Check {@link AbsoluteDate#toString()}. */
1306     @Test
1307     void testToString() {
1308         // setup
1309         AbsoluteDate date = new AbsoluteDate(2009, 1, 1, utc);
1310         double one = FastMath.nextDown(1.0);
1311         double zeroUlp = FastMath.nextUp(0.0);
1312         double oneUlp = FastMath.ulp(1.0);
1313         //double sixty = FastMath.nextDown(60.0);
1314         double sixtyUlp = FastMath.ulp(60.0);
1315 
1316         // action
1317         // test midnight
1318         checkToString(date, "2009-01-01T00:00:00.000");
1319         checkToString(date.shiftedBy(1), "2009-01-01T00:00:01.000");
1320         // test digits and rounding
1321         checkToString(date.shiftedBy(new TimeOffset(12L, 345678912345678900L)), "2009-01-01T00:00:12.3456789123456789");
1322         checkToString(date.shiftedBy(new TimeOffset(0L, 12345678912345678L)), "2009-01-01T00:00:00.012345678912345678");
1323         // test min and max values
1324         checkToString(date.shiftedBy(zeroUlp), "2009-01-01T00:00:00.000");
1325         // Orekit 13.0 is accurate to attosecond
1326         checkToString(date.shiftedBy(59.0).shiftedBy(one), "2009-01-01T00:00:59.999999999999999889");
1327         // Orekit 13.0 is accurate to attosecond
1328         checkToString(date.shiftedBy(86399).shiftedBy(one), "2009-01-01T23:59:59.999999999999999889");
1329         // Orekit 13.0 is accurate to attosecond
1330         checkToString(date.shiftedBy(oneUlp), "2009-01-01T00:00:00.000000000000000222");
1331         // Orekit 13.0 is accurate to attosecond
1332         checkToString(date.shiftedBy(one), "2009-01-01T00:00:00.999999999999999889");
1333         checkToString(date.shiftedBy(-zeroUlp), "2009-01-01T00:00:00.000");
1334         // test leap
1335         // Orekit 10.1 throw OIAE, 10.2 rounds up, 13.0 is accurate to attosecond
1336         checkToString(date.shiftedBy(-oneUlp), "2008-12-31T23:59:60.999999999999999778");
1337         // Orekit 13.0 is accurate to attosecond
1338         checkToString(date.shiftedBy(-1).shiftedBy(one), "2008-12-31T23:59:60.999999999999999889");
1339         checkToString(date.shiftedBy(-0.5), "2008-12-31T23:59:60.500");
1340         checkToString(date.shiftedBy(-1).shiftedBy(zeroUlp), "2008-12-31T23:59:60.000");
1341         checkToString(date.shiftedBy(-1), "2008-12-31T23:59:60.000");
1342         checkToString(date.shiftedBy(-1).shiftedBy(-zeroUlp), "2008-12-31T23:59:60.000");
1343         // Orekit 13.0 is accurate to attosecond
1344         checkToString(date.shiftedBy(-1).shiftedBy(-oneUlp), "2008-12-31T23:59:59.999999999999999778");
1345         checkToString(date.shiftedBy(-2), "2008-12-31T23:59:59.000");
1346         // Orekit 13.0 is accurate to attosecond
1347         checkToString(date.shiftedBy(-1).shiftedBy(-sixtyUlp), "2008-12-31T23:59:59.999999999999992895");
1348         checkToString(date.shiftedBy(-61).shiftedBy(zeroUlp), "2008-12-31T23:59:00.000");
1349         // Orekit 13.0 is accurate to attosecond
1350         checkToString(date.shiftedBy(-61).shiftedBy(oneUlp), "2008-12-31T23:59:00.000000000000000222");
1351         // test UTC weirdness
1352         AbsoluteDate d = new AbsoluteDate(1966, 1, 1, utc);
1353         checkToString(d, "1966-01-01T00:00:00.000");
1354         checkToString(d.shiftedBy(zeroUlp), "1966-01-01T00:00:00.000");
1355         checkToString(d.shiftedBy(oneUlp), "1966-01-01T00:00:00.000000000000000222");
1356         // as we are after the 1966 leap, slope is 30ns/s
1357         // decimals should therefore be (1 - 2⁻⁵³) ⨉ 10⁹ / (10⁹ + 30) ≈ 0.9999999700000007889776…
1358         // Orekit 13.0 is accurate to attosecond
1359         checkToString(d.shiftedBy(one), "1966-01-01T00:00:00.999999970000000789");
1360         // as we are after the 1966 leap, slope is 30ns/s
1361         // decimals should therefore be [59 + (1 - 2⁻⁵³)] ⨉ 10⁹ / (10⁹ + 30) ≈ 0.9999982000000538889760…
1362         // Orekit 13.0 is accurate to attosecond
1363         checkToString(d.shiftedBy(59).shiftedBy(one), "1966-01-01T00:00:59.999998200000053889");
1364         // as we are after the 1966 leap, slope is 30ns/s
1365         // decimals should therefore be [86399 + (1 - 2⁻⁵³)] ⨉ 10⁹ / (10⁹ + 30) ≈ 0.9974080000777598866449…
1366         // Orekit 13.0 is accurate to attosecond
1367         checkToString(d.shiftedBy(86399).shiftedBy(one), "1966-01-01T23:59:59.997408000077759887");
1368         checkToString(d.shiftedBy(-zeroUlp), "1966-01-01T00:00:00.000");
1369         // actual leap is small ~1e-16, Orekit 13.0 get it
1370         checkToString(d.shiftedBy(-oneUlp), "1965-12-31T23:59:59.999999999999999779");
1371         // as we are before the 1966 leap, slope is 15ns/s
1372         // decimals should therefore be 15 / (10⁹ + 15) ≈ 0.000000014999999775000003375…
1373         // Orekit 13.0 is accurate to attosecond
1374         checkToString(d.shiftedBy(-1).shiftedBy(zeroUlp), "1965-12-31T23:59:59.000000014999999776");
1375         checkToString(d.shiftedBy(-1).shiftedBy(-zeroUlp), "1965-12-31T23:59:59.000000014999999776");
1376         // we subtract ulp(1) = 2⁻⁵² ≈ 222 as
1377         // Orekit 13.0 is accurate to attosecond
1378         checkToString(d.shiftedBy(-1).shiftedBy(-oneUlp), "1965-12-31T23:59:59.000000014999999554");
1379         // we subtract ulp(60) = 2⁻⁴⁷ ≈ 7105 as
1380         // Orekit 13.0 is accurate to attosecond
1381         checkToString(d.shiftedBy(-1).shiftedBy(-sixtyUlp), "1965-12-31T23:59:59.000000014999992671");
1382         // 60 ⨉ 15 / (10⁹ + 15) ≈ 0.0000008999999865000002025
1383         checkToString(d.shiftedBy(-60).shiftedBy(zeroUlp), "1965-12-31T23:59:00.000000899999986501");
1384         checkToString(d.shiftedBy(-60).shiftedBy(oneUlp), "1965-12-31T23:59:00.000000899999986723");
1385 
1386         // check first leap second, which was actually 1.422818 s.
1387         checkToString(new AbsoluteDate(1961, 1, 1, utc), "1961-01-01T00:00:00.000");
1388         AbsoluteDate d3 = AbsoluteDate.ARBITRARY_EPOCH.shiftedBy(-1230724800);
1389         checkToString(d3, "1960-12-31T23:59:60.000");
1390         // FIXME something wrong because a date a smidgen before 1961-01-01 is not in a leap second
1391         //checkToString(d3.shiftedBy(FastMath.nextDown(1.422818)), "1960-12-31T23:59:61.423");
1392 
1393         // test proleptic
1394         checkToString(new AbsoluteDate(123, 4, 5, 6, 7, new TimeOffset(8, TimeOffset.SECOND, 900, TimeOffset.MILLISECOND), utc),
1395                       "0123-04-05T06:07:08.900");
1396 
1397         // there is no way to produce valid RFC3339 for these cases
1398         // I would rather print something useful than throw an exception
1399         // so these cases don't check for a correct answer, just an informative one
1400         checkToString(new AbsoluteDate(-123, 4, 5, 6, 7, new TimeOffset(8, TimeOffset.SECOND, 900, TimeOffset.MILLISECOND), utc),
1401                       "-123-04-05T06:07:08.900");
1402         checkToString(new AbsoluteDate(-1230, 4, 5, 6, 7, new TimeOffset(8, TimeOffset.SECOND, 900, TimeOffset.MILLISECOND), utc),
1403                       "-1230-04-05T06:07:08.900");
1404         // test far future
1405         checkToString(new AbsoluteDate(12300, 4, 5, 6, 7, new TimeOffset(8, TimeOffset.SECOND, 900, TimeOffset.MILLISECOND), utc),
1406                       "12300-04-05T06:07:08.900");
1407         // test infinity
1408         checkToString(AbsoluteDate.FUTURE_INFINITY, "5881610-07-11T23:59:59.999");
1409         checkToString(AbsoluteDate.PAST_INFINITY, "-5877490-03-03T00:00:00.000");
1410         // test NaN
1411         checkToString(date.shiftedBy(Double.NaN), "2000-01-01T00:00:NaN");
1412     }
1413 
1414     private void checkToString(final AbsoluteDate d, final String s) {
1415         MatcherAssert.assertThat(d.toString(), CoreMatchers.is(s + "Z"));
1416         MatcherAssert.assertThat(d.getComponents(utc).toString(), CoreMatchers.is(s + "+00:00"));
1417     }
1418 
1419     @Test
1420     void testToStringWithoutUtcOffset() {
1421         // setup
1422         AbsoluteDate date = new AbsoluteDate(2009, 1, 1, utc);
1423         double one = FastMath.nextDown(1.0);
1424         double zeroUlp = FastMath.nextUp(0.0);
1425         double oneUlp = FastMath.ulp(1.0);
1426         //double sixty = FastMath.nextDown(60.0);
1427         double sixtyUlp = FastMath.ulp(60.0);
1428 
1429         // action
1430         // test midnight
1431         checkToStringNoOffset(date, "2009-01-01T00:00:00.000");
1432         checkToStringNoOffset(date.shiftedBy(1), "2009-01-01T00:00:01.000");
1433         // test digits and rounding
1434         checkToStringNoOffset(date.shiftedBy(12.3456789123456789), "2009-01-01T00:00:12.346");
1435         checkToStringNoOffset(date.shiftedBy(0.0123456789123456789), "2009-01-01T00:00:00.012");
1436         // test min and max values
1437         checkToStringNoOffset(date.shiftedBy(zeroUlp), "2009-01-01T00:00:00.000");
1438         // Orekit 10.1 rounds up
1439         checkToStringNoOffset(date.shiftedBy(59.0).shiftedBy(one), "2009-01-01T00:01:00.000");
1440         // Orekit 10.1 rounds up
1441         checkToStringNoOffset(date.shiftedBy(86399).shiftedBy(one), "2009-01-02T00:00:00.000");
1442         checkToStringNoOffset(date.shiftedBy(oneUlp), "2009-01-01T00:00:00.000");
1443         checkToStringNoOffset(date.shiftedBy(one), "2009-01-01T00:00:01.000");
1444         checkToStringNoOffset(date.shiftedBy(-zeroUlp), "2009-01-01T00:00:00.000");
1445         // test leap
1446         // Orekit 10.1 throw OIAE, 10.2 rounds up
1447         checkToStringNoOffset(date.shiftedBy(-oneUlp), "2009-01-01T00:00:00.000");
1448         // Orekit 10.1 rounds up
1449         checkToStringNoOffset(date.shiftedBy(-1).shiftedBy(one), "2009-01-01T00:00:00.000");
1450         checkToStringNoOffset(date.shiftedBy(-0.5), "2008-12-31T23:59:60.500");
1451         checkToStringNoOffset(date.shiftedBy(-1).shiftedBy(zeroUlp), "2008-12-31T23:59:60.000");
1452         checkToStringNoOffset(date.shiftedBy(-1), "2008-12-31T23:59:60.000");
1453         checkToStringNoOffset(date.shiftedBy(-1).shiftedBy(-zeroUlp), "2008-12-31T23:59:60.000");
1454         checkToStringNoOffset(date.shiftedBy(-1).shiftedBy(-oneUlp), "2008-12-31T23:59:60.000");
1455         checkToStringNoOffset(date.shiftedBy(-2), "2008-12-31T23:59:59.000");
1456         // Orekit 10.1 rounds up
1457         checkToStringNoOffset(date.shiftedBy(-1).shiftedBy(-sixtyUlp), "2008-12-31T23:59:60.000");
1458         checkToStringNoOffset(date.shiftedBy(-61).shiftedBy(zeroUlp), "2008-12-31T23:59:00.000");
1459         checkToStringNoOffset(date.shiftedBy(-61).shiftedBy(oneUlp), "2008-12-31T23:59:00.000");
1460     }
1461 
1462 
1463     private void checkToStringNoOffset(final AbsoluteDate d, final String s) {
1464         MatcherAssert.assertThat(d.toStringWithoutUtcOffset(utc, 3), CoreMatchers.is(s));
1465         MatcherAssert.assertThat(
1466                 d.getComponents(utc).toStringWithoutUtcOffset(utc.minuteDuration(d), 3),
1467                 CoreMatchers.is(s));
1468     }
1469 
1470     /**
1471      * Check {@link AbsoluteDate#toString()} when UTC throws an exception. This ~is~ was
1472      * the most common issue new and old users face.
1473      */
1474     @Test
1475     void testToStringException() {
1476         Utils.setDataRoot("no-data");
1477         try {
1478             DataContext.getDefault().getTimeScales().getUTC();
1479             Assertions.fail("Expected Exception");
1480         } catch (OrekitException e) {
1481             // expected
1482             Assertions.assertEquals(e.getSpecifier(), OrekitMessages.NO_IERS_UTC_TAI_HISTORY_DATA_LOADED);
1483         }
1484         // try some unusual values
1485         MatcherAssert.assertThat(present.toString(), CoreMatchers.is("2000-01-01T12:00:32.000 TAI"));
1486         MatcherAssert.assertThat(present.shiftedBy(Double.POSITIVE_INFINITY).toString(),
1487                                  CoreMatchers.is("5881610-07-11T23:59:59.999 TAI"));
1488         MatcherAssert.assertThat(present.shiftedBy(Double.NEGATIVE_INFINITY).toString(),
1489                                  CoreMatchers.is("-5877490-03-03T00:00:00.000 TAI"));
1490         MatcherAssert.assertThat(present.shiftedBy(Double.NaN).toString(),
1491                                  CoreMatchers.is("2000-01-01T00:00:NaN TAI"));
1492         // infinity is special cased, but I can make AbsoluteDate.offset larger than
1493         // Long.MAX_VALUE see #584
1494         Assertions.assertTrue(Double.isInfinite(present.shiftedBy(1e300).durationFrom(present)));
1495     }
1496 
1497     /** Test for issue 943: management of past and future infinity in equality checks. */
1498     @Test
1499     void test_issue_943() {
1500 
1501         // Run issue test
1502         final AbsoluteDate date1 = new AbsoluteDate(AbsoluteDate.PAST_INFINITY, 0);
1503         final AbsoluteDate date2 = new AbsoluteDate(AbsoluteDate.PAST_INFINITY, 0);
1504         date1.durationFrom(date2);
1505         Assertions.assertEquals(date1, date2);
1506 
1507         // Check equality is as expected for PAST INFINITY
1508         final AbsoluteDate date3 = AbsoluteDate.PAST_INFINITY;
1509         final AbsoluteDate date4 = new AbsoluteDate(AbsoluteDate.PAST_INFINITY, 0);
1510         Assertions.assertEquals(date3, date4);
1511 
1512         // Check equality is as expected for FUTURE INFINITY
1513         final AbsoluteDate date5 = AbsoluteDate.FUTURE_INFINITY;
1514         final AbsoluteDate date6 = new AbsoluteDate(AbsoluteDate.FUTURE_INFINITY, 0);
1515         Assertions.assertEquals(date5, date6);
1516 
1517         // Check inequality is as expected
1518         final AbsoluteDate date7 = new AbsoluteDate(AbsoluteDate.PAST_INFINITY, 0);
1519         final AbsoluteDate date8 = new AbsoluteDate(AbsoluteDate.FUTURE_INFINITY, 0);
1520         Assertions.assertNotEquals(date7, date8);
1521 
1522         // Check inequality is as expected
1523         final AbsoluteDate date9 = new AbsoluteDate(new TimeOffset(Double.POSITIVE_INFINITY));
1524         final AbsoluteDate date10 = new AbsoluteDate(new TimeOffset(Double.POSITIVE_INFINITY));
1525         Assertions.assertEquals(date9, date10);
1526     }
1527 
1528     @Test
1529     void testNegativeOffsetConstructor() {
1530         AbsoluteDate date = new AbsoluteDate(2019, 10, 11, 20, 40,
1531                                              FastMath.scalb(6629298651489277.0, -55),
1532                                              TimeScalesFactory.getTT());
1533         AbsoluteDate after = date.shiftedBy(Precision.EPSILON);
1534         Assertions.assertEquals(624098367L, date.getSeconds());
1535         Assertions.assertEquals(FastMath.nextAfter(1.0, Double.NEGATIVE_INFINITY), 1.0e-18 * date.getAttoSeconds(), 2.4e-15);
1536         Assertions.assertEquals(Precision.EPSILON, after.durationFrom(date), 1.0e-18);
1537     }
1538 
1539     @Test
1540     void testNegativeOffsetShift() {
1541         AbsoluteDate reference = new AbsoluteDate(2019, 10, 11, 20, 40,
1542                                                   1.6667019180022178E-7,
1543                                                   TimeScalesFactory.getTAI());
1544         double dt = FastMath.scalb(6596520010750484.0, -39);
1545         AbsoluteDate shifted = reference.shiftedBy(dt);
1546         AbsoluteDate after = shifted.shiftedBy(Precision.EPSILON);
1547         Assertions.assertEquals(624110398L, shifted.getSeconds());
1548         Assertions.assertEquals((1.0 - 1.6922e-13) * 1.0e18, shifted.getAttoSeconds(), 1.0e-15);
1549         Assertions.assertEquals(Precision.EPSILON, after.durationFrom(shifted), 1.0e-18);
1550     }
1551 
1552     @Test
1553     void testDurationFromWithTimeUnit() {
1554         AbsoluteDate reference = new AbsoluteDate(2023, 1, 1, 12, 13, 59.12334567, utc);
1555         for (TimeUnit timeUnit : TimeUnit.values()) {
1556             Assertions.assertEquals(0, reference.durationFrom(reference, timeUnit));
1557 
1558             long dayInTimeUnit = timeUnit.convert((long) Constants.JULIAN_DAY, TimeUnit.SECONDS);
1559             for (int i = 1; i <= 365; i++) {
1560               AbsoluteDate minusDays = reference.shiftedBy(-i * Constants.JULIAN_DAY);
1561               AbsoluteDate plusDays = reference.shiftedBy(i* Constants.JULIAN_DAY);
1562 
1563 
1564               Assertions.assertEquals(i * dayInTimeUnit, reference.durationFrom(minusDays, timeUnit));
1565 
1566               Assertions.assertEquals(-i * dayInTimeUnit, reference.durationFrom(plusDays, timeUnit));
1567             }
1568 
1569            for (long ns = 1; ns <= 1_000_000_000; ns += 1_000_000) {
1570               AbsoluteDate minus = reference.shiftedBy(-1e-9 * ns);
1571               AbsoluteDate plus = reference.shiftedBy(1e-9 * ns);
1572 
1573               double deltaInTimeUnit = ns / (double) timeUnit.toNanos(1);
1574               Assertions.assertEquals(FastMath.round(deltaInTimeUnit), reference.durationFrom(minus, timeUnit),
1575                   String.format("TimeUnit: %s, ns: %d", timeUnit, ns));
1576 
1577               Assertions.assertEquals(FastMath.round(-deltaInTimeUnit), reference.durationFrom(plus, timeUnit),
1578                   String.format("TimeUnit: %s, ns: %d", timeUnit, ns));
1579             }
1580 
1581 
1582         }
1583     }
1584 
1585     @Test
1586     void testConstructWithTimeUnitOffset() {
1587       AbsoluteDate reference = new AbsoluteDate(2023, 1, 1, 12, 13, 59.12334567, utc);
1588 
1589       for (TimeUnit timeUnit : TimeUnit.values()) {
1590         Assertions.assertEquals(0,
1591             FastMath.abs(reference.durationFrom(new AbsoluteDate(reference, 0, timeUnit))), 1e-10);
1592 
1593         long dayInTimeUnit = timeUnit.convert((long) Constants.JULIAN_DAY, TimeUnit.SECONDS);
1594         for (int i = 1; i <= 365; i++) {
1595           AbsoluteDate minusDays = reference.shiftedBy(-i * Constants.JULIAN_DAY);
1596           AbsoluteDate plusDays = reference.shiftedBy(i* Constants.JULIAN_DAY);
1597 
1598           Assertions.assertEquals(0,
1599               FastMath.abs(reference.durationFrom(new AbsoluteDate(minusDays, i * dayInTimeUnit, timeUnit))),
1600               1e-10,
1601               String.format("TimeUnit: %s", timeUnit));
1602           Assertions.assertEquals(0,
1603               FastMath.abs(reference.durationFrom(new AbsoluteDate(plusDays, -i * dayInTimeUnit, timeUnit))),
1604               1e-10,
1605               String.format("TimeUnit: %s", timeUnit));
1606         }
1607 
1608         for (long ns = 1; ns <= 1_000_000_000; ns += 1_000_000) {
1609           if (timeUnit.convert(1, TimeUnit.SECONDS) < 1) {
1610             //Skip everything larger than one second
1611             continue;
1612           }
1613           AbsoluteDate minus = reference.shiftedBy(-1e-9 * ns);
1614           AbsoluteDate plus = reference.shiftedBy(1e-9 * ns);
1615 
1616           double deltaInTimeUnit =  ns / (double) timeUnit.toNanos(1);
1617           Assertions.assertEquals(0,
1618               FastMath.abs(reference.durationFrom(new AbsoluteDate(minus, FastMath.round(deltaInTimeUnit), timeUnit))),
1619               1.0 / timeUnit.convert(1, TimeUnit.SECONDS),
1620               String.format("TimeUnit: %s, ns: %d", timeUnit, ns));
1621           Assertions.assertEquals(0,
1622               FastMath.abs(reference.durationFrom(new AbsoluteDate(plus, FastMath.round(-deltaInTimeUnit), timeUnit))),
1623               1.0 / timeUnit.convert(1, TimeUnit.SECONDS),
1624               String.format("TimeUnit: %s, ns: %d", timeUnit, ns));
1625         }
1626       }
1627     }
1628 
1629     @Test
1630     void testShiftedByWithTimeUnit() {
1631         AbsoluteDate reference = new AbsoluteDate(2023, 1, 1, 12, 13, 59.12334567, utc);
1632 
1633         for (TimeUnit timeUnit : TimeUnit.values()) {
1634             Assertions.assertEquals(0,
1635                 FastMath.abs(reference.durationFrom(reference.shiftedBy( 0, timeUnit))), 1e-10);
1636 
1637             long dayInTimeUnit = timeUnit.convert((long) Constants.JULIAN_DAY, TimeUnit.SECONDS);
1638             for (int i = 1; i <= 365; i++) {
1639                 AbsoluteDate minusDays = reference.shiftedBy(-i * Constants.JULIAN_DAY);
1640                 AbsoluteDate plusDays = reference.shiftedBy(i* Constants.JULIAN_DAY);
1641 
1642                 Assertions.assertEquals(0,
1643                     FastMath.abs(reference.durationFrom(minusDays.shiftedBy(i * dayInTimeUnit, timeUnit))),
1644                     1e-10,
1645                     String.format("TimeUnit: %s", timeUnit));
1646                 Assertions.assertEquals(0,
1647                     FastMath.abs(reference.durationFrom(plusDays.shiftedBy(-i * dayInTimeUnit, timeUnit))),
1648                     1e-10,
1649                     String.format("TimeUnit: %s", timeUnit));
1650             }
1651 
1652             for (long ns = 1; ns <= 1_000_000_000; ns += 1_000_000) {
1653                 if (timeUnit.convert(1, TimeUnit.SECONDS) < 1) {
1654                     //Skip everything larger than one second
1655                     continue;
1656                 }
1657                 AbsoluteDate minus = reference.shiftedBy(-1e-9 * ns);
1658                 AbsoluteDate plus = reference.shiftedBy(1e-9 * ns);
1659 
1660                 double deltaInTimeUnit =  ns / (double) timeUnit.toNanos(1);
1661                 Assertions.assertEquals(0,
1662                     FastMath.abs(reference.durationFrom(minus.shiftedBy(FastMath.round(deltaInTimeUnit), timeUnit))),
1663                     1.0 / timeUnit.convert(1, TimeUnit.SECONDS),
1664                     String.format("TimeUnit: %s, ns: %d", timeUnit, ns));
1665                 Assertions.assertEquals(0,
1666                     FastMath.abs(reference.durationFrom(plus.shiftedBy(FastMath.round(-deltaInTimeUnit), timeUnit))),
1667                     1.0 / timeUnit.convert(1, TimeUnit.SECONDS),
1668                     String.format("TimeUnit: %s, ns: %d", timeUnit, ns));
1669             }
1670         }
1671     }
1672 
1673     @Test
1674     void testGetJulianDates() {
1675 
1676         // GIVEN a reference date
1677         final TimeScale utc = TimeScalesFactory.getUTC();
1678 
1679         AbsoluteDate reference              = new AbsoluteDate(2024, 7, 4, 13, 0, 0, utc);
1680         AbsoluteDate referenceFromJDMethod  = AbsoluteDate.createJDDate(2460496, .0416667 * Constants.JULIAN_DAY, utc);
1681         AbsoluteDate referenceFromMJDMethod = AbsoluteDate.createMJDDate(60495, 0.54166670 * Constants.JULIAN_DAY, utc);
1682 
1683         // WHEN converting it to Julian Date or Modified Julian Date
1684         double mjdDateDefaultData = reference.getMJD();
1685         double jdDateDefaultData  = reference.getJD();
1686         double mjdDate            = reference.getMJD(utc);
1687         double jdDate             = reference.getJD(utc);
1688 
1689         // THEN
1690         // source : Time/Date Converter - HEASARC - NASA
1691         Assertions.assertEquals(2460496.0416667, jdDateDefaultData, 1.0e-6);
1692         Assertions.assertEquals(60495.54166670, mjdDateDefaultData, 1.0e-6);
1693         Assertions.assertEquals(jdDate, jdDateDefaultData, 1.0e-6);
1694         Assertions.assertEquals(mjdDateDefaultData, mjdDate);
1695 
1696         // Assert that static method are correct when creating date from JD or MJD
1697         Assertions.assertTrue(reference.isCloseTo(referenceFromJDMethod, 1e-2));
1698         Assertions.assertTrue(reference.isCloseTo(referenceFromMJDMethod, 1e-2));
1699     }
1700 
1701     @Test
1702     void testLargeLeapSecond() {
1703         // this corresponds to issue 707
1704         Assertions.assertEquals(new AbsoluteDate(1961, 1, 1, utc).
1705                                 shiftedBy(new TimeOffset(22818, TimeOffset.MICROSECOND).negate()),
1706                                 new AbsoluteDate("1960-12-31T23:59:61.4", utc));
1707     }
1708 
1709     @Test
1710     void testGetDayOfYear() {
1711         Assertions.assertEquals(0.501,
1712                                 new AbsoluteDate(2004,  1,  1,  0,  0,  0.001, utc).getDayOfYear(utc),
1713                                 1.0e-3);
1714         Assertions.assertEquals(1.000,
1715                                 new AbsoluteDate(2004,  1,  1, 12,  0,  0.000, utc).getDayOfYear(utc),
1716                                 1.0e-3);
1717         Assertions.assertEquals(366.0,
1718                                 new AbsoluteDate(2004, 12, 31, 12,  0,  0.000, utc).getDayOfYear(utc),
1719                                 1.0e-3);
1720         Assertions.assertEquals(366.499999988426,
1721                                 new AbsoluteDate(2004, 12, 31, 23, 59, 59.999, utc).getDayOfYear(utc),
1722                                 1.0e-12);
1723         Assertions.assertEquals(0.500000011574,
1724                                 new AbsoluteDate(2004, 12, 31, 23, 59, 59.999, utc).shiftedBy(0.002).getDayOfYear(utc),
1725                                 1.0e-12);
1726     }
1727 
1728     @BeforeEach
1729     public void setUp() {
1730         Utils.setDataRoot("regular-data");
1731         utc = TimeScalesFactory.getUTC();
1732         present = new AbsoluteDate(new DateComponents(2000, 1, 1),
1733                                     new TimeComponents(12, 0, 0), utc);
1734         presentToo = new AnyTimeStamped(present.shiftedBy(0));
1735         past = new AnyTimeStamped(present.shiftedBy(-1000));
1736         future = new AnyTimeStamped(present.shiftedBy(1000));
1737     }
1738 
1739     private TimeScale utc;
1740     private AbsoluteDate present;
1741     private AnyTimeStamped past;
1742     private AnyTimeStamped presentToo;
1743     private AnyTimeStamped future;
1744 
1745     static class AnyTimeStamped implements TimeStamped {
1746         AbsoluteDate date;
1747         public AnyTimeStamped(AbsoluteDate date) {
1748             this.date = date;
1749         }
1750 
1751         @Override
1752         public AbsoluteDate getDate() {
1753             return date;
1754         }
1755     }
1756 }