UTCScale.java

  1. /* Copyright 2002-2019 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. import java.io.Serializable;
  19. import java.util.List;

  20. import org.hipparchus.RealFieldElement;
  21. import org.orekit.errors.OrekitException;
  22. import org.orekit.errors.OrekitInternalError;
  23. import org.orekit.utils.Constants;

  24. /** Coordinated Universal Time.
  25.  * <p>UTC is related to TAI using step adjustments from time to time
  26.  * according to IERS (International Earth Rotation Service) rules. Before 1972,
  27.  * these adjustments were piecewise linear offsets. Since 1972, these adjustments
  28.  * are piecewise constant offsets, which require introduction of leap seconds.</p>
  29.  * <p>Leap seconds are always inserted as additional seconds at the last minute
  30.  * of the day, pushing the next day forward. Such minutes are therefore more
  31.  * than 60 seconds long. In theory, there may be seconds removal instead of seconds
  32.  * insertion, but up to now (2010) it has never been used. As an example, when a
  33.  * one second leap was introduced at the end of 2005, the UTC time sequence was
  34.  * 2005-12-31T23:59:59 UTC, followed by 2005-12-31T23:59:60 UTC, followed by
  35.  * 2006-01-01T00:00:00 UTC.</p>
  36.  * <p>This is intended to be accessed thanks to the {@link TimeScalesFactory} class,
  37.  * so there is no public constructor.</p>
  38.  * @author Luc Maisonobe
  39.  * @see AbsoluteDate
  40.  */
  41. public class UTCScale implements TimeScale {

  42.     /** Serializable UID. */
  43.     private static final long serialVersionUID = 20150402L;

  44.     /** UTC-TAI offsets. */
  45.     private UTCTAIOffset[] offsets;

  46.     /** Package private constructor for the factory.
  47.      * Used to create the prototype instance of this class that is used to
  48.      * clone all subsequent instances of {@link UTCScale}. Initializes the offset
  49.      * table that is shared among all instances.
  50.      * @param offsetModels UTC-TAI offsets
  51.      */
  52.     UTCScale(final List<OffsetModel> offsetModels) {

  53.         if (offsetModels.get(0).getStart().getYear() > 1968) {
  54.             // the pre-1972 linear offsets are missing, add them manually
  55.             // excerpt from UTC-TAI.history file:
  56.             //  1961  Jan.  1 - 1961  Aug.  1     1.422 818 0s + (MJD - 37 300) x 0.001 296s
  57.             //        Aug.  1 - 1962  Jan.  1     1.372 818 0s +        ""
  58.             //  1962  Jan.  1 - 1963  Nov.  1     1.845 858 0s + (MJD - 37 665) x 0.001 123 2s
  59.             //  1963  Nov.  1 - 1964  Jan.  1     1.945 858 0s +        ""
  60.             //  1964  Jan.  1 -       April 1     3.240 130 0s + (MJD - 38 761) x 0.001 296s
  61.             //        April 1 -       Sept. 1     3.340 130 0s +        ""
  62.             //        Sept. 1 - 1965  Jan.  1     3.440 130 0s +        ""
  63.             //  1965  Jan.  1 -       March 1     3.540 130 0s +        ""
  64.             //        March 1 -       Jul.  1     3.640 130 0s +        ""
  65.             //        Jul.  1 -       Sept. 1     3.740 130 0s +        ""
  66.             //        Sept. 1 - 1966  Jan.  1     3.840 130 0s +        ""
  67.             //  1966  Jan.  1 - 1968  Feb.  1     4.313 170 0s + (MJD - 39 126) x 0.002 592s
  68.             //  1968  Feb.  1 - 1972  Jan.  1     4.213 170 0s +        ""
  69.             offsetModels.add( 0, new OffsetModel(new DateComponents(1961,  1, 1), 37300, 1.4228180, 0.0012960));
  70.             offsetModels.add( 1, new OffsetModel(new DateComponents(1961,  8, 1), 37300, 1.3728180, 0.0012960));
  71.             offsetModels.add( 2, new OffsetModel(new DateComponents(1962,  1, 1), 37665, 1.8458580, 0.0011232));
  72.             offsetModels.add( 3, new OffsetModel(new DateComponents(1963, 11, 1), 37665, 1.9458580, 0.0011232));
  73.             offsetModels.add( 4, new OffsetModel(new DateComponents(1964,  1, 1), 38761, 3.2401300, 0.0012960));
  74.             offsetModels.add( 5, new OffsetModel(new DateComponents(1964,  4, 1), 38761, 3.3401300, 0.0012960));
  75.             offsetModels.add( 6, new OffsetModel(new DateComponents(1964,  9, 1), 38761, 3.4401300, 0.0012960));
  76.             offsetModels.add( 7, new OffsetModel(new DateComponents(1965,  1, 1), 38761, 3.5401300, 0.0012960));
  77.             offsetModels.add( 8, new OffsetModel(new DateComponents(1965,  3, 1), 38761, 3.6401300, 0.0012960));
  78.             offsetModels.add( 9, new OffsetModel(new DateComponents(1965,  7, 1), 38761, 3.7401300, 0.0012960));
  79.             offsetModels.add(10, new OffsetModel(new DateComponents(1965,  9, 1), 38761, 3.8401300, 0.0012960));
  80.             offsetModels.add(11, new OffsetModel(new DateComponents(1966,  1, 1), 39126, 4.3131700, 0.0025920));
  81.             offsetModels.add(12, new OffsetModel(new DateComponents(1968,  2, 1), 39126, 4.2131700, 0.0025920));
  82.         }

  83.         // create cache
  84.         offsets = new UTCTAIOffset[offsetModels.size()];

  85.         UTCTAIOffset previous = null;

  86.         // link the offsets together
  87.         final TimeScale tai = TimeScalesFactory.getTAI();
  88.         for (int i = 0; i < offsetModels.size(); ++i) {

  89.             final OffsetModel    o      = offsetModels.get(i);
  90.             final DateComponents date   = o.getStart();
  91.             final int            mjdRef = o.getMJDRef();
  92.             final double         offset = o.getOffset();
  93.             final double         slope  = o.getSlope();

  94.             // start of the leap
  95.             final double previousOffset    = (previous == null) ? 0.0 : previous.getOffset(date, TimeComponents.H00);
  96.             final AbsoluteDate leapStart   = new AbsoluteDate(date, tai).shiftedBy(previousOffset);

  97.             // end of the leap
  98.             final double startOffset       = offset + slope * (date.getMJD() - mjdRef);
  99.             final AbsoluteDate leapEnd     = new AbsoluteDate(date, tai).shiftedBy(startOffset);

  100.             // leap computed at leap start and in UTC scale
  101.             final double normalizedSlope   = slope / Constants.JULIAN_DAY;
  102.             final double leap              = leapEnd.durationFrom(leapStart) / (1 + normalizedSlope);

  103.             previous = new UTCTAIOffset(leapStart, date.getMJD(), leap, offset, mjdRef, normalizedSlope);
  104.             offsets[i] = previous;

  105.         }

  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     public double offsetFromTAI(final AbsoluteDate date) {
  110.         final int offsetIndex = findOffsetIndex(date);
  111.         if (offsetIndex < 0) {
  112.             // the date is before the first known leap
  113.             return 0;
  114.         } else {
  115.             return -offsets[offsetIndex].getOffset(date);
  116.         }
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     public <T extends RealFieldElement<T>> T offsetFromTAI(final FieldAbsoluteDate<T> date) {
  121.         final int offsetIndex = findOffsetIndex(date.toAbsoluteDate());
  122.         if (offsetIndex < 0) {
  123.             // the date is before the first known leap
  124.             return date.getField().getZero();
  125.         } else {
  126.             return offsets[offsetIndex].getOffset(date).negate();
  127.         }
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public double offsetToTAI(final DateComponents date,
  132.                               final TimeComponents time) {

  133.         // take offset from local time into account, but ignoring seconds,
  134.         // so when we parse an hour like 23:59:60.5 during leap seconds introduction,
  135.         // we do not jump to next day
  136.         final int minuteInDay = time.getHour() * 60 + time.getMinute() - time.getMinutesFromUTC();
  137.         final int correction  = minuteInDay < 0 ? (minuteInDay - 1439) / 1440 : minuteInDay / 1440;

  138.         // find close neighbors, assuming date in TAI, i.e a date earlier than real UTC date
  139.         final int mjd = date.getMJD() + correction;
  140.         final UTCTAIOffset offset = findOffset(mjd);
  141.         if (offset == null) {
  142.             // the date is before the first known leap
  143.             return 0;
  144.         } else {
  145.             return offset.getOffset(date, time);
  146.         }

  147.     }

  148.     /** {@inheritDoc} */
  149.     public String getName() {
  150.         return "UTC";
  151.     }

  152.     /** {@inheritDoc} */
  153.     public String toString() {
  154.         return getName();
  155.     }

  156.     /** Get the date of the first known leap second.
  157.      * @return date of the first known leap second
  158.      */
  159.     public AbsoluteDate getFirstKnownLeapSecond() {
  160.         return offsets[0].getDate();
  161.     }

  162.     /** Get the date of the last known leap second.
  163.      * @return date of the last known leap second
  164.      */
  165.     public AbsoluteDate getLastKnownLeapSecond() {
  166.         return offsets[offsets.length - 1].getDate();
  167.     }

  168.     /** {@inheritDoc} */
  169.     @Override
  170.     public boolean insideLeap(final AbsoluteDate date) {
  171.         final int offsetIndex = findOffsetIndex(date);
  172.         if (offsetIndex < 0) {
  173.             // the date is before the first known leap
  174.             return false;
  175.         } else {
  176.             return date.compareTo(offsets[offsetIndex].getValidityStart()) < 0;
  177.         }
  178.     }

  179.     /** {@inheritDoc} */
  180.     @Override
  181.     public <T extends RealFieldElement<T>> boolean insideLeap(final FieldAbsoluteDate<T> date) {
  182.         return insideLeap(date.toAbsoluteDate());
  183.     }

  184.     /** {@inheritDoc} */
  185.     @Override
  186.     public int minuteDuration(final AbsoluteDate date) {
  187.         final int offsetIndex = findOffsetIndex(date);
  188.         if (offsetIndex < 0) {
  189.             // the date is before the first known leap
  190.             return 60;
  191.         } else {
  192.             if (date.compareTo(offsets[offsetIndex].getValidityStart()) < 0) {
  193.                 // the date is during the leap itself
  194.                 return 61;
  195.             } else {
  196.                 // the date is after a leap, but it may be just before the next one
  197.                 if (offsetIndex + 1 < offsets.length &&
  198.                     offsets[offsetIndex + 1].getDate().durationFrom(date) <= 60.0) {
  199.                     // the next leap will start in one minute, it will extend the current minute
  200.                     return 61;
  201.                 } else {
  202.                     // no leap is expected within the next minute
  203.                     return 60;
  204.                 }
  205.             }
  206.         }
  207.     }

  208.     /** {@inheritDoc} */
  209.     @Override
  210.     public <T extends RealFieldElement<T>> int minuteDuration(final FieldAbsoluteDate<T> date) {
  211.         return minuteDuration(date.toAbsoluteDate());
  212.     }

  213.     /** {@inheritDoc} */
  214.     @Override
  215.     public double getLeap(final AbsoluteDate date) {
  216.         final int offsetIndex = findOffsetIndex(date);
  217.         if (offsetIndex < 0) {
  218.             // the date is before the first known leap
  219.             return 0;
  220.         } else {
  221.             return offsets[offsetIndex].getLeap();
  222.         }
  223.     }

  224.     /** {@inheritDoc} */
  225.     @Override
  226.     public <T extends RealFieldElement<T>> T getLeap(final FieldAbsoluteDate<T> date) {
  227.         return date.getField().getZero().add(getLeap(date.toAbsoluteDate()));
  228.     }

  229.     /** Find the index of the offset valid at some date.
  230.      * @param date date at which offset is requested
  231.      * @return index of the offset valid at this date, or -1 if date is before first offset.
  232.      */
  233.     private int findOffsetIndex(final AbsoluteDate date) {
  234.         int inf = 0;
  235.         int sup = offsets.length;
  236.         while (sup - inf > 1) {
  237.             final int middle = (inf + sup) >>> 1;
  238.             if (date.compareTo(offsets[middle].getDate()) < 0) {
  239.                 sup = middle;
  240.             } else {
  241.                 inf = middle;
  242.             }
  243.         }
  244.         if (sup == offsets.length) {
  245.             // the date is after the last known leap second
  246.             return offsets.length - 1;
  247.         } else if (date.compareTo(offsets[inf].getDate()) < 0) {
  248.             // the date is before the first known leap
  249.             return -1;
  250.         } else {
  251.             return inf;
  252.         }
  253.     }

  254.     /** Find the offset valid at some date.
  255.      * @param mjd Modified Julian Day of the date at which offset is requested
  256.      * @return offset valid at this date, or null if date is before first offset.
  257.      */
  258.     private UTCTAIOffset findOffset(final int mjd) {
  259.         int inf = 0;
  260.         int sup = offsets.length;
  261.         while (sup - inf > 1) {
  262.             final int middle = (inf + sup) >>> 1;
  263.             if (mjd < offsets[middle].getMJD()) {
  264.                 sup = middle;
  265.             } else {
  266.                 inf = middle;
  267.             }
  268.         }
  269.         if (sup == offsets.length) {
  270.             // the date is after the last known leap second
  271.             return offsets[offsets.length - 1];
  272.         } else if (mjd < offsets[inf].getMJD()) {
  273.             // the date is before the first known leap
  274.             return null;
  275.         } else {
  276.             return offsets[inf];
  277.         }
  278.     }

  279.     /** Replace the instance with a data transfer object for serialization.
  280.      * @return data transfer object that will be serialized
  281.      */
  282.     private Object writeReplace() {
  283.         return new DataTransferObject();
  284.     }

  285.     /** Internal class used only for serialization. */
  286.     private static class DataTransferObject implements Serializable {

  287.         /** Serializable UID. */
  288.         private static final long serialVersionUID = 20131209L;

  289.         /** Replace the deserialized data transfer object with a {@link UTCScale}.
  290.          * @return replacement {@link UTCScale}
  291.          */
  292.         private Object readResolve() {
  293.             try {
  294.                 return TimeScalesFactory.getUTC();
  295.             } catch (OrekitException oe) {
  296.                 throw new OrekitInternalError(oe);
  297.             }
  298.         }

  299.     }

  300. }