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.BeforeEach;
21  import org.junit.jupiter.api.Test;
22  import org.orekit.Utils;
23  import org.orekit.utils.Constants;
24  
25  public class GLONASSScaleTest {
26  
27      private GLONASSScale glonass;
28  
29      @Test
30      public void testT0() {
31          TimeScale scale = TimeScalesFactory.getGLONASS();
32          Assertions.assertEquals("GLONASS", scale.toString());
33          AbsoluteDate t0 =
34              new AbsoluteDate(new DateComponents(1996, 1, 1), TimeComponents.H00, scale);
35          Assertions.assertEquals(AbsoluteDate.GLONASS_EPOCH, t0);
36      }
37  
38      @Test
39      public void testArbitrary() {
40          AbsoluteDate tGLONASS =
41              new AbsoluteDate(new DateComponents(1999, 3, 4), TimeComponents.H00, glonass);
42          AbsoluteDate tUTC =
43              new AbsoluteDate(new DateComponents(1999, 3, 3), new TimeComponents(21, 0, 0),
44                               TimeScalesFactory.getUTC());
45          Assertions.assertEquals(tUTC, tGLONASS);
46      }
47  
48      @Test
49      public void testLeap2006() {
50          final UTCScale utc = TimeScalesFactory.getUTC();
51          AbsoluteDate leapDate =
52              new AbsoluteDate(new DateComponents(2006, 1, 1), TimeComponents.H00, utc);
53          AbsoluteDate d1 = leapDate.shiftedBy(-1);
54          AbsoluteDate d2 = leapDate.shiftedBy(+1);
55          Assertions.assertEquals(2.0, d2.durationFrom(d1), 1.0e-10);
56  
57          AbsoluteDate d3 = new AbsoluteDate(new DateComponents(2006, 1, 1),
58                                             new TimeComponents(2, 59, 59),
59                                             glonass);
60          Assertions.assertEquals(new AbsoluteDate(new DateComponents(2005, 12, 31),
61                                               new TimeComponents(23, 59, 59),
62                                               utc),
63                              d3);
64          AbsoluteDate d4 = new AbsoluteDate(new DateComponents(2006, 1, 1),
65                                             new TimeComponents(3, 0, 1),
66                                             glonass);
67          Assertions.assertEquals(new AbsoluteDate(new DateComponents(2006, 1, 1),
68                                               new TimeComponents(0, 0, 1),
69                                               utc),
70                              d4);
71          Assertions.assertEquals(3.0, d4.durationFrom(d3), 1.0e-10);
72      }
73  
74      @Test
75      public void testDuringLeap() {
76          AbsoluteDate d = new AbsoluteDate(new DateComponents(1983, 6, 30),
77                                            new TimeComponents(23, 59, 59),
78                                            TimeScalesFactory.getUTC());
79          Assertions.assertEquals("1983-07-01T02:58:59.000", d.shiftedBy(-60).toString(glonass));
80          Assertions.assertEquals(60, glonass.minuteDuration(d.shiftedBy(-60)));
81          Assertions.assertFalse(glonass.insideLeap(d.shiftedBy(-60)));
82          Assertions.assertEquals("1983-07-01T02:59:59.000", d.toString(glonass));
83          Assertions.assertEquals(61, glonass.minuteDuration(d));
84          Assertions.assertFalse(glonass.insideLeap(d));
85          d = d.shiftedBy(0.251);
86          Assertions.assertEquals("1983-07-01T02:59:59.251", d.toString(glonass));
87          Assertions.assertEquals(61, glonass.minuteDuration(d));
88          Assertions.assertFalse(glonass.insideLeap(d));
89          d = d.shiftedBy(0.251);
90          Assertions.assertEquals("1983-07-01T02:59:59.502", d.toString(glonass));
91          Assertions.assertEquals(61, glonass.minuteDuration(d));
92          Assertions.assertFalse(glonass.insideLeap(d));
93          d = d.shiftedBy(0.251);
94          Assertions.assertEquals("1983-07-01T02:59:59.753", d.toString(glonass));
95          Assertions.assertEquals(61, glonass.minuteDuration(d));
96          Assertions.assertFalse(glonass.insideLeap(d));
97          d = d.shiftedBy( 0.251);
98          Assertions.assertEquals("1983-07-01T02:59:60.004", d.toString(glonass));
99          Assertions.assertEquals(61, glonass.minuteDuration(d));
100         Assertions.assertTrue(glonass.insideLeap(d));
101         d = d.shiftedBy(0.251);
102         Assertions.assertEquals("1983-07-01T02:59:60.255", d.toString(glonass));
103         Assertions.assertEquals(61, glonass.minuteDuration(d));
104         Assertions.assertTrue(glonass.insideLeap(d));
105         d = d.shiftedBy(0.251);
106         Assertions.assertEquals("1983-07-01T02:59:60.506", d.toString(glonass));
107         Assertions.assertEquals(61, glonass.minuteDuration(d));
108         Assertions.assertTrue(glonass.insideLeap(d));
109         d = d.shiftedBy(0.251);
110         Assertions.assertEquals("1983-07-01T02:59:60.757", d.toString(glonass));
111         Assertions.assertEquals(61, glonass.minuteDuration(d));
112         Assertions.assertTrue(glonass.insideLeap(d));
113         d = d.shiftedBy(0.251);
114         Assertions.assertEquals("1983-07-01T03:00:00.008", d.toString(glonass));
115         Assertions.assertEquals(60, glonass.minuteDuration(d));
116         Assertions.assertFalse(glonass.insideLeap(d));
117     }
118 
119     @Test
120     public void testSymmetry() {
121         for (double dt = -10000; dt < 10000; dt += 123.456789) {
122             AbsoluteDate date = AbsoluteDate.J2000_EPOCH.shiftedBy(dt * Constants.JULIAN_DAY);
123             double dt1 = glonass.offsetFromTAI(date).toDouble();
124             DateTimeComponents components = date.getComponents(glonass);
125             double dt2 = glonass.offsetToTAI(components.getDate(), components.getTime()).toDouble();
126             Assertions.assertEquals( 0.0, dt1 + dt2, 1.0e-10);
127         }
128     }
129 
130     @Test
131     public void testWrapBeforeLeap() {
132         AbsoluteDate t = new AbsoluteDate("2015-07-01T02:59:59.999999", glonass);
133         Assertions.assertEquals("2015-07-01T02:59:60.000+00:00",
134                 t.getComponents(glonass).toString(glonass.minuteDuration(t)));
135     }
136 
137     @Test
138     public void testMinuteDuration() {
139         final AbsoluteDate t0 = new AbsoluteDate("1983-07-01T02:58:59.000", glonass);
140         for (double dt = 0; dt < 63; dt += 0.3) {
141             if (dt < 1.0) {
142                 // before the minute of the leap
143                 Assertions.assertEquals(60, glonass.minuteDuration(t0.shiftedBy(dt)));
144             } else if (dt < 62.0) {
145                 // during the minute of the leap
146                 Assertions.assertEquals(61, glonass.minuteDuration(t0.shiftedBy(dt)));
147             } else {
148                 // after the minute of the leap
149                 Assertions.assertEquals(60, glonass.minuteDuration(t0.shiftedBy(dt)));
150             }
151         }
152     }
153 
154     @BeforeEach
155     public void setUp() {
156         Utils.setDataRoot("regular-data");
157         glonass = TimeScalesFactory.getGLONASS();
158     }
159 
160 }