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 org.junit.jupiter.api.Assertions;
20  import org.junit.jupiter.api.Test;
21  import org.orekit.Utils;
22  import org.orekit.errors.OrekitException;
23  import org.orekit.errors.OrekitMessages;
24  
25  public class AGILeapSecondFilesLoaderTest {
26  
27      @Test
28      public void testRegularFile() {
29  
30          Utils.setDataRoot("AGI");
31  
32          // we arbitrary put UTC == TAI before 1961-01-01
33          checkOffset(1950,  1,  1,   0);
34  
35          // linear models between 1961 and 1972
36          checkOffset(1961,  1,  2,  -(1.422818 +   1 * 0.001296));  // MJD 37300 +   1
37          checkOffset(1961,  8,  2,  -(1.372818 + 213 * 0.001296));  // MJD 37300 + 213
38          checkOffset(1962,  1,  2,  -(1.845858 +   1 * 0.0011232)); // MJD 37665 +   1
39          checkOffset(1963, 11,  2,  -(1.945858 + 670 * 0.0011232)); // MJD 37665 + 670
40          checkOffset(1964,  1,  2,  -(3.240130 - 365 * 0.001296));  // MJD 38761 - 365
41          checkOffset(1964,  4,  2,  -(3.340130 - 274 * 0.001296));  // MJD 38761 - 274
42          checkOffset(1964,  9,  2,  -(3.440130 - 121 * 0.001296));  // MJD 38761 - 121
43          checkOffset(1965,  1,  2,  -(3.540130 +   1 * 0.001296));  // MJD 38761 +   1
44          checkOffset(1965,  3,  2,  -(3.640130 +  60 * 0.001296));  // MJD 38761 +  60
45          checkOffset(1965,  7,  2,  -(3.740130 + 182 * 0.001296));  // MJD 38761 + 182
46          checkOffset(1965,  9,  2,  -(3.840130 + 244 * 0.001296));  // MJD 38761 + 244
47          checkOffset(1966,  1,  2,  -(4.313170 +   1 * 0.002592));  // MJD 39126 +   1
48          checkOffset(1968,  2,  2,  -(4.213170 + 762 * 0.002592));  // MJD 39126 + 762
49  
50          // since 1972-01-01, offsets are only whole seconds
51          checkOffset(1972,  3,  5, -10);
52          checkOffset(1972,  7, 14, -11);
53          checkOffset(1979, 12, 31, -18);
54          checkOffset(1980,  1, 22, -19);
55          checkOffset(2006,  7,  7, -33);
56          checkOffset(2010,  7,  7, -34);
57          checkOffset(2012,  7,  7, -35);
58          checkOffset(2015,  7,  7, -36);
59  
60      }
61  
62      @Test
63      public void testInconsistentDate() {
64          checkException("LeapSecond-inconsistent-date.dat",
65                         OrekitMessages.INCONSISTENT_DATES_IN_IERS_FILE);
66      }
67  
68      @Test
69      public void testNonChronological() {
70          checkException("LeapSecond-non-chronological.dat",
71                         OrekitMessages.NON_CHRONOLOGICAL_DATES_IN_FILE);
72      }
73  
74      @Test
75      public void testFormatError() {
76          checkException("LeapSecond-format-error.dat",
77                         OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE);
78      }
79  
80      private void checkOffset(int year, int month, int day, double offset) {
81          TimeScale utc = TimeScalesFactory.getUTC();
82          AbsoluteDate date = new AbsoluteDate(year, month, day, utc);
83          Assertions.assertEquals(offset, utc.offsetFromTAI(date).toDouble(), 1.0e-10);
84      }
85  
86      private void checkException(String name, OrekitMessages message) {
87          Utils.setDataRoot("AGI");
88          TimeScalesFactory.addUTCTAIOffsetsLoader(new AGILeapSecondFilesLoader(name));
89          try {
90              TimeScalesFactory.getUTC();
91              Assertions.fail("an exception should have been thrown");
92          } catch (OrekitException oe) {
93              Assertions.assertEquals(message, oe.getSpecifier());
94          }
95      }
96  
97  }