AbsoluteDate.java

  1. /* Copyright 2002-2024 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.time.Instant;
  20. import java.time.LocalDateTime;
  21. import java.time.ZoneOffset;
  22. import java.time.format.DateTimeFormatter;
  23. import java.util.Date;
  24. import java.util.TimeZone;

  25. import java.util.concurrent.TimeUnit;
  26. import org.hipparchus.util.FastMath;
  27. import org.hipparchus.util.MathUtils;
  28. import org.hipparchus.util.MathUtils.SumAndResidual;
  29. import org.orekit.annotation.DefaultDataContext;
  30. import org.orekit.data.DataContext;
  31. import org.orekit.errors.OrekitIllegalArgumentException;
  32. import org.orekit.utils.Constants;


  33. /** This class represents a specific instant in time.

  34.  * <p>Instances of this class are considered to be absolute in the sense
  35.  * that each one represent the occurrence of some event and can be compared
  36.  * to other instances or located in <em>any</em> {@link TimeScale time scale}. In
  37.  * other words the different locations of an event with respect to two different
  38.  * time scales (say {@link TAIScale TAI} and {@link UTCScale UTC} for example) are
  39.  * simply different perspective related to a single object. Only one
  40.  * <code>AbsoluteDate</code> instance is needed, both representations being available
  41.  * from this single instance by specifying the time scales as parameter when calling
  42.  * the ad-hoc methods.</p>
  43.  *
  44.  * <p>Since an instance is not bound to a specific time-scale, all methods related
  45.  * to the location of the date within some time scale require to provide the time
  46.  * scale as an argument. It is therefore possible to define a date in one time scale
  47.  * and to use it in another one. An example of such use is to read a date from a file
  48.  * in UTC and write it in another file in TAI. This can be done as follows:</p>
  49.  * <pre>
  50.  *   DateTimeComponents utcComponents = readNextDate();
  51.  *   AbsoluteDate date = new AbsoluteDate(utcComponents, TimeScalesFactory.getUTC());
  52.  *   writeNextDate(date.getComponents(TimeScalesFactory.getTAI()));
  53.  * </pre>
  54.  *
  55.  * <p>Two complementary views are available:</p>
  56.  * <ul>
  57.  *   <li><p>location view (mainly for input/output or conversions)</p>
  58.  *   <p>locations represent the coordinate of one event with respect to a
  59.  *   {@link TimeScale time scale}. The related methods are {@link
  60.  *   #AbsoluteDate(DateComponents, TimeComponents, TimeScale)}, {@link
  61.  *   #AbsoluteDate(int, int, int, int, int, double, TimeScale)}, {@link
  62.  *   #AbsoluteDate(int, int, int, TimeScale)}, {@link #AbsoluteDate(Date,
  63.  *   TimeScale)}, {@link #parseCCSDSCalendarSegmentedTimeCode(byte, byte[])},
  64.  *   {@link #toDate(TimeScale)}, {@link #toString(TimeScale) toString(timeScale)},
  65.  *   {@link #toString()}, and {@link #timeScalesOffset}.</p>
  66.  *   </li>
  67.  *   <li><p>offset view (mainly for physical computation)</p>
  68.  *   <p>offsets represent either the flow of time between two events
  69.  *   (two instances of the class) or durations. They are counted in seconds,
  70.  *   are continuous and could be measured using only a virtually perfect stopwatch.
  71.  *   The related methods are {@link #AbsoluteDate(AbsoluteDate, double)},
  72.  *   {@link #parseCCSDSUnsegmentedTimeCode(byte, byte, byte[], AbsoluteDate)},
  73.  *   {@link #parseCCSDSDaySegmentedTimeCode(byte, byte[], DateComponents)},
  74.  *   {@link #durationFrom(AbsoluteDate)}, {@link #compareTo(AbsoluteDate)}, {@link #equals(Object)}
  75.  *   and {@link #hashCode()}.</p>
  76.  *   </li>
  77.  * </ul>
  78.  * <p>
  79.  * A few reference epochs which are commonly used in space systems have been defined. These
  80.  * epochs can be used as the basis for offset computation. The supported epochs are:
  81.  * {@link #JULIAN_EPOCH}, {@link #MODIFIED_JULIAN_EPOCH}, {@link #FIFTIES_EPOCH},
  82.  * {@link #CCSDS_EPOCH}, {@link #GALILEO_EPOCH}, {@link #GPS_EPOCH}, {@link #QZSS_EPOCH}
  83.  * {@link #J2000_EPOCH}, {@link #JAVA_EPOCH}.
  84.  * There are also two factory methods {@link #createJulianEpoch(double)}
  85.  * and {@link #createBesselianEpoch(double)} that can be used to compute other reference
  86.  * epochs like J1900.0 or B1950.0.
  87.  * In addition to these reference epochs, two other constants are defined for convenience:
  88.  * {@link #PAST_INFINITY} and {@link #FUTURE_INFINITY}, which can be used either as dummy
  89.  * dates when a date is not yet initialized, or for initialization of loops searching for
  90.  * a min or max date.
  91.  * </p>
  92.  * <p>
  93.  * Instances of the <code>AbsoluteDate</code> class are guaranteed to be immutable.
  94.  * </p>
  95.  * @author Luc Maisonobe
  96.  * @author Evan Ward
  97.  * @see TimeScale
  98.  * @see TimeStamped
  99.  * @see ChronologicalComparator
  100.  */
  101. public class AbsoluteDate
  102.     implements TimeStamped, TimeShiftable<AbsoluteDate>, Comparable<AbsoluteDate>, Serializable {

  103.     /** Reference epoch for julian dates: -4712-01-01T12:00:00 Terrestrial Time.
  104.      * <p>Both <code>java.util.Date</code> and {@link DateComponents} classes
  105.      * follow the astronomical conventions and consider a year 0 between
  106.      * years -1 and +1, hence this reference date lies in year -4712 and not
  107.      * in year -4713 as can be seen in other documents or programs that obey
  108.      * a different convention (for example the <code>convcal</code> utility).</p>
  109.      *
  110.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  111.      *
  112.      * @see TimeScales#getJulianEpoch()
  113.      */
  114.     @DefaultDataContext
  115.     public static final AbsoluteDate JULIAN_EPOCH =
  116.             DataContext.getDefault().getTimeScales().getJulianEpoch();

  117.     /** Reference epoch for modified julian dates: 1858-11-17T00:00:00 Terrestrial Time.
  118.      *
  119.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  120.      *
  121.      * @see TimeScales#getModifiedJulianEpoch()
  122.      */
  123.     @DefaultDataContext
  124.     public static final AbsoluteDate MODIFIED_JULIAN_EPOCH =
  125.             DataContext.getDefault().getTimeScales().getModifiedJulianEpoch();

  126.     /** Reference epoch for 1950 dates: 1950-01-01T00:00:00 Terrestrial Time.
  127.      *
  128.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  129.      *
  130.      * @see TimeScales#getFiftiesEpoch()
  131.      */
  132.     @DefaultDataContext
  133.     public static final AbsoluteDate FIFTIES_EPOCH =
  134.             DataContext.getDefault().getTimeScales().getFiftiesEpoch();

  135.     /** Reference epoch for CCSDS Time Code Format (CCSDS 301.0-B-4):
  136.      * 1958-01-01T00:00:00 International Atomic Time (<em>not</em> UTC).
  137.      *
  138.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  139.      *
  140.      * @see TimeScales#getCcsdsEpoch()
  141.      */
  142.     @DefaultDataContext
  143.     public static final AbsoluteDate CCSDS_EPOCH =
  144.             DataContext.getDefault().getTimeScales().getCcsdsEpoch();

  145.     /** Reference epoch for Galileo System Time: 1999-08-22T00:00:00 GST.
  146.      *
  147.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  148.      *
  149.      * @see TimeScales#getGalileoEpoch()
  150.      */
  151.     @DefaultDataContext
  152.     public static final AbsoluteDate GALILEO_EPOCH =
  153.             DataContext.getDefault().getTimeScales().getGalileoEpoch();

  154.     /** Reference epoch for GPS weeks: 1980-01-06T00:00:00 GPS time.
  155.      *
  156.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  157.      *
  158.      * @see TimeScales#getGpsEpoch()
  159.      */
  160.     @DefaultDataContext
  161.     public static final AbsoluteDate GPS_EPOCH =
  162.             DataContext.getDefault().getTimeScales().getGpsEpoch();

  163.     /** Reference epoch for QZSS weeks: 1980-01-06T00:00:00 QZSS time.
  164.      *
  165.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  166.      *
  167.      * @see TimeScales#getQzssEpoch()
  168.      */
  169.     @DefaultDataContext
  170.     public static final AbsoluteDate QZSS_EPOCH =
  171.             DataContext.getDefault().getTimeScales().getQzssEpoch();

  172.     /** Reference epoch for IRNSS weeks: 1999-08-22T00:00:00 IRNSS time.
  173.      *
  174.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  175.      *
  176.      * @see TimeScales#getIrnssEpoch()
  177.      */
  178.     @DefaultDataContext
  179.     public static final AbsoluteDate IRNSS_EPOCH =
  180.             DataContext.getDefault().getTimeScales().getIrnssEpoch();

  181.     /** Reference epoch for BeiDou weeks: 2006-01-01T00:00:00 UTC.
  182.      *
  183.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  184.      *
  185.      * @see TimeScales#getBeidouEpoch()
  186.      */
  187.     @DefaultDataContext
  188.     public static final AbsoluteDate BEIDOU_EPOCH =
  189.             DataContext.getDefault().getTimeScales().getBeidouEpoch();

  190.     /** Reference epoch for GLONASS four-year interval number: 1996-01-01T00:00:00 GLONASS time.
  191.      * <p>By convention, TGLONASS = UTC + 3 hours.</p>
  192.      *
  193.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  194.      *
  195.      * @see TimeScales#getGlonassEpoch()
  196.      */
  197.     @DefaultDataContext
  198.     public static final AbsoluteDate GLONASS_EPOCH =
  199.             DataContext.getDefault().getTimeScales().getGlonassEpoch();

  200.     /** J2000.0 Reference epoch: 2000-01-01T12:00:00 Terrestrial Time (<em>not</em> UTC).
  201.      *
  202.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  203.      *
  204.      * @see #createJulianEpoch(double)
  205.      * @see #createBesselianEpoch(double)
  206.      * @see TimeScales#getJ2000Epoch()
  207.      */
  208.     @DefaultDataContext
  209.     public static final AbsoluteDate J2000_EPOCH = // TODO
  210.             DataContext.getDefault().getTimeScales().getJ2000Epoch();

  211.     /** Java Reference epoch: 1970-01-01T00:00:00 Universal Time Coordinate.
  212.      * <p>
  213.      * Between 1968-02-01 and 1972-01-01, UTC-TAI = 4.213 170 0s + (MJD - 39 126) x 0.002 592s.
  214.      * As on 1970-01-01 MJD = 40587, UTC-TAI = 8.000082s
  215.      * </p>
  216.      *
  217.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  218.      *
  219.      * @see TimeScales#getJavaEpoch()
  220.      */
  221.     @DefaultDataContext
  222.     public static final AbsoluteDate JAVA_EPOCH =
  223.             DataContext.getDefault().getTimeScales().getJavaEpoch();

  224.     /**
  225.      * An arbitrary finite date. Uses when a non-null date is needed but its value doesn't
  226.      * matter.
  227.      */
  228.     public static final AbsoluteDate ARBITRARY_EPOCH = new AbsoluteDate(0, 0);

  229.     /** Dummy date at infinity in the past direction.
  230.      * @see TimeScales#getPastInfinity()
  231.      */
  232.     public static final AbsoluteDate PAST_INFINITY = ARBITRARY_EPOCH.shiftedBy(Double.NEGATIVE_INFINITY);

  233.     /** Dummy date at infinity in the future direction.
  234.      * @see TimeScales#getFutureInfinity()
  235.      */
  236.     public static final AbsoluteDate FUTURE_INFINITY = ARBITRARY_EPOCH.shiftedBy(Double.POSITIVE_INFINITY);

  237.     /** Serializable UID. */
  238.     private static final long serialVersionUID = 617061803741806846L;

  239.     /** Reference epoch in seconds from 2000-01-01T12:00:00 TAI.
  240.      * <p>Beware, it is not {@link #J2000_EPOCH} since it is in TAI and not in TT.</p> */
  241.     private final long epoch;

  242.     /** Offset from the reference epoch in seconds. */
  243.     private final double offset;

  244.     /** Create an instance with a default value ({@link #J2000_EPOCH}).
  245.      *
  246.      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
  247.      *
  248.      * @see #AbsoluteDate(DateTimeComponents, TimeScale)
  249.      */
  250.     @DefaultDataContext
  251.     public AbsoluteDate() {
  252.         epoch  = J2000_EPOCH.epoch;
  253.         offset = J2000_EPOCH.offset;
  254.     }

  255.     /** Build an instance from a location (parsed from a string) in a {@link TimeScale time scale}.
  256.      * <p>
  257.      * The supported formats for location are mainly the ones defined in ISO-8601 standard,
  258.      * the exact subset is explained in {@link DateTimeComponents#parseDateTime(String)},
  259.      * {@link DateComponents#parseDate(String)} and {@link TimeComponents#parseTime(String)}.
  260.      * </p>
  261.      * <p>
  262.      * As CCSDS ASCII calendar segmented time code is a trimmed down version of ISO-8601,
  263.      * it is also supported by this constructor.
  264.      * </p>
  265.      * @param location location in the time scale, must be in a supported format
  266.      * @param timeScale time scale
  267.      * @exception IllegalArgumentException if location string is not in a supported format
  268.      */
  269.     public AbsoluteDate(final String location, final TimeScale timeScale) {
  270.         this(DateTimeComponents.parseDateTime(location), timeScale);
  271.     }

  272.     /** Build an instance from a location in a {@link TimeScale time scale}.
  273.      * @param location location in the time scale
  274.      * @param timeScale time scale
  275.      */
  276.     public AbsoluteDate(final DateTimeComponents location, final TimeScale timeScale) {
  277.         this(location.getDate(), location.getTime(), timeScale);
  278.     }

  279.     /** Build an instance from a location in a {@link TimeScale time scale}.
  280.      * @param date date location in the time scale
  281.      * @param time time location in the time scale
  282.      * @param timeScale time scale
  283.      */
  284.     public AbsoluteDate(final DateComponents date, final TimeComponents time,
  285.                         final TimeScale timeScale) {

  286.         final double seconds  = time.getSecond();
  287.         final double tsOffset = timeScale.offsetToTAI(date, time);

  288.         // Use 2Sum for high precision.
  289.         final SumAndResidual sumAndResidual = MathUtils.twoSum(seconds, tsOffset);
  290.         final long dl = (long) FastMath.floor(sumAndResidual.getSum());
  291.         final double regularOffset = (sumAndResidual.getSum() - dl) + sumAndResidual.getResidual();

  292.         if (regularOffset >= 0) {
  293.             // regular case, the offset is between 0.0 and 1.0
  294.             offset = regularOffset;
  295.             epoch  = 60L * ((date.getJ2000Day() * 24L + time.getHour()) * 60L +
  296.                             time.getMinute() - time.getMinutesFromUTC() - 720L) + dl;
  297.         } else {
  298.             // very rare case, the offset is just before a whole second
  299.             // we will loose some bits of accuracy when adding 1 second
  300.             // but this will ensure the offset remains in the [0.0; 1.0] interval
  301.             offset = 1.0 + regularOffset;
  302.             epoch  = 60L * ((date.getJ2000Day() * 24L + time.getHour()) * 60L +
  303.                             time.getMinute() - time.getMinutesFromUTC() - 720L) + dl - 1;
  304.         }

  305.     }

  306.     /** Build an instance from a location in a {@link TimeScale time scale}.
  307.      * @param year year number (may be 0 or negative for BC years)
  308.      * @param month month number from 1 to 12
  309.      * @param day day number from 1 to 31
  310.      * @param hour hour number from 0 to 23
  311.      * @param minute minute number from 0 to 59
  312.      * @param second second number from 0.0 to 60.0 (excluded)
  313.      * @param timeScale time scale
  314.      * @exception IllegalArgumentException if inconsistent arguments
  315.      * are given (parameters out of range)
  316.      */
  317.     public AbsoluteDate(final int year, final int month, final int day,
  318.                         final int hour, final int minute, final double second,
  319.                         final TimeScale timeScale) throws IllegalArgumentException {
  320.         this(new DateComponents(year, month, day), new TimeComponents(hour, minute, second), timeScale);
  321.     }

  322.     /** Build an instance from a location in a {@link TimeScale time scale}.
  323.      * @param year year number (may be 0 or negative for BC years)
  324.      * @param month month enumerate
  325.      * @param day day number from 1 to 31
  326.      * @param hour hour number from 0 to 23
  327.      * @param minute minute number from 0 to 59
  328.      * @param second second number from 0.0 to 60.0 (excluded)
  329.      * @param timeScale time scale
  330.      * @exception IllegalArgumentException if inconsistent arguments
  331.      * are given (parameters out of range)
  332.      */
  333.     public AbsoluteDate(final int year, final Month month, final int day,
  334.                         final int hour, final int minute, final double second,
  335.                         final TimeScale timeScale) throws IllegalArgumentException {
  336.         this(new DateComponents(year, month, day), new TimeComponents(hour, minute, second), timeScale);
  337.     }

  338.     /** Build an instance from a location in a {@link TimeScale time scale}.
  339.      * <p>The hour is set to 00:00:00.000.</p>
  340.      * @param date date location in the time scale
  341.      * @param timeScale time scale
  342.      * @exception IllegalArgumentException if inconsistent arguments
  343.      * are given (parameters out of range)
  344.      */
  345.     public AbsoluteDate(final DateComponents date, final TimeScale timeScale)
  346.         throws IllegalArgumentException {
  347.         this(date, TimeComponents.H00, timeScale);
  348.     }

  349.     /** Build an instance from a location in a {@link TimeScale time scale}.
  350.      * <p>The hour is set to 00:00:00.000.</p>
  351.      * @param year year number (may be 0 or negative for BC years)
  352.      * @param month month number from 1 to 12
  353.      * @param day day number from 1 to 31
  354.      * @param timeScale time scale
  355.      * @exception IllegalArgumentException if inconsistent arguments
  356.      * are given (parameters out of range)
  357.      */
  358.     public AbsoluteDate(final int year, final int month, final int day,
  359.                         final TimeScale timeScale) throws IllegalArgumentException {
  360.         this(new DateComponents(year, month, day), TimeComponents.H00, timeScale);
  361.     }

  362.     /** Build an instance from a location in a {@link TimeScale time scale}.
  363.      * <p>The hour is set to 00:00:00.000.</p>
  364.      * @param year year number (may be 0 or negative for BC years)
  365.      * @param month month enumerate
  366.      * @param day day number from 1 to 31
  367.      * @param timeScale time scale
  368.      * @exception IllegalArgumentException if inconsistent arguments
  369.      * are given (parameters out of range)
  370.      */
  371.     public AbsoluteDate(final int year, final Month month, final int day,
  372.                         final TimeScale timeScale) throws IllegalArgumentException {
  373.         this(new DateComponents(year, month, day), TimeComponents.H00, timeScale);
  374.     }

  375.     /** Build an instance from a location in a {@link TimeScale time scale}.
  376.      * @param location location in the time scale
  377.      * @param timeScale time scale
  378.      */
  379.     public AbsoluteDate(final Date location, final TimeScale timeScale) {
  380.         this(new DateComponents(DateComponents.JAVA_EPOCH,
  381.                                 (int) (location.getTime() / 86400000L)),
  382.                                  millisToTimeComponents((int) (location.getTime() % 86400000L)),
  383.              timeScale);
  384.     }

  385.     /** Build an instance from an {@link Instant instant} in a {@link TimeScale time scale}.
  386.      *
  387.      * @deprecated Use {@link AbsoluteDate#AbsoluteDate(Instant, UTCScale)} or {@link AbsoluteDate#AbsoluteDate(Instant)} instead
  388.      * @param instant instant in the time scale
  389.      * @param timeScale time scale
  390.      * @since 12.0
  391.      */
  392.     @Deprecated
  393.     public AbsoluteDate(final Instant instant, final TimeScale timeScale) {
  394.         this(new DateComponents(DateComponents.JAVA_EPOCH,
  395.                                 (int) (instant.getEpochSecond() / 86400L)),
  396.              instantToTimeComponents(instant),
  397.              timeScale);
  398.     }

  399.     /** Build an instance from an {@link Instant instant} in utc time scale.
  400.      * @param instant instant in the time scale
  401.      * @since 12.1
  402.      */
  403.     @DefaultDataContext
  404.     public AbsoluteDate(final Instant instant) {
  405.         this(instant, TimeScalesFactory.getUTC());
  406.     }

  407.     /** Build an instance from an {@link Instant instant} in the {@link UTCScale time scale}.
  408.      * @param instant instant in the time scale
  409.      * @param utcScale utc time scale
  410.      * @since 12.1
  411.      */
  412.     public AbsoluteDate(final Instant instant, final UTCScale utcScale) {
  413.         this(new DateComponents(DateComponents.JAVA_EPOCH,
  414.                 (int) (instant.getEpochSecond() / 86400l)),
  415.             instantToTimeComponents(instant),
  416.             utcScale);
  417.     }

  418.     /** Build an instance from an elapsed duration since to another instant.
  419.      * <p>It is important to note that the elapsed duration is <em>not</em>
  420.      * the difference between two readings on a time scale. As an example,
  421.      * the duration between the two instants leading to the readings
  422.      * 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the {@link UTCScale UTC}
  423.      * time scale is <em>not</em> 1 second, but a stop watch would have measured
  424.      * an elapsed duration of 2 seconds between these two instances because a leap
  425.      * second was introduced at the end of 2005 in this time scale.</p>
  426.      * <p>This constructor is the reverse of the {@link #durationFrom(AbsoluteDate)}
  427.      * method.</p>
  428.      * @param since start instant of the measured duration
  429.      * @param elapsedDuration physically elapsed duration from the <code>since</code>
  430.      * instant, as measured in a regular time scale
  431.      * @see #durationFrom(AbsoluteDate)
  432.      */
  433.     public AbsoluteDate(final AbsoluteDate since, final double elapsedDuration) {
  434.         // Use 2Sum for high precision.
  435.         final SumAndResidual sumAndResidual = MathUtils.twoSum(since.offset, elapsedDuration);
  436.         if (Double.isInfinite(sumAndResidual.getSum())) {
  437.             offset = sumAndResidual.getSum();
  438.             epoch  = (sumAndResidual.getSum() < 0) ? Long.MIN_VALUE : Long.MAX_VALUE;
  439.         } else {
  440.             final long dl = (long) FastMath.floor(sumAndResidual.getSum());
  441.             final double regularOffset = (sumAndResidual.getSum() - dl) + sumAndResidual.getResidual();
  442.             if (regularOffset >= 0) {
  443.                 // regular case, the offset is between 0.0 and 1.0
  444.                 offset = regularOffset;
  445.                 epoch  = since.epoch + dl;
  446.             } else {
  447.                 // very rare case, the offset is just before a whole second
  448.                 // we will loose some bits of accuracy when adding 1 second
  449.                 // but this will ensure the offset remains in the [0.0; 1.0] interval
  450.                 offset = 1.0 + regularOffset;
  451.                 epoch  = since.epoch + dl - 1;
  452.             }
  453.         }
  454.     }

  455.     /** Build an instance from an elapsed duration since to another instant.
  456.      * <p>It is important to note that the elapsed duration is <em>not</em>
  457.      * the difference between two readings on a time scale. As an example,
  458.      * the duration between the two instants leading to the readings
  459.      * 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the {@link UTCScale UTC}
  460.      * time scale is <em>not</em> 1 second, but a stop watch would have measured
  461.      * an elapsed duration of 2 seconds between these two instances because a leap
  462.      * second was introduced at the end of 2005 in this time scale.</p>
  463.      * <p>This constructor is the reverse of the {@link #durationFrom(AbsoluteDate, TimeUnit)}
  464.      * method.</p>
  465.      * @param since start instant of the measured duration
  466.      * @param elapsedDuration physically elapsed duration from the <code>since</code>
  467.      * instant, as measured in a regular time scale
  468.      * @param timeUnit {@link TimeUnit} of the elapsedDuration
  469.      * @see #durationFrom(AbsoluteDate, TimeUnit)
  470.      * @since 12.1
  471.      */
  472.     public AbsoluteDate(final AbsoluteDate since, final long elapsedDuration, final TimeUnit timeUnit) {
  473.         final long elapsedDurationNanoseconds = TimeUnit.NANOSECONDS.convert(elapsedDuration, timeUnit);
  474.         final long deltaEpoch = elapsedDurationNanoseconds / TimeUnit.SECONDS.toNanos(1);
  475.         final double deltaOffset = (elapsedDurationNanoseconds - (deltaEpoch * TimeUnit.SECONDS.toNanos(1))) / (double) TimeUnit.SECONDS.toNanos(1);
  476.         final double newOffset = since.offset + deltaOffset;
  477.         if (newOffset >= 1.0) {
  478.             // newOffset is in [1.0, 2.0]
  479.             epoch = since.epoch + deltaEpoch + 1L;
  480.             offset = newOffset - 1.0;
  481.         } else if (newOffset < 0) {
  482.             epoch = since.epoch + deltaEpoch - 1L;
  483.             offset = 1.0 + newOffset;
  484.         } else {
  485.             epoch = since.epoch + deltaEpoch;
  486.             offset = newOffset;
  487.         }
  488.     }

  489.     /** Build an instance from an apparent clock offset with respect to another
  490.      * instant <em>in the perspective of a specific {@link TimeScale time scale}</em>.
  491.      * <p>It is important to note that the apparent clock offset <em>is</em> the
  492.      * difference between two readings on a time scale and <em>not</em> an elapsed
  493.      * duration. As an example, the apparent clock offset between the two instants
  494.      * leading to the readings 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the
  495.      * {@link UTCScale UTC} time scale is 1 second, but the elapsed duration is 2
  496.      * seconds because a leap second has been introduced at the end of 2005 in this
  497.      * time scale.</p>
  498.      * <p>This constructor is the reverse of the {@link #offsetFrom(AbsoluteDate,
  499.      * TimeScale)} method.</p>
  500.      * @param reference reference instant
  501.      * @param apparentOffset apparent clock offset from the reference instant
  502.      * (difference between two readings in the specified time scale)
  503.      * @param timeScale time scale with respect to which the offset is defined
  504.      * @see #offsetFrom(AbsoluteDate, TimeScale)
  505.      */
  506.     public AbsoluteDate(final AbsoluteDate reference, final double apparentOffset,
  507.                         final TimeScale timeScale) {
  508.         this(new DateTimeComponents(reference.getComponents(timeScale), apparentOffset),
  509.              timeScale);
  510.     }

  511.     /** Build a date from its internal components.
  512.      * <p>
  513.      * This method is reserved for internal used (for example by {@link FieldAbsoluteDate}).
  514.      * </p>
  515.      * @param epoch reference epoch in seconds from 2000-01-01T12:00:00 TAI.
  516.      * (beware, it is not {@link #J2000_EPOCH} since it is in TAI and not in TT)
  517.      * @param offset offset from the reference epoch in seconds (must be
  518.      * between 0.0 included and 1.0 excluded)
  519.      * @since 9.0
  520.      */
  521.     AbsoluteDate(final long epoch, final double offset) {
  522.         this.epoch  = epoch;
  523.         this.offset = offset;
  524.     }

  525.     /** Extract time components from a number of milliseconds within the day.
  526.      * @param millisInDay number of milliseconds within the day
  527.      * @return time components
  528.      */
  529.     private static TimeComponents millisToTimeComponents(final int millisInDay) {
  530.         return new TimeComponents(millisInDay / 1000, 0.001 * (millisInDay % 1000));
  531.     }

  532.     /** Extract time components from an instant within the day.
  533.      * @param instant instant to extract the number of seconds within the day
  534.      * @return time components
  535.      */
  536.     private static TimeComponents instantToTimeComponents(final Instant instant) {
  537.         final int secInDay = (int) (instant.getEpochSecond() % 86400L);
  538.         return new TimeComponents(secInDay, 1.0e-9 * instant.getNano());
  539.     }

  540.     /** Get the reference epoch in seconds from 2000-01-01T12:00:00 TAI.
  541.      * <p>
  542.      * This method is reserved for internal used (for example by {@link FieldAbsoluteDate}).
  543.      * </p>
  544.      * <p>
  545.      * Beware, it is not {@link #J2000_EPOCH} since it is in TAI and not in TT.
  546.      * </p>
  547.      * @return reference epoch in seconds from 2000-01-01T12:00:00 TAI
  548.      * @since 9.0
  549.      */
  550.     long getEpoch() {
  551.         return epoch;
  552.     }

  553.     /** Get the offset from the reference epoch in seconds.
  554.      * <p>
  555.      * This method is reserved for internal used (for example by {@link FieldAbsoluteDate}).
  556.      * </p>
  557.      * @return offset from the reference epoch in seconds
  558.      * @since 9.0
  559.      */
  560.     double getOffset() {
  561.         return offset;
  562.     }

  563.     /** Build an instance from a CCSDS Unsegmented Time Code (CUC).
  564.      * <p>
  565.      * CCSDS Unsegmented Time Code is defined in the blue book:
  566.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  567.      * </p>
  568.      * <p>
  569.      * If the date to be parsed is formatted using version 3 of the standard
  570.      * (CCSDS 301.0-B-3 published in 2002) or if the extension of the preamble
  571.      * field introduced in version 4 of the standard is not used, then the
  572.      * {@code preambleField2} parameter can be set to 0.
  573.      * </p>
  574.      *
  575.      * <p>This method uses the {@link DataContext#getDefault() default data context} if
  576.      * the CCSDS epoch is used.
  577.      *
  578.      * @param preambleField1 first byte of the field specifying the format, often
  579.      * not transmitted in data interfaces, as it is constant for a given data interface
  580.      * @param preambleField2 second byte of the field specifying the format
  581.      * (added in revision 4 of the CCSDS standard in 2010), often not transmitted in data
  582.      * interfaces, as it is constant for a given data interface (value ignored if presence
  583.      * not signaled in {@code preambleField1})
  584.      * @param timeField byte array containing the time code
  585.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field
  586.      * specifies the {@link #CCSDS_EPOCH CCSDS reference epoch} is used (and hence
  587.      * may be null in this case)
  588.      * @return an instance corresponding to the specified date
  589.      * @see #parseCCSDSUnsegmentedTimeCode(byte, byte, byte[], AbsoluteDate, AbsoluteDate)
  590.      */
  591.     @DefaultDataContext
  592.     public static AbsoluteDate parseCCSDSUnsegmentedTimeCode(final byte preambleField1,
  593.                                                              final byte preambleField2,
  594.                                                              final byte[] timeField,
  595.                                                              final AbsoluteDate agencyDefinedEpoch) {
  596.         return parseCCSDSUnsegmentedTimeCode(preambleField1, preambleField2, timeField,
  597.                 agencyDefinedEpoch,
  598.                 DataContext.getDefault().getTimeScales().getCcsdsEpoch());
  599.     }

  600.     /**
  601.      * Build an instance from a CCSDS Unsegmented Time Code (CUC).
  602.      * <p>
  603.      * CCSDS Unsegmented Time Code is defined in the blue book: CCSDS Time Code Format
  604.      * (CCSDS 301.0-B-4) published in November 2010
  605.      * </p>
  606.      * <p>
  607.      * If the date to be parsed is formatted using version 3 of the standard (CCSDS
  608.      * 301.0-B-3 published in 2002) or if the extension of the preamble field introduced
  609.      * in version 4 of the standard is not used, then the {@code preambleField2} parameter
  610.      * can be set to 0.
  611.      * </p>
  612.      *
  613.      * @param preambleField1     first byte of the field specifying the format, often not
  614.      *                           transmitted in data interfaces, as it is constant for a
  615.      *                           given data interface
  616.      * @param preambleField2     second byte of the field specifying the format (added in
  617.      *                           revision 4 of the CCSDS standard in 2010), often not
  618.      *                           transmitted in data interfaces, as it is constant for a
  619.      *                           given data interface (value ignored if presence not
  620.      *                           signaled in {@code preambleField1})
  621.      * @param timeField          byte array containing the time code
  622.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field specifies
  623.      *                           the {@link #CCSDS_EPOCH CCSDS reference epoch} is used
  624.      *                           (and hence may be null in this case)
  625.      * @param ccsdsEpoch         reference epoch, ignored if the preamble field specifies
  626.      *                           the agency epoch is used.
  627.      * @return an instance corresponding to the specified date
  628.      * @since 10.1
  629.      */
  630.     public static AbsoluteDate parseCCSDSUnsegmentedTimeCode(
  631.             final byte preambleField1,
  632.             final byte preambleField2,
  633.             final byte[] timeField,
  634.             final AbsoluteDate agencyDefinedEpoch,
  635.             final AbsoluteDate ccsdsEpoch) {
  636.         final CcsdsUnsegmentedTimeCode<AbsoluteDate> timeCode =
  637.             new CcsdsUnsegmentedTimeCode<>(preambleField1, preambleField2, timeField,
  638.                                            agencyDefinedEpoch, ccsdsEpoch);
  639.         return new AbsoluteDate(timeCode.getEpoch(), timeCode.getSeconds()).
  640.                shiftedBy(timeCode.getSubSecond());

  641.     }

  642.     /** Build an instance from a CCSDS Day Segmented Time Code (CDS).
  643.      * <p>
  644.      * CCSDS Day Segmented Time Code is defined in the blue book:
  645.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  646.      * </p>
  647.      *
  648.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  649.      *
  650.      * @param preambleField field specifying the format, often not transmitted in
  651.      * data interfaces, as it is constant for a given data interface
  652.      * @param timeField byte array containing the time code
  653.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field
  654.      * specifies the {@link #CCSDS_EPOCH CCSDS reference epoch} is used (and hence
  655.      * may be null in this case)
  656.      * @return an instance corresponding to the specified date
  657.      * @see #parseCCSDSDaySegmentedTimeCode(byte, byte[], DateComponents, TimeScale)
  658.      */
  659.     @DefaultDataContext
  660.     public static AbsoluteDate parseCCSDSDaySegmentedTimeCode(final byte preambleField, final byte[] timeField,
  661.                                                               final DateComponents agencyDefinedEpoch) {
  662.         return parseCCSDSDaySegmentedTimeCode(preambleField, timeField,
  663.                 agencyDefinedEpoch, DataContext.getDefault().getTimeScales().getUTC());
  664.     }

  665.     /** Build an instance from a CCSDS Day Segmented Time Code (CDS).
  666.      * <p>
  667.      * CCSDS Day Segmented Time Code is defined in the blue book:
  668.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  669.      * </p>
  670.      * @param preambleField field specifying the format, often not transmitted in
  671.      * data interfaces, as it is constant for a given data interface
  672.      * @param timeField byte array containing the time code
  673.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field
  674.      * specifies the {@link #CCSDS_EPOCH CCSDS reference epoch} is used (and hence
  675.      * may be null in this case)
  676.      * @param utc      time scale used to compute date and time components.
  677.      * @return an instance corresponding to the specified date
  678.      * @since 10.1
  679.      */
  680.     public static AbsoluteDate parseCCSDSDaySegmentedTimeCode(final byte preambleField,
  681.                                                               final byte[] timeField,
  682.                                                               final DateComponents agencyDefinedEpoch,
  683.                                                               final TimeScale utc) {

  684.         final CcsdsSegmentedTimeCode timeCode = new CcsdsSegmentedTimeCode(preambleField, timeField,
  685.                                                                            agencyDefinedEpoch);
  686.         return new AbsoluteDate(timeCode.getDate(), timeCode.getTime(), utc).
  687.                shiftedBy(timeCode.getSubSecond());
  688.     }

  689.     /** Build an instance from a CCSDS Calendar Segmented Time Code (CCS).
  690.      * <p>
  691.      * CCSDS Calendar Segmented Time Code is defined in the blue book:
  692.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  693.      * </p>
  694.      *
  695.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  696.      *
  697.      * @param preambleField field specifying the format, often not transmitted in
  698.      * data interfaces, as it is constant for a given data interface
  699.      * @param timeField byte array containing the time code
  700.      * @return an instance corresponding to the specified date
  701.      * @see #parseCCSDSCalendarSegmentedTimeCode(byte, byte[], TimeScale)
  702.      */
  703.     @DefaultDataContext
  704.     public static AbsoluteDate parseCCSDSCalendarSegmentedTimeCode(final byte preambleField, final byte[] timeField) {
  705.         return parseCCSDSCalendarSegmentedTimeCode(preambleField, timeField,
  706.                 DataContext.getDefault().getTimeScales().getUTC());
  707.     }

  708.     /** Build an instance from a CCSDS Calendar Segmented Time Code (CCS).
  709.      * <p>
  710.      * CCSDS Calendar Segmented Time Code is defined in the blue book:
  711.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  712.      * </p>
  713.      * @param preambleField field specifying the format, often not transmitted in
  714.      * data interfaces, as it is constant for a given data interface
  715.      * @param timeField byte array containing the time code
  716.      * @param utc      time scale used to compute date and time components.
  717.      * @return an instance corresponding to the specified date
  718.      * @since 10.1
  719.      */
  720.     public static AbsoluteDate parseCCSDSCalendarSegmentedTimeCode(final byte preambleField,
  721.                                                                    final byte[] timeField,
  722.                                                                    final TimeScale utc) {
  723.         final CcsdsSegmentedTimeCode timeCode = new CcsdsSegmentedTimeCode(preambleField, timeField);
  724.         return new AbsoluteDate(timeCode.getDate(), timeCode.getTime(), utc).
  725.                shiftedBy(timeCode.getSubSecond());
  726.     }

  727.     /** Build an instance corresponding to a Julian Day date.
  728.      * @param jd Julian day
  729.      * @param secondsSinceNoon seconds in the Julian day
  730.      * (BEWARE, Julian days start at noon, so 0.0 is noon)
  731.      * @param timeScale time scale in which the seconds in day are defined
  732.      * @return a new instant
  733.      */
  734.     public static AbsoluteDate createJDDate(final int jd, final double secondsSinceNoon,
  735.                                             final TimeScale timeScale) {
  736.         return new AbsoluteDate(new DateComponents(DateComponents.JULIAN_EPOCH, jd),
  737.                 TimeComponents.H12, timeScale).shiftedBy(secondsSinceNoon);
  738.     }

  739.     /** Build an instance corresponding to a Julian Day date.
  740.      * <p>
  741.      * This function should be preferred to {@link #createMJDDate(int, double, TimeScale)} when the target time scale
  742.      * has a non-constant offset with respect to TAI.
  743.      * <p>
  744.      * The idea is to introduce a pivot time scale that is close to the target time scale but has a constant bias with TAI.
  745.      * <p>
  746.      * For example, to get a date from an MJD in TDB time scale, it's advised to use the TT time scale
  747.      * as a pivot scale. TT is very close to TDB and has constant offset to TAI.
  748.      * @param jd Julian day
  749.      * @param secondsSinceNoon seconds in the Julian day
  750.      * (BEWARE, Julian days start at noon, so 0.0 is noon)
  751.      * @param timeScale timescale in which the seconds in day are defined
  752.      * @param pivotTimeScale pivot timescale used as intermediate timescale
  753.      * @return a new instant
  754.      */
  755.     public static AbsoluteDate createJDDate(final int jd, final double secondsSinceNoon,
  756.                                             final TimeScale timeScale,
  757.                                             final TimeScale pivotTimeScale) {
  758.         // Get the date in pivot timescale
  759.         final AbsoluteDate dateInPivotTimeScale = createJDDate(jd, secondsSinceNoon, pivotTimeScale);

  760.         // Compare offsets to TAI of the two time scales
  761.         final double offsetFromTAI = timeScale.offsetFromTAI(dateInPivotTimeScale) -  pivotTimeScale.offsetFromTAI(dateInPivotTimeScale);

  762.         // Return date in desired timescale
  763.         return dateInPivotTimeScale.shiftedBy(-offsetFromTAI);
  764.     }

  765.     /** Build an instance corresponding to a Modified Julian Day date.
  766.      * @param mjd modified Julian day
  767.      * @param secondsInDay seconds in the day
  768.      * @param timeScale time scale in which the seconds in day are defined
  769.      * @return a new instant
  770.      * @exception OrekitIllegalArgumentException if seconds number is out of range
  771.      */
  772.     public static AbsoluteDate createMJDDate(final int mjd, final double secondsInDay,
  773.                                              final TimeScale timeScale)
  774.         throws OrekitIllegalArgumentException {
  775.         final DateComponents dc = new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd);
  776.         final TimeComponents tc;
  777.         if (secondsInDay >= Constants.JULIAN_DAY) {
  778.             // check we are really allowed to use this number of seconds
  779.             final int    secondsA = 86399; // 23:59:59, i.e. 59s in the last minute of the day
  780.             final double secondsB = secondsInDay - secondsA;
  781.             final TimeComponents safeTC = new TimeComponents(secondsA, 0.0);
  782.             final AbsoluteDate safeDate = new AbsoluteDate(dc, safeTC, timeScale);
  783.             if (timeScale.minuteDuration(safeDate) > 59 + secondsB) {
  784.                 // we are within the last minute of the day, the number of seconds is OK
  785.                 return safeDate.shiftedBy(secondsB);
  786.             } else {
  787.                 // let TimeComponents trigger an OrekitIllegalArgumentException
  788.                 // for the wrong number of seconds
  789.                 tc = new TimeComponents(secondsA, secondsB);
  790.             }
  791.         } else {
  792.             tc = new TimeComponents(secondsInDay);
  793.         }

  794.         // create the date
  795.         return new AbsoluteDate(dc, tc, timeScale);

  796.     }

  797.     /** Build an instance corresponding to a Julian Epoch (JE).
  798.      * <p>According to Lieske paper: <a
  799.      * href="http://articles.adsabs.harvard.edu/cgi-bin/nph-iarticle_query?1979A%26A....73..282L&amp;defaultprint=YES&amp;filetype=.pdf.">
  800.      * Precession Matrix Based on IAU (1976) System of Astronomical Constants</a>, Astronomy and Astrophysics,
  801.      * vol. 73, no. 3, Mar. 1979, p. 282-284, Julian Epoch is related to Julian Ephemeris Date as:</p>
  802.      * <pre>
  803.      * JE = 2000.0 + (JED - 2451545.0) / 365.25
  804.      * </pre>
  805.      * <p>
  806.      * This method reverts the formula above and computes an {@code AbsoluteDate} from the Julian Epoch.
  807.      * </p>
  808.      *
  809.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  810.      *
  811.      * @param julianEpoch Julian epoch, like 2000.0 for defining the classical reference J2000.0
  812.      * @return a new instant
  813.      * @see #J2000_EPOCH
  814.      * @see #createBesselianEpoch(double)
  815.      * @see TimeScales#createJulianEpoch(double)
  816.      */
  817.     @DefaultDataContext
  818.     public static AbsoluteDate createJulianEpoch(final double julianEpoch) {
  819.         return DataContext.getDefault().getTimeScales().createJulianEpoch(julianEpoch);
  820.     }

  821.     /** Build an instance corresponding to a Besselian Epoch (BE).
  822.      * <p>According to Lieske paper: <a
  823.      * href="http://articles.adsabs.harvard.edu/cgi-bin/nph-iarticle_query?1979A%26A....73..282L&amp;defaultprint=YES&amp;filetype=.pdf.">
  824.      * Precession Matrix Based on IAU (1976) System of Astronomical Constants</a>, Astronomy and Astrophysics,
  825.      * vol. 73, no. 3, Mar. 1979, p. 282-284, Besselian Epoch is related to Julian Ephemeris Date as:</p>
  826.      * <pre>
  827.      * BE = 1900.0 + (JED - 2415020.31352) / 365.242198781
  828.      * </pre>
  829.      * <p>
  830.      * This method reverts the formula above and computes an {@code AbsoluteDate} from the Besselian Epoch.
  831.      * </p>
  832.      *
  833.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  834.      *
  835.      * @param besselianEpoch Besselian epoch, like 1950 for defining the classical reference B1950.0
  836.      * @return a new instant
  837.      * @see #createJulianEpoch(double)
  838.      * @see TimeScales#createBesselianEpoch(double)
  839.      */
  840.     @DefaultDataContext
  841.     public static AbsoluteDate createBesselianEpoch(final double besselianEpoch) {
  842.         return DataContext.getDefault().getTimeScales()
  843.                 .createBesselianEpoch(besselianEpoch);
  844.     }

  845.     /** Get a time-shifted date.
  846.      * <p>
  847.      * Calling this method is equivalent to call <code>new AbsoluteDate(this, dt)</code>.
  848.      * </p>
  849.      * @param dt time shift in seconds
  850.      * @return a new date, shifted with respect to instance (which is immutable)
  851.      * @see org.orekit.utils.PVCoordinates#shiftedBy(double)
  852.      * @see org.orekit.attitudes.Attitude#shiftedBy(double)
  853.      * @see org.orekit.orbits.Orbit#shiftedBy(double)
  854.      * @see org.orekit.propagation.SpacecraftState#shiftedBy(double)
  855.      */
  856.     public AbsoluteDate shiftedBy(final double dt) {
  857.         return new AbsoluteDate(this, dt);
  858.     }

  859.     /** Get a time-shifted date.
  860.      * <p>
  861.      * Calling this method is equivalent to call <code>new AbsoluteDate(this, shift, timeUnit)</code>.
  862.      * </p>
  863.      * @param dt time shift in time units
  864.      * @param timeUnit {@link TimeUnit} of the shift
  865.      * @return a new date, shifted with respect to instance (which is immutable)
  866.      * @since 12.1
  867.      */
  868.     public AbsoluteDate shiftedBy(final long dt, final TimeUnit timeUnit) {
  869.         return new AbsoluteDate(this, dt, timeUnit);
  870.     }

  871.     /** Compute the physically elapsed duration between two instants.
  872.      * <p>The returned duration is the number of seconds physically
  873.      * elapsed between the two instants, measured in a regular time
  874.      * scale with respect to surface of the Earth (i.e either the {@link
  875.      * TAIScale TAI scale}, the {@link TTScale TT scale} or the {@link
  876.      * GPSScale GPS scale}). It is the only method that gives a
  877.      * duration with a physical meaning.</p>
  878.      * <p>This method gives the same result (with less computation)
  879.      * as calling {@link #offsetFrom(AbsoluteDate, TimeScale)}
  880.      * with a second argument set to one of the regular scales cited
  881.      * above.</p>
  882.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  883.      * double)} constructor.</p>
  884.      * @param instant instant to subtract from the instance
  885.      * @return offset in seconds between the two instants (positive
  886.      * if the instance is posterior to the argument)
  887.      * @see #offsetFrom(AbsoluteDate, TimeScale)
  888.      * @see #AbsoluteDate(AbsoluteDate, double)
  889.      */
  890.     public double durationFrom(final AbsoluteDate instant) {
  891.         return (epoch - instant.epoch) + (offset - instant.offset);
  892.     }

  893.     /** Compute the physically elapsed duration between two instants.
  894.      * <p>The returned duration is the duration physically
  895.      * elapsed between the two instants, using the given time unit and rounded to the nearest integer, measured in a regular time
  896.      * scale with respect to surface of the Earth (i.e either the {@link
  897.      * TAIScale TAI scale}, the {@link TTScale TT scale} or the {@link
  898.      * GPSScale GPS scale}). It is the only method that gives a
  899.      * duration with a physical meaning.</p>
  900.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  901.      * long, TimeUnit)} constructor.</p>
  902.      * @param instant instant to subtract from the instance
  903.      * @param timeUnit {@link TimeUnit} precision for the offset
  904.      * @return offset in the given timeunit between the two instants (positive
  905.      * if the instance is posterior to the argument), rounded to the nearest integer {@link TimeUnit}
  906.      * @see #AbsoluteDate(AbsoluteDate, long, TimeUnit)
  907.      * @since 12.1
  908.      */
  909.     public long durationFrom(final AbsoluteDate instant, final TimeUnit timeUnit) {
  910.         final long deltaEpoch = timeUnit.convert(epoch - instant.epoch, TimeUnit.SECONDS);

  911.         final long multiplier = timeUnit.convert(1, TimeUnit.SECONDS);
  912.         final long deltaOffset = FastMath.round((offset - instant.offset) * multiplier);

  913.         return deltaEpoch + deltaOffset;
  914.     }

  915.     /** Compute the apparent clock offset between two instant <em>in the
  916.      * perspective of a specific {@link TimeScale time scale}</em>.
  917.      * <p>The offset is the number of seconds counted in the given
  918.      * time scale between the locations of the two instants, with
  919.      * all time scale irregularities removed (i.e. considering all
  920.      * days are exactly 86400 seconds long). This method will give
  921.      * a result that may not have a physical meaning if the time scale
  922.      * is irregular. For example since a leap second was introduced at
  923.      * the end of 2005, the apparent offset between 2005-12-31T23:59:59
  924.      * and 2006-01-01T00:00:00 is 1 second, but the physical duration
  925.      * of the corresponding time interval as returned by the {@link
  926.      * #durationFrom(AbsoluteDate)} method is 2 seconds.</p>
  927.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  928.      * double, TimeScale)} constructor.</p>
  929.      * @param instant instant to subtract from the instance
  930.      * @param timeScale time scale with respect to which the offset should
  931.      * be computed
  932.      * @return apparent clock offset in seconds between the two instants
  933.      * (positive if the instance is posterior to the argument)
  934.      * @see #durationFrom(AbsoluteDate)
  935.      * @see #AbsoluteDate(AbsoluteDate, double, TimeScale)
  936.      */
  937.     public double offsetFrom(final AbsoluteDate instant, final TimeScale timeScale) {
  938.         final long   elapsedDurationA = epoch - instant.epoch;
  939.         final double elapsedDurationB = (offset         + timeScale.offsetFromTAI(this)) -
  940.                                         (instant.offset + timeScale.offsetFromTAI(instant));
  941.         return  elapsedDurationA + elapsedDurationB;
  942.     }

  943.     /** Compute the offset between two time scales at the current instant.
  944.      * <p>The offset is defined as <i>l₁-l₂</i>
  945.      * where <i>l₁</i> is the location of the instant in
  946.      * the <code>scale1</code> time scale and <i>l₂</i> is the
  947.      * location of the instant in the <code>scale2</code> time scale.</p>
  948.      * @param scale1 first time scale
  949.      * @param scale2 second time scale
  950.      * @return offset in seconds between the two time scales at the
  951.      * current instant
  952.      */
  953.     public double timeScalesOffset(final TimeScale scale1, final TimeScale scale2) {
  954.         return scale1.offsetFromTAI(this) - scale2.offsetFromTAI(this);
  955.     }

  956.     /** Convert the instance to a Java {@link java.util.Date Date}.
  957.      * <p>Conversion to the Date class induces a loss of precision because
  958.      * the Date class does not provide sub-millisecond information. Java Dates
  959.      * are considered to be locations in some times scales.</p>
  960.      * @param timeScale time scale to use
  961.      * @return a {@link java.util.Date Date} instance representing the location
  962.      * of the instant in the time scale
  963.      */
  964.     public Date toDate(final TimeScale timeScale) {
  965.         final double time = epoch + (offset + timeScale.offsetFromTAI(this));
  966.         return new Date(FastMath.round((time + 10957.5 * 86400.0) * 1000));
  967.     }

  968.     /**
  969.      * Convert the instance to a Java {@link java.time.Instant Instant}.
  970.      * Nanosecond precision is preserved during this conversion
  971.      *
  972.      * @return a {@link java.time.Instant Instant} instance representing the location
  973.      * of the instant in the utc time scale
  974.      * @since 12.1
  975.      */
  976.     @DefaultDataContext
  977.     public Instant toInstant() {
  978.         return toInstant(TimeScalesFactory.getTimeScales());
  979.     }

  980.     /**
  981.      * Convert the instance to a Java {@link java.time.Instant Instant}.
  982.      * Nanosecond precision is preserved during this conversion
  983.      *
  984.      * @param timeScales the timescales to use
  985.      * @return a {@link java.time.Instant Instant} instance representing the location
  986.      * of the instant in the utc time scale
  987.      * @since 12.1
  988.      */
  989.     public Instant toInstant(final TimeScales timeScales) {
  990.         final UTCScale utc = timeScales.getUTC();
  991.         final String stringWithoutUtcOffset = toStringWithoutUtcOffset(utc, 9);

  992.         final LocalDateTime localDateTime = LocalDateTime.parse(stringWithoutUtcOffset, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
  993.         return localDateTime.toInstant(ZoneOffset.UTC);
  994.     }

  995.     /** Split the instance into date/time components.
  996.      * @param timeScale time scale to use
  997.      * @return date/time components
  998.      */
  999.     public DateTimeComponents getComponents(final TimeScale timeScale) {

  1000.         if (Double.isInfinite(offset)) {
  1001.             // special handling for past and future infinity
  1002.             if (offset < 0) {
  1003.                 return new DateTimeComponents(DateComponents.MIN_EPOCH, TimeComponents.H00);
  1004.             } else {
  1005.                 return new DateTimeComponents(DateComponents.MAX_EPOCH,
  1006.                                               new TimeComponents(23, 59, 59.999));
  1007.             }
  1008.         }

  1009.         // Compute offset from 2000-01-01T00:00:00 in specified time scale.
  1010.         // Use 2Sum for high precision.
  1011.         final double taiOffset = timeScale.offsetFromTAI(this);
  1012.         final SumAndResidual sumAndResidual = MathUtils.twoSum(offset, taiOffset);

  1013.         // split date and time
  1014.         final long   carry = (long) FastMath.floor(sumAndResidual.getSum());
  1015.         double offset2000B = (sumAndResidual.getSum() - carry) + sumAndResidual.getResidual();
  1016.         long   offset2000A = epoch + carry + 43200L;
  1017.         if (offset2000B < 0) {
  1018.             offset2000A -= 1;
  1019.             offset2000B += 1;
  1020.         }
  1021.         long time = offset2000A % 86400L;
  1022.         if (time < 0L) {
  1023.             time += 86400L;
  1024.         }
  1025.         final int date = (int) ((offset2000A - time) / 86400L);

  1026.         // extract calendar elements
  1027.         final DateComponents dateComponents = new DateComponents(DateComponents.J2000_EPOCH, date);

  1028.         // extract time element, accounting for leap seconds
  1029.         final double leap = timeScale.insideLeap(this) ? timeScale.getLeap(this) : 0;
  1030.         final int minuteDuration = timeScale.minuteDuration(this);
  1031.         final TimeComponents timeComponents =
  1032.                 TimeComponents.fromSeconds((int) time, offset2000B, leap, minuteDuration);

  1033.         // build the components
  1034.         return new DateTimeComponents(dateComponents, timeComponents);

  1035.     }

  1036.     /** Split the instance into date/time components for a local time.
  1037.      *
  1038.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  1039.      *
  1040.      * @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
  1041.      * negative Westward UTC)
  1042.      * @return date/time components
  1043.      * @since 7.2
  1044.      * @see #getComponents(int, TimeScale)
  1045.      */
  1046.     @DefaultDataContext
  1047.     public DateTimeComponents getComponents(final int minutesFromUTC) {
  1048.         return getComponents(minutesFromUTC,
  1049.                 DataContext.getDefault().getTimeScales().getUTC());
  1050.     }

  1051.     /**
  1052.      * Split the instance into date/time components for a local time.
  1053.      *
  1054.      * @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
  1055.      *                       negative Westward UTC)
  1056.      * @param utc            time scale used to compute date and time components.
  1057.      * @return date/time components
  1058.      * @since 10.1
  1059.      */
  1060.     public DateTimeComponents getComponents(final int minutesFromUTC,
  1061.                                             final TimeScale utc) {

  1062.         final DateTimeComponents utcComponents = getComponents(utc);

  1063.         // shift the date according to UTC offset, but WITHOUT touching the seconds,
  1064.         // as they may exceed 60.0 during a leap seconds introduction,
  1065.         // and we want to preserve these special cases
  1066.         final double seconds = utcComponents.getTime().getSecond();

  1067.         int minute = utcComponents.getTime().getMinute() + minutesFromUTC;
  1068.         final int hourShift;
  1069.         if (minute < 0) {
  1070.             hourShift = (minute - 59) / 60;
  1071.         } else if (minute > 59) {
  1072.             hourShift = minute / 60;
  1073.         } else {
  1074.             hourShift = 0;
  1075.         }
  1076.         minute -= 60 * hourShift;

  1077.         int hour = utcComponents.getTime().getHour() + hourShift;
  1078.         final int dayShift;
  1079.         if (hour < 0) {
  1080.             dayShift = (hour - 23) / 24;
  1081.         } else if (hour > 23) {
  1082.             dayShift = hour / 24;
  1083.         } else {
  1084.             dayShift = 0;
  1085.         }
  1086.         hour -= 24 * dayShift;

  1087.         return new DateTimeComponents(new DateComponents(utcComponents.getDate(), dayShift),
  1088.                                       new TimeComponents(hour, minute, seconds, minutesFromUTC));

  1089.     }

  1090.     /** Split the instance into date/time components for a time zone.
  1091.      *
  1092.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  1093.      *
  1094.      * @param timeZone time zone
  1095.      * @return date/time components
  1096.      * @since 7.2
  1097.      * @see #getComponents(TimeZone, TimeScale)
  1098.      */
  1099.     @DefaultDataContext
  1100.     public DateTimeComponents getComponents(final TimeZone timeZone) {
  1101.         return getComponents(timeZone, DataContext.getDefault().getTimeScales().getUTC());
  1102.     }

  1103.     /**
  1104.      * Split the instance into date/time components for a time zone.
  1105.      *
  1106.      * @param timeZone time zone
  1107.      * @param utc      time scale used to computed date and time components.
  1108.      * @return date/time components
  1109.      * @since 10.1
  1110.      */
  1111.     public DateTimeComponents getComponents(final TimeZone timeZone,
  1112.                                             final TimeScale utc) {
  1113.         final AbsoluteDate javaEpoch = new AbsoluteDate(DateComponents.JAVA_EPOCH, utc);
  1114.         final long milliseconds = FastMath.round(1000 * offsetFrom(javaEpoch, utc));
  1115.         return getComponents(timeZone.getOffset(milliseconds) / 60000, utc);
  1116.     }

  1117.     /** Compare the instance with another date.
  1118.      * @param date other date to compare the instance to
  1119.      * @return a negative integer, zero, or a positive integer as this date
  1120.      * is before, simultaneous, or after the specified date.
  1121.      */
  1122.     public int compareTo(final AbsoluteDate date) {
  1123.         final double duration = durationFrom(date);
  1124.         if (!Double.isNaN(duration)) {
  1125.             return Double.compare(duration, 0.0);
  1126.         }
  1127.         // both dates are infinity or one is NaN or both are NaN
  1128.         return Double.compare(offset, date.offset);
  1129.     }

  1130.     /** {@inheritDoc} */
  1131.     public AbsoluteDate getDate() {
  1132.         return this;
  1133.     }

  1134.     /** Check if the instance represents the same time as another instance.
  1135.      * @param date other date
  1136.      * @return true if the instance and the other date refer to the same instant
  1137.      */
  1138.     public boolean equals(final Object date) {

  1139.         if (date == this) {
  1140.             // first fast check
  1141.             return true;
  1142.         }

  1143.         if (date instanceof AbsoluteDate) {

  1144.             // Improve robustness against positive/negative infinity dates
  1145.             if ( this.offset == Double.NEGATIVE_INFINITY && ((AbsoluteDate) date).offset == Double.NEGATIVE_INFINITY ||
  1146.                     this.offset == Double.POSITIVE_INFINITY && ((AbsoluteDate) date).offset == Double.POSITIVE_INFINITY ) {
  1147.                 return true;
  1148.             } else {
  1149.                 return durationFrom((AbsoluteDate) date) == 0;
  1150.             }
  1151.         }

  1152.         return false;
  1153.     }

  1154.     /** Check if the instance represents the same time as another.
  1155.      * @param other the instant to compare this date to
  1156.      * @return true if the instance and the argument refer to the same instant
  1157.      * @see #isCloseTo(TimeStamped, double)
  1158.      * @since 10.1
  1159.      */
  1160.     public boolean isEqualTo(final TimeStamped other) {
  1161.         return this.equals(other.getDate());
  1162.     }

  1163.     /** Check if the instance time is close to another.
  1164.      * @param other the instant to compare this date to
  1165.      * @param tolerance the separation, in seconds, under which the two instants will be considered close to each other
  1166.      * @return true if the duration between the instance and the argument is strictly below the tolerance
  1167.      * @see #isEqualTo(TimeStamped)
  1168.      * @since 10.1
  1169.      */
  1170.     public boolean isCloseTo(final TimeStamped other, final double tolerance) {
  1171.         return FastMath.abs(this.durationFrom(other.getDate())) < tolerance;
  1172.     }

  1173.     /** Check if the instance represents a time that is strictly before another.
  1174.      * @param other the instant to compare this date to
  1175.      * @return true if the instance is strictly before the argument when ordering chronologically
  1176.      * @see #isBeforeOrEqualTo(TimeStamped)
  1177.      * @since 10.1
  1178.      */
  1179.     public boolean isBefore(final TimeStamped other) {
  1180.         return this.compareTo(other.getDate()) < 0;
  1181.     }

  1182.     /** Check if the instance represents a time that is strictly after another.
  1183.      * @param other the instant to compare this date to
  1184.      * @return true if the instance is strictly after the argument when ordering chronologically
  1185.      * @see #isAfterOrEqualTo(TimeStamped)
  1186.      * @since 10.1
  1187.      */
  1188.     public boolean isAfter(final TimeStamped other) {
  1189.         return this.compareTo(other.getDate()) > 0;
  1190.     }

  1191.     /** Check if the instance represents a time that is before or equal to another.
  1192.      * @param other the instant to compare this date to
  1193.      * @return true if the instance is before (or equal to) the argument when ordering chronologically
  1194.      * @see #isBefore(TimeStamped)
  1195.      * @since 10.1
  1196.      */
  1197.     public boolean isBeforeOrEqualTo(final TimeStamped other) {
  1198.         return this.isEqualTo(other) || this.isBefore(other);
  1199.     }

  1200.     /** Check if the instance represents a time that is after or equal to another.
  1201.      * @param other the instant to compare this date to
  1202.      * @return true if the instance is after (or equal to) the argument when ordering chronologically
  1203.      * @see #isAfterOrEqualTo(TimeStamped)
  1204.      * @since 10.1
  1205.      */
  1206.     public boolean isAfterOrEqualTo(final TimeStamped other) {
  1207.         return this.isEqualTo(other) || this.isAfter(other);
  1208.     }

  1209.     /** Check if the instance represents a time that is strictly between two others representing
  1210.      * the boundaries of a time span. The two boundaries can be provided in any order: in other words,
  1211.      * whether <code>boundary</code> represents a time that is before or after <code>otherBoundary</code> will
  1212.      * not change the result of this method.
  1213.      * @param boundary one end of the time span
  1214.      * @param otherBoundary the other end of the time span
  1215.      * @return true if the instance is strictly between the two arguments when ordering chronologically
  1216.      * @see #isBetweenOrEqualTo(TimeStamped, TimeStamped)
  1217.      * @since 10.1
  1218.      */
  1219.     public boolean isBetween(final TimeStamped boundary, final TimeStamped otherBoundary) {
  1220.         final TimeStamped beginning;
  1221.         final TimeStamped end;
  1222.         if (boundary.getDate().isBefore(otherBoundary)) {
  1223.             beginning = boundary;
  1224.             end = otherBoundary;
  1225.         } else {
  1226.             beginning = otherBoundary;
  1227.             end = boundary;
  1228.         }
  1229.         return this.isAfter(beginning) && this.isBefore(end);
  1230.     }

  1231.     /** Check if the instance represents a time that is between two others representing
  1232.      * the boundaries of a time span, or equal to one of them. The two boundaries can be provided in any order:
  1233.      * in other words, whether <code>boundary</code> represents a time that is before or after
  1234.      * <code>otherBoundary</code> will not change the result of this method.
  1235.      * @param boundary one end of the time span
  1236.      * @param otherBoundary the other end of the time span
  1237.      * @return true if the instance is between the two arguments (or equal to at least one of them)
  1238.      * when ordering chronologically
  1239.      * @see #isBetween(TimeStamped, TimeStamped)
  1240.      * @since 10.1
  1241.      */
  1242.     public boolean isBetweenOrEqualTo(final TimeStamped boundary, final TimeStamped otherBoundary) {
  1243.         return this.isEqualTo(boundary) || this.isEqualTo(otherBoundary) || this.isBetween(boundary, otherBoundary);
  1244.     }

  1245.     /** Get a hashcode for this date.
  1246.      * @return hashcode
  1247.      */
  1248.     public int hashCode() {
  1249.         final long l = Double.doubleToLongBits(durationFrom(ARBITRARY_EPOCH));
  1250.         return (int) (l ^ (l >>> 32));
  1251.     }

  1252.     /**
  1253.      * Get a String representation of the instant location with up to 16 digits of
  1254.      * precision for the seconds value.
  1255.      *
  1256.      * <p> Since this method is used in exception messages and error handling every
  1257.      * effort is made to return some representation of the instant. If UTC is available
  1258.      * from the default data context then it is used to format the string in UTC. If not
  1259.      * then TAI is used. Finally if the prior attempts fail this method falls back to
  1260.      * converting this class's internal representation to a string.
  1261.      *
  1262.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  1263.      *
  1264.      * @return a string representation of the instance, in ISO-8601 format if UTC is
  1265.      * available from the default data context.
  1266.      * @see #toString(TimeScale)
  1267.      * @see #toStringRfc3339(TimeScale)
  1268.      * @see DateTimeComponents#toString(int, int)
  1269.      */
  1270.     @DefaultDataContext
  1271.     public String toString() {
  1272.         // CHECKSTYLE: stop IllegalCatch check
  1273.         try {
  1274.             // try to use UTC first at that is likely most familiar to the user.
  1275.             return toString(DataContext.getDefault().getTimeScales().getUTC()) + "Z";
  1276.         } catch (RuntimeException e1) {
  1277.             // catch OrekitException, OrekitIllegalStateException, etc.
  1278.             try {
  1279.                 // UTC failed, try to use TAI
  1280.                 return toString(new TAIScale()) + " TAI";
  1281.             } catch (RuntimeException e2) {
  1282.                 // catch OrekitException, OrekitIllegalStateException, etc.
  1283.                 // Likely failed to convert to ymdhms.
  1284.                 // Give user some indication of what time it is.
  1285.                 try {
  1286.                     return "(" + this.epoch + " + " + this.offset + ") seconds past epoch";
  1287.                 } catch (RuntimeException e3) {
  1288.                     // give up and throw an exception
  1289.                     e2.addSuppressed(e3);
  1290.                     e1.addSuppressed(e2);
  1291.                     throw e1;
  1292.                 }
  1293.             }
  1294.         }
  1295.         // CHECKSTYLE: resume IllegalCatch check
  1296.     }

  1297.     /**
  1298.      * Get a String representation of the instant location in ISO-8601 format without the
  1299.      * UTC offset and with up to 16 digits of precision for the seconds value.
  1300.      *
  1301.      * @param timeScale time scale to use
  1302.      * @return a string representation of the instance.
  1303.      * @see #toStringRfc3339(TimeScale)
  1304.      * @see DateTimeComponents#toString(int, int)
  1305.      */
  1306.     public String toString(final TimeScale timeScale) {
  1307.         return getComponents(timeScale).toStringWithoutUtcOffset();
  1308.     }

  1309.     /** Get a String representation of the instant location for a local time.
  1310.      *
  1311.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  1312.      *
  1313.      * @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
  1314.      * negative Westward UTC).
  1315.      * @return string representation of the instance,
  1316.      * in ISO-8601 format with milliseconds accuracy
  1317.      * @since 7.2
  1318.      * @see #toString(int, TimeScale)
  1319.      */
  1320.     @DefaultDataContext
  1321.     public String toString(final int minutesFromUTC) {
  1322.         return toString(minutesFromUTC,
  1323.                 DataContext.getDefault().getTimeScales().getUTC());
  1324.     }

  1325.     /**
  1326.      * Get a String representation of the instant location for a local time.
  1327.      *
  1328.      * @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
  1329.      *                       negative Westward UTC).
  1330.      * @param utc            time scale used to compute date and time components.
  1331.      * @return string representation of the instance, in ISO-8601 format with milliseconds
  1332.      * accuracy
  1333.      * @since 10.1
  1334.      * @see #getComponents(int, TimeScale)
  1335.      * @see DateTimeComponents#toString(int, int)
  1336.      */
  1337.     public String toString(final int minutesFromUTC, final TimeScale utc) {
  1338.         final int minuteDuration = utc.minuteDuration(this);
  1339.         return getComponents(minutesFromUTC, utc).toString(minuteDuration);
  1340.     }

  1341.     /** Get a String representation of the instant location for a time zone.
  1342.      *
  1343.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  1344.      *
  1345.      * @param timeZone time zone
  1346.      * @return string representation of the instance,
  1347.      * in ISO-8601 format with milliseconds accuracy
  1348.      * @since 7.2
  1349.      * @see #toString(TimeZone, TimeScale)
  1350.      */
  1351.     @DefaultDataContext
  1352.     public String toString(final TimeZone timeZone) {
  1353.         return toString(timeZone, DataContext.getDefault().getTimeScales().getUTC());
  1354.     }

  1355.     /**
  1356.      * Get a String representation of the instant location for a time zone.
  1357.      *
  1358.      * @param timeZone time zone
  1359.      * @param utc      time scale used to compute date and time components.
  1360.      * @return string representation of the instance, in ISO-8601 format with milliseconds
  1361.      * accuracy
  1362.      * @since 10.1
  1363.      * @see #getComponents(TimeZone, TimeScale)
  1364.      * @see DateTimeComponents#toString(int, int)
  1365.      */
  1366.     public String toString(final TimeZone timeZone, final TimeScale utc) {
  1367.         final int minuteDuration = utc.minuteDuration(this);
  1368.         return getComponents(timeZone, utc).toString(minuteDuration);
  1369.     }

  1370.     /**
  1371.      * Represent the given date as a string according to the format in RFC 3339. RFC3339
  1372.      * is a restricted subset of ISO 8601 with a well defined grammar. Enough digits are
  1373.      * included in the seconds value to avoid rounding up to the next minute.
  1374.      *
  1375.      * <p>This method is different than {@link AbsoluteDate#toString(TimeScale)} in that
  1376.      * it includes a {@code "Z"} at the end to indicate the time zone and enough precision
  1377.      * to represent the point in time without rounding up to the next minute.
  1378.      *
  1379.      * <p>RFC3339 is unable to represent BC years, years of 10000 or more, time zone
  1380.      * offsets of 100 hours or more, or NaN. In these cases the value returned from this
  1381.      * method will not be valid RFC3339 format.
  1382.      *
  1383.      * @param utc time scale.
  1384.      * @return RFC 3339 format string.
  1385.      * @see <a href="https://tools.ietf.org/html/rfc3339#page-8">RFC 3339</a>
  1386.      * @see DateTimeComponents#toStringRfc3339()
  1387.      * @see #toString(TimeScale)
  1388.      * @see #getComponents(TimeScale)
  1389.      */
  1390.     public String toStringRfc3339(final TimeScale utc) {
  1391.         return this.getComponents(utc).toStringRfc3339();
  1392.     }

  1393.     /**
  1394.      * Return a string representation of this date-time, rounded to the given precision.
  1395.      *
  1396.      * <p>The format used is ISO8601 without the UTC offset.</p>
  1397.      *
  1398.      * <p>Calling {@code toStringWithoutUtcOffset(DataContext.getDefault().getTimeScales().getUTC(),
  1399.      * 3)} will emulate the behavior of {@link #toString()} in Orekit 10 and earlier. Note
  1400.      * this method is more accurate as it correctly handles rounding during leap seconds.
  1401.      *
  1402.      * @param timeScale      to use to compute components.
  1403.      * @param fractionDigits the number of digits to include after the decimal point in
  1404.      *                       the string representation of the seconds. The date and time
  1405.      *                       is first rounded as necessary. {@code fractionDigits} must be
  1406.      *                       greater than or equal to {@code 0}.
  1407.      * @return string representation of this date, time, and UTC offset
  1408.      * @see #toString(TimeScale)
  1409.      * @see #toStringRfc3339(TimeScale)
  1410.      * @see DateTimeComponents#toString(int, int)
  1411.      * @see DateTimeComponents#toStringWithoutUtcOffset(int, int)
  1412.      * @since 11.1
  1413.      */
  1414.     public String toStringWithoutUtcOffset(final TimeScale timeScale,
  1415.                                            final int fractionDigits) {
  1416.         return this.getComponents(timeScale)
  1417.                 .toStringWithoutUtcOffset(timeScale.minuteDuration(this), fractionDigits);
  1418.     }

  1419.     /**
  1420.      * Return the given date as a Modified Julian Date <b>expressed in UTC</b>.
  1421.      *
  1422.      * @return double representation of the given date as Modified Julian Date.
  1423.      *
  1424.      * @since 12.2
  1425.      */
  1426.     @DefaultDataContext
  1427.     public double getMJD() {
  1428.         return this.getJD() - DateComponents.JD_TO_MJD;
  1429.     }

  1430.     /**
  1431.      * Return the given date as a Modified Julian Date expressed in given timescale.
  1432.      *
  1433.      * @param ts time scale
  1434.      *
  1435.      * @return double representation of the given date as Modified Julian Date.
  1436.      *
  1437.      * @since 12.2
  1438.      */
  1439.     public double getMJD(final TimeScale ts) {
  1440.         return this.getJD(ts) - DateComponents.JD_TO_MJD;
  1441.     }

  1442.     /**
  1443.      * Return the given date as a Julian Date <b>expressed in UTC</b>.
  1444.      *
  1445.      * @return double representation of the given date as Julian Date.
  1446.      *
  1447.      * @since 12.2
  1448.      */
  1449.     @DefaultDataContext
  1450.     public double getJD() {
  1451.         return getJD(TimeScalesFactory.getUTC());
  1452.     }

  1453.     /**
  1454.      * Return the given date as a Julian Date expressed in given timescale.
  1455.      *
  1456.      * @param ts time scale
  1457.      *
  1458.      * @return double representation of the given date as Julian Date.
  1459.      *
  1460.      * @since 12.2
  1461.      */
  1462.     public double getJD(final TimeScale ts) {
  1463.         return this.getComponents(ts).offsetFrom(DateTimeComponents.JULIAN_EPOCH) / Constants.JULIAN_DAY;
  1464.     }
  1465. }