1 /* Copyright 2002-2016 CS Systèmes d'Information
2 * Licensed to CS Systèmes d'Information (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.io.Serializable;
20 import java.util.List;
21
22 import org.orekit.errors.OrekitException;
23 import org.orekit.errors.OrekitInternalError;
24 import org.orekit.utils.Constants;
25
26 /** Coordinated Universal Time.
27 * <p>UTC is related to TAI using step adjustments from time to time
28 * according to IERS (International Earth Rotation Service) rules. Before 1972,
29 * these adjustments were piecewise linear offsets. Since 1972, these adjustments
30 * are piecewise constant offsets, which require introduction of leap seconds.</p>
31 * <p>Leap seconds are always inserted as additional seconds at the last minute
32 * of the day, pushing the next day forward. Such minutes are therefore more
33 * than 60 seconds long. In theory, there may be seconds removal instead of seconds
34 * insertion, but up to now (2010) it has never been used. As an example, when a
35 * one second leap was introduced at the end of 2005, the UTC time sequence was
36 * 2005-12-31T23:59:59 UTC, followed by 2005-12-31T23:59:60 UTC, followed by
37 * 2006-01-01T00:00:00 UTC.</p>
38 * <p>This is intended to be accessed thanks to the {@link TimeScalesFactory} class,
39 * so there is no public constructor.</p>
40 * @author Luc Maisonobe
41 * @see AbsoluteDate
42 */
43 public class UTCScale implements TimeScale {
44
45 /** Serializable UID. */
46 private static final long serialVersionUID = 20150402L;
47
48 /** UTC-TAI offsets. */
49 private UTCTAIOffset[] offsets;
50
51 /** Package private constructor for the factory.
52 * Used to create the prototype instance of this class that is used to
53 * clone all subsequent instances of {@link UTCScale}. Initializes the offset
54 * table that is shared among all instances.
55 * @param offsetModels UTC-TAI offsets
56 * @exception OrekitException if cache cannot be set up
57 */
58 UTCScale(final List<OffsetModel> offsetModels) throws OrekitException {
59
60 if (offsetModels.get(0).getStart().getYear() > 1968) {
61 // the pre-1972 linear offsets are missing, add them manually
62 // excerpt from UTC-TAI.history file:
63 // 1961 Jan. 1 - 1961 Aug. 1 1.422 818 0s + (MJD - 37 300) x 0.001 296s
64 // Aug. 1 - 1962 Jan. 1 1.372 818 0s + ""
65 // 1962 Jan. 1 - 1963 Nov. 1 1.845 858 0s + (MJD - 37 665) x 0.001 123 2s
66 // 1963 Nov. 1 - 1964 Jan. 1 1.945 858 0s + ""
67 // 1964 Jan. 1 - April 1 3.240 130 0s + (MJD - 38 761) x 0.001 296s
68 // April 1 - Sept. 1 3.340 130 0s + ""
69 // Sept. 1 - 1965 Jan. 1 3.440 130 0s + ""
70 // 1965 Jan. 1 - March 1 3.540 130 0s + ""
71 // March 1 - Jul. 1 3.640 130 0s + ""
72 // Jul. 1 - Sept. 1 3.740 130 0s + ""
73 // Sept. 1 - 1966 Jan. 1 3.840 130 0s + ""
74 // 1966 Jan. 1 - 1968 Feb. 1 4.313 170 0s + (MJD - 39 126) x 0.002 592s
75 // 1968 Feb. 1 - 1972 Jan. 1 4.213 170 0s + ""
76 offsetModels.add( 0, new OffsetModel(new DateComponents(1961, 1, 1), 37300, 1.4228180, 0.0012960));
77 offsetModels.add( 1, new OffsetModel(new DateComponents(1961, 8, 1), 37300, 1.3728180, 0.0012960));
78 offsetModels.add( 2, new OffsetModel(new DateComponents(1962, 1, 1), 37665, 1.8458580, 0.0011232));
79 offsetModels.add( 3, new OffsetModel(new DateComponents(1963, 11, 1), 37665, 1.9458580, 0.0011232));
80 offsetModels.add( 4, new OffsetModel(new DateComponents(1964, 1, 1), 38761, 3.2401300, 0.0012960));
81 offsetModels.add( 5, new OffsetModel(new DateComponents(1964, 4, 1), 38761, 3.3401300, 0.0012960));
82 offsetModels.add( 6, new OffsetModel(new DateComponents(1964, 9, 1), 38761, 3.4401300, 0.0012960));
83 offsetModels.add( 7, new OffsetModel(new DateComponents(1965, 1, 1), 38761, 3.5401300, 0.0012960));
84 offsetModels.add( 8, new OffsetModel(new DateComponents(1965, 3, 1), 38761, 3.6401300, 0.0012960));
85 offsetModels.add( 9, new OffsetModel(new DateComponents(1965, 7, 1), 38761, 3.7401300, 0.0012960));
86 offsetModels.add(10, new OffsetModel(new DateComponents(1965, 9, 1), 38761, 3.8401300, 0.0012960));
87 offsetModels.add(11, new OffsetModel(new DateComponents(1966, 1, 1), 39126, 4.3131700, 0.0025920));
88 offsetModels.add(12, new OffsetModel(new DateComponents(1968, 2, 1), 39126, 4.2131700, 0.0025920));
89 }
90
91 // create cache
92 offsets = new UTCTAIOffset[offsetModels.size()];
93
94 UTCTAIOffset previous = null;
95
96 // link the offsets together
97 final TimeScale tai = TimeScalesFactory.getTAI();
98 for (int i = 0; i < offsetModels.size(); ++i) {
99
100 final OffsetModel o = offsetModels.get(i);
101 final DateComponents date = o.getStart();
102 final int mjdRef = o.getMJDRef();
103 final double offset = o.getOffset();
104 final double slope = o.getSlope();
105
106 // start of the leap
107 final double previousOffset = (previous == null) ? 0.0 : previous.getOffset(date, TimeComponents.H00);
108 final AbsoluteDate leapStart = new AbsoluteDate(date, tai).shiftedBy(previousOffset);
109
110 // end of the leap
111 final double startOffset = offset + slope * (date.getMJD() - mjdRef);
112 final AbsoluteDate leapEnd = new AbsoluteDate(date, tai).shiftedBy(startOffset);
113
114 // leap computed at leap start and in UTC scale
115 final double normalizedSlope = slope / Constants.JULIAN_DAY;
116 final double leap = leapEnd.durationFrom(leapStart) / (1 + normalizedSlope);
117
118 previous = new UTCTAIOffset(leapStart, date.getMJD(), leap, offset, mjdRef, normalizedSlope);
119 offsets[i] = previous;
120
121 }
122
123 }
124
125 /** {@inheritDoc} */
126 @Override
127 public double offsetFromTAI(final AbsoluteDate date) {
128 final int offsetIndex = findOffsetIndex(date);
129 if (offsetIndex < 0) {
130 // the date is before the first known leap
131 return 0;
132 } else {
133 return -offsets[offsetIndex].getOffset(date);
134 }
135 }
136
137 /** {@inheritDoc} */
138 @Override
139 public double offsetToTAI(final DateComponents date,
140 final TimeComponents time) {
141
142 // take offset from local time into account, but ignoring seconds,
143 // so when we parse an hour like 23:59:60.5 during leap seconds introduction,
144 // we do not jump to next day
145 final int minuteInDay = time.getHour() * 60 + time.getMinute() - time.getMinutesFromUTC();
146 final int correction = minuteInDay < 0 ? (minuteInDay - 1439) / 1440 : minuteInDay / 1440;
147
148 // find close neighbors, assuming date in TAI, i.e a date earlier than real UTC date
149 final int mjd = date.getMJD() + correction;
150 final UTCTAIOffset offset = findOffset(mjd);
151 if (offset == null) {
152 // the date is before the first known leap
153 return 0;
154 } else {
155 return offset.getOffset(date, time);
156 }
157
158 }
159
160 /** {@inheritDoc} */
161 public String getName() {
162 return "UTC";
163 }
164
165 /** {@inheritDoc} */
166 public String toString() {
167 return getName();
168 }
169
170 /** Get the date of the first known leap second.
171 * @return date of the first known leap second
172 */
173 public AbsoluteDate getFirstKnownLeapSecond() {
174 return offsets[0].getDate();
175 }
176
177 /** Get the date of the last known leap second.
178 * @return date of the last known leap second
179 */
180 public AbsoluteDate getLastKnownLeapSecond() {
181 return offsets[offsets.length - 1].getDate();
182 }
183
184 /** {@inheritDoc} */
185 @Override
186 public boolean insideLeap(final AbsoluteDate date) {
187 final int offsetIndex = findOffsetIndex(date);
188 if (offsetIndex < 0) {
189 // the date is before the first known leap
190 return false;
191 } else {
192 return date.compareTo(offsets[offsetIndex].getValidityStart()) < 0;
193 }
194 }
195
196 /** {@inheritDoc} */
197 @Override
198 public int minuteDuration(final AbsoluteDate date) {
199 final int offsetIndex = findOffsetIndex(date);
200 if (offsetIndex < 0) {
201 // the date is before the first known leap
202 return 60;
203 } else {
204 if (date.compareTo(offsets[offsetIndex].getValidityStart()) < 0) {
205 // the date is during the leap itself
206 return 61;
207 } else {
208 // the date is after a leap, but it may be just before the next one
209 if (offsetIndex + 1 < offsets.length &&
210 offsets[offsetIndex + 1].getDate().durationFrom(date) <= 60.0) {
211 // the next leap will start in one minute, it will extend the current minute
212 return 61;
213 } else {
214 // no leap is expected within the next minute
215 return 60;
216 }
217 }
218 }
219 }
220
221 /** {@inheritDoc} */
222 @Override
223 public double getLeap(final AbsoluteDate date) {
224 final int offsetIndex = findOffsetIndex(date);
225 if (offsetIndex < 0) {
226 // the date is before the first known leap
227 return 0;
228 } else {
229 return offsets[offsetIndex].getLeap();
230 }
231 }
232
233 /** Find the index of the offset valid at some date.
234 * @param date date at which offset is requested
235 * @return index of the offset valid at this date, or -1 if date is before first offset.
236 */
237 private int findOffsetIndex(final AbsoluteDate date) {
238 int inf = 0;
239 int sup = offsets.length;
240 while (sup - inf > 1) {
241 final int middle = (inf + sup) >>> 1;
242 if (date.compareTo(offsets[middle].getDate()) < 0) {
243 sup = middle;
244 } else {
245 inf = middle;
246 }
247 }
248 if (sup == offsets.length) {
249 // the date is after the last known leap second
250 return offsets.length - 1;
251 } else if (date.compareTo(offsets[inf].getDate()) < 0) {
252 // the date is before the first known leap
253 return -1;
254 } else {
255 return inf;
256 }
257 }
258
259 /** Find the offset valid at some date.
260 * @param mjd Modified Julian Day of the date at which offset is requested
261 * @return offset valid at this date, or null if date is before first offset.
262 */
263 private UTCTAIOffset findOffset(final int mjd) {
264 int inf = 0;
265 int sup = offsets.length;
266 while (sup - inf > 1) {
267 final int middle = (inf + sup) >>> 1;
268 if (mjd < offsets[middle].getMJD()) {
269 sup = middle;
270 } else {
271 inf = middle;
272 }
273 }
274 if (sup == offsets.length) {
275 // the date is after the last known leap second
276 return offsets[offsets.length - 1];
277 } else if (mjd < offsets[inf].getMJD()) {
278 // the date is before the first known leap
279 return null;
280 } else {
281 return offsets[inf];
282 }
283 }
284
285 /** Replace the instance with a data transfer object for serialization.
286 * @return data transfer object that will be serialized
287 */
288 private Object writeReplace() {
289 return new DataTransferObject();
290 }
291
292 /** Internal class used only for serialization. */
293 private static class DataTransferObject implements Serializable {
294
295 /** Serializable UID. */
296 private static final long serialVersionUID = 20131209L;
297
298 /** Replace the deserialized data transfer object with a {@link UTCScale}.
299 * @return replacement {@link UTCScale}
300 */
301 private Object readResolve() {
302 try {
303 return TimeScalesFactory.getUTC();
304 } catch (OrekitException oe) {
305 throw new OrekitInternalError(oe);
306 }
307 }
308
309 }
310
311 }