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