UTCScale.java

  1. /* Copyright 2002-2020 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. import java.io.Serializable;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Comparator;
  22. import java.util.List;

  23. import org.hipparchus.RealFieldElement;
  24. import org.orekit.annotation.DefaultDataContext;
  25. import org.orekit.data.DataContext;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitInternalError;
  28. import org.orekit.utils.Constants;

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

  47.     /** Serializable UID. */
  48.     private static final long serialVersionUID = 20150402L;

  49.     /** UTC-TAI offsets. */
  50.     private UTCTAIOffset[] offsets;

  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 tai TAI time scale this UTC time scale references.
  56.      * @param offsets UTC-TAI offsets
  57.      */
  58.     UTCScale(final TimeScale tai, final Collection<? extends OffsetModel> offsets) {
  59.         // copy input so the original list is unmodified
  60.         final List<OffsetModel> offsetModels = new ArrayList<>(offsets);
  61.         offsetModels.sort(Comparator.comparing(OffsetModel::getStart));
  62.         if (offsetModels.get(0).getStart().getYear() > 1968) {
  63.             // the pre-1972 linear offsets are missing, add them manually
  64.             // excerpt from UTC-TAI.history file:
  65.             //  1961  Jan.  1 - 1961  Aug.  1     1.422 818 0s + (MJD - 37 300) x 0.001 296s
  66.             //        Aug.  1 - 1962  Jan.  1     1.372 818 0s +        ""
  67.             //  1962  Jan.  1 - 1963  Nov.  1     1.845 858 0s + (MJD - 37 665) x 0.001 123 2s
  68.             //  1963  Nov.  1 - 1964  Jan.  1     1.945 858 0s +        ""
  69.             //  1964  Jan.  1 -       April 1     3.240 130 0s + (MJD - 38 761) x 0.001 296s
  70.             //        April 1 -       Sept. 1     3.340 130 0s +        ""
  71.             //        Sept. 1 - 1965  Jan.  1     3.440 130 0s +        ""
  72.             //  1965  Jan.  1 -       March 1     3.540 130 0s +        ""
  73.             //        March 1 -       Jul.  1     3.640 130 0s +        ""
  74.             //        Jul.  1 -       Sept. 1     3.740 130 0s +        ""
  75.             //        Sept. 1 - 1966  Jan.  1     3.840 130 0s +        ""
  76.             //  1966  Jan.  1 - 1968  Feb.  1     4.313 170 0s + (MJD - 39 126) x 0.002 592s
  77.             //  1968  Feb.  1 - 1972  Jan.  1     4.213 170 0s +        ""
  78.             offsetModels.add( 0, new OffsetModel(new DateComponents(1961,  1, 1), 37300, 1.4228180, 0.0012960));
  79.             offsetModels.add( 1, new OffsetModel(new DateComponents(1961,  8, 1), 37300, 1.3728180, 0.0012960));
  80.             offsetModels.add( 2, new OffsetModel(new DateComponents(1962,  1, 1), 37665, 1.8458580, 0.0011232));
  81.             offsetModels.add( 3, new OffsetModel(new DateComponents(1963, 11, 1), 37665, 1.9458580, 0.0011232));
  82.             offsetModels.add( 4, new OffsetModel(new DateComponents(1964,  1, 1), 38761, 3.2401300, 0.0012960));
  83.             offsetModels.add( 5, new OffsetModel(new DateComponents(1964,  4, 1), 38761, 3.3401300, 0.0012960));
  84.             offsetModels.add( 6, new OffsetModel(new DateComponents(1964,  9, 1), 38761, 3.4401300, 0.0012960));
  85.             offsetModels.add( 7, new OffsetModel(new DateComponents(1965,  1, 1), 38761, 3.5401300, 0.0012960));
  86.             offsetModels.add( 8, new OffsetModel(new DateComponents(1965,  3, 1), 38761, 3.6401300, 0.0012960));
  87.             offsetModels.add( 9, new OffsetModel(new DateComponents(1965,  7, 1), 38761, 3.7401300, 0.0012960));
  88.             offsetModels.add(10, new OffsetModel(new DateComponents(1965,  9, 1), 38761, 3.8401300, 0.0012960));
  89.             offsetModels.add(11, new OffsetModel(new DateComponents(1966,  1, 1), 39126, 4.3131700, 0.0025920));
  90.             offsetModels.add(12, new OffsetModel(new DateComponents(1968,  2, 1), 39126, 4.2131700, 0.0025920));
  91.         }

  92.         // create cache
  93.         this.offsets = new UTCTAIOffset[offsetModels.size()];

  94.         UTCTAIOffset previous = null;

  95.         // link the offsets together
  96.         for (int i = 0; i < offsetModels.size(); ++i) {

  97.             final OffsetModel    o      = offsetModels.get(i);
  98.             final DateComponents date   = o.getStart();
  99.             final int            mjdRef = o.getMJDRef();
  100.             final double         offset = o.getOffset();
  101.             final double         slope  = o.getSlope();

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

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

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

  111.             final AbsoluteDate reference = AbsoluteDate.createMJDDate(mjdRef, 0, tai)
  112.                     .shiftedBy(offset);
  113.             previous = new UTCTAIOffset(leapStart, date.getMJD(), leap, offset, mjdRef,
  114.                     normalizedSlope, reference);
  115.             this.offsets[i] = previous;

  116.         }

  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     public double offsetFromTAI(final AbsoluteDate date) {
  121.         final int offsetIndex = findOffsetIndex(date);
  122.         if (offsetIndex < 0) {
  123.             // the date is before the first known leap
  124.             return 0;
  125.         } else {
  126.             return -offsets[offsetIndex].getOffset(date);
  127.         }
  128.     }

  129.     /** {@inheritDoc} */
  130.     @Override
  131.     public <T extends RealFieldElement<T>> T offsetFromTAI(final FieldAbsoluteDate<T> date) {
  132.         final int offsetIndex = findOffsetIndex(date.toAbsoluteDate());
  133.         if (offsetIndex < 0) {
  134.             // the date is before the first known leap
  135.             return date.getField().getZero();
  136.         } else {
  137.             return offsets[offsetIndex].getOffset(date).negate();
  138.         }
  139.     }

  140.     /** {@inheritDoc} */
  141.     @Override
  142.     public double offsetToTAI(final DateComponents date,
  143.                               final TimeComponents time) {

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

  149.         // find close neighbors, assuming date in TAI, i.e a date earlier than real UTC date
  150.         final int mjd = date.getMJD() + correction;
  151.         final UTCTAIOffset offset = findOffset(mjd);
  152.         if (offset == null) {
  153.             // the date is before the first known leap
  154.             return 0;
  155.         } else {
  156.             return offset.getOffset(date, time);
  157.         }

  158.     }

  159.     /** {@inheritDoc} */
  160.     public String getName() {
  161.         return "UTC";
  162.     }

  163.     /** {@inheritDoc} */
  164.     public String toString() {
  165.         return getName();
  166.     }

  167.     /** Get the date of the first known leap second.
  168.      * @return date of the first known leap second
  169.      */
  170.     public AbsoluteDate getFirstKnownLeapSecond() {
  171.         return offsets[0].getDate();
  172.     }

  173.     /** Get the date of the last known leap second.
  174.      * @return date of the last known leap second
  175.      */
  176.     public AbsoluteDate getLastKnownLeapSecond() {
  177.         return offsets[offsets.length - 1].getDate();
  178.     }

  179.     /** {@inheritDoc} */
  180.     @Override
  181.     public boolean insideLeap(final AbsoluteDate date) {
  182.         final int offsetIndex = findOffsetIndex(date);
  183.         if (offsetIndex < 0) {
  184.             // the date is before the first known leap
  185.             return false;
  186.         } else {
  187.             return date.compareTo(offsets[offsetIndex].getValidityStart()) < 0;
  188.         }
  189.     }

  190.     /** {@inheritDoc} */
  191.     @Override
  192.     public <T extends RealFieldElement<T>> boolean insideLeap(final FieldAbsoluteDate<T> date) {
  193.         return insideLeap(date.toAbsoluteDate());
  194.     }

  195.     /** {@inheritDoc} */
  196.     @Override
  197.     public int minuteDuration(final AbsoluteDate date) {
  198.         final int offsetIndex = findOffsetIndex(date);
  199.         if (offsetIndex < 0) {
  200.             // the date is before the first known leap
  201.             return 60;
  202.         } else {
  203.             if (date.compareTo(offsets[offsetIndex].getValidityStart()) < 0) {
  204.                 // the date is during the leap itself
  205.                 return 61;
  206.             } else {
  207.                 // the date is after a leap, but it may be just before the next one
  208.                 if (offsetIndex + 1 < offsets.length &&
  209.                     offsets[offsetIndex + 1].getDate().durationFrom(date) <= 60.0) {
  210.                     // the next leap will start in one minute, it will extend the current minute
  211.                     return 61;
  212.                 } else {
  213.                     // no leap is expected within the next minute
  214.                     return 60;
  215.                 }
  216.             }
  217.         }
  218.     }

  219.     /** {@inheritDoc} */
  220.     @Override
  221.     public <T extends RealFieldElement<T>> int minuteDuration(final FieldAbsoluteDate<T> date) {
  222.         return minuteDuration(date.toAbsoluteDate());
  223.     }

  224.     /** {@inheritDoc} */
  225.     @Override
  226.     public double getLeap(final AbsoluteDate date) {
  227.         final int offsetIndex = findOffsetIndex(date);
  228.         if (offsetIndex < 0) {
  229.             // the date is before the first known leap
  230.             return 0;
  231.         } else {
  232.             return offsets[offsetIndex].getLeap();
  233.         }
  234.     }

  235.     /** {@inheritDoc} */
  236.     @Override
  237.     public <T extends RealFieldElement<T>> T getLeap(final FieldAbsoluteDate<T> date) {
  238.         return date.getField().getZero().add(getLeap(date.toAbsoluteDate()));
  239.     }

  240.     /** Find the index of the offset valid at some date.
  241.      * @param date date at which offset is requested
  242.      * @return index of the offset valid at this date, or -1 if date is before first offset.
  243.      */
  244.     private int findOffsetIndex(final AbsoluteDate date) {
  245.         int inf = 0;
  246.         int sup = offsets.length;
  247.         while (sup - inf > 1) {
  248.             final int middle = (inf + sup) >>> 1;
  249.             if (date.compareTo(offsets[middle].getDate()) < 0) {
  250.                 sup = middle;
  251.             } else {
  252.                 inf = middle;
  253.             }
  254.         }
  255.         if (sup == offsets.length) {
  256.             // the date is after the last known leap second
  257.             return offsets.length - 1;
  258.         } else if (date.compareTo(offsets[inf].getDate()) < 0) {
  259.             // the date is before the first known leap
  260.             return -1;
  261.         } else {
  262.             return inf;
  263.         }
  264.     }

  265.     /** Find the offset valid at some date.
  266.      * @param mjd Modified Julian Day of the date at which offset is requested
  267.      * @return offset valid at this date, or null if date is before first offset.
  268.      */
  269.     private UTCTAIOffset findOffset(final int mjd) {
  270.         int inf = 0;
  271.         int sup = offsets.length;
  272.         while (sup - inf > 1) {
  273.             final int middle = (inf + sup) >>> 1;
  274.             if (mjd < offsets[middle].getMJD()) {
  275.                 sup = middle;
  276.             } else {
  277.                 inf = middle;
  278.             }
  279.         }
  280.         if (sup == offsets.length) {
  281.             // the date is after the last known leap second
  282.             return offsets[offsets.length - 1];
  283.         } else if (mjd < offsets[inf].getMJD()) {
  284.             // the date is before the first known leap
  285.             return null;
  286.         } else {
  287.             return offsets[inf];
  288.         }
  289.     }

  290.     /** Replace the instance with a data transfer object for serialization.
  291.      * @return data transfer object that will be serialized
  292.      */
  293.     @DefaultDataContext
  294.     private Object writeReplace() {
  295.         return new DataTransferObject();
  296.     }

  297.     /** Internal class used only for serialization. */
  298.     @DefaultDataContext
  299.     private static class DataTransferObject implements Serializable {

  300.         /** Serializable UID. */
  301.         private static final long serialVersionUID = 20131209L;

  302.         /** Replace the deserialized data transfer object with a {@link UTCScale}.
  303.          * @return replacement {@link UTCScale}
  304.          */
  305.         private Object readResolve() {
  306.             try {
  307.                 return DataContext.getDefault().getTimeScales().getUTC();
  308.             } catch (OrekitException oe) {
  309.                 throw new OrekitInternalError(oe);
  310.             }
  311.         }

  312.     }

  313. }