AbsoluteDate.java

  1. /* Copyright 2002-2025 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.orekit.annotation.DefaultDataContext;
  28. import org.orekit.data.DataContext;
  29. import org.orekit.errors.OrekitIllegalArgumentException;
  30. import org.orekit.utils.Constants;

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

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

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

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

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

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

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

  148.     /** Reference epoch for GPS weeks: 1980-01-06T00:00:00 GPS time.
  149.      *
  150.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  151.      *
  152.      * @see TimeScales#getGpsEpoch()
  153.      */
  154.     @DefaultDataContext
  155.     public static final AbsoluteDate GPS_EPOCH = DataContext.getDefault().getTimeScales().getGpsEpoch();

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

  164.     /** Reference epoch for NavIC weeks: 1999-08-22T00:00:00 NavIC time.
  165.      *
  166.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  167.      *
  168.      * @see TimeScales#getNavicEpoch()
  169.      */
  170.     @DefaultDataContext
  171.     public static final AbsoluteDate NAVIC_EPOCH = DataContext.getDefault().getTimeScales().getNavicEpoch();

  172.     /** Reference epoch for BeiDou weeks: 2006-01-01T00:00:00 UTC.
  173.      *
  174.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  175.      *
  176.      * @see TimeScales#getBeidouEpoch()
  177.      */
  178.     @DefaultDataContext
  179.     public static final AbsoluteDate BEIDOU_EPOCH = DataContext.getDefault().getTimeScales().getBeidouEpoch();

  180.     /** Reference epoch for GLONASS four-year interval number: 1996-01-01T00:00:00 GLONASS time.
  181.      * <p>By convention, TGLONASS = UTC + 3 hours.</p>
  182.      *
  183.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  184.      *
  185.      * @see TimeScales#getGlonassEpoch()
  186.      */
  187.     @DefaultDataContext
  188.     public static final AbsoluteDate GLONASS_EPOCH = DataContext.getDefault().getTimeScales().getGlonassEpoch();

  189.     /** J2000.0 Reference epoch: 2000-01-01T12:00:00 Terrestrial Time (<em>not</em> UTC).
  190.      *
  191.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  192.      *
  193.      * @see #createJulianEpoch(double)
  194.      * @see #createBesselianEpoch(double)
  195.      * @see TimeScales#getJ2000Epoch()
  196.      */
  197.     @DefaultDataContext
  198.     public static final AbsoluteDate J2000_EPOCH = DataContext.getDefault().getTimeScales().getJ2000Epoch();

  199.     /** Java Reference epoch: 1970-01-01T00:00:00 Universal Time Coordinate.
  200.      * <p>
  201.      * Between 1968-02-01 and 1972-01-01, UTC-TAI = 4.213 170 0s + (MJD - 39 126) x 0.002 592s.
  202.      * As on 1970-01-01 MJD = 40587, UTC-TAI = 8.000082s
  203.      * </p>
  204.      *
  205.      * <p>This constant uses the {@link DataContext#getDefault() default data context}.
  206.      *
  207.      * @see TimeScales#getJavaEpoch()
  208.      */
  209.     @DefaultDataContext
  210.     public static final AbsoluteDate JAVA_EPOCH = DataContext.getDefault().getTimeScales().getJavaEpoch();

  211.     /**
  212.      * An arbitrary finite date. Uses when a non-null date is needed but its value doesn't
  213.      * matter.
  214.      */
  215.     public static final AbsoluteDate ARBITRARY_EPOCH = new AbsoluteDate(TimeOffset.ZERO);

  216.     /** Dummy date at infinity in the past direction.
  217.      * @see TimeScales#getPastInfinity()
  218.      */
  219.     public static final AbsoluteDate PAST_INFINITY = ARBITRARY_EPOCH.shiftedBy(Double.NEGATIVE_INFINITY);

  220.     /** Dummy date at infinity in the future direction.
  221.      * @see TimeScales#getFutureInfinity()
  222.      */
  223.     public static final AbsoluteDate FUTURE_INFINITY = ARBITRARY_EPOCH.shiftedBy(Double.POSITIVE_INFINITY);

  224.     /** Serializable UID. */
  225.     private static final long serialVersionUID = 20240711L;

  226.     /** Create an instance with a default value ({@link #J2000_EPOCH}).
  227.      *
  228.      * <p>This constructor uses the {@link DataContext#getDefault() default data context}.
  229.      *
  230.      * @see #AbsoluteDate(DateTimeComponents, TimeScale)
  231.      */
  232.     @DefaultDataContext
  233.     public AbsoluteDate() {
  234.         super(J2000_EPOCH.getSeconds(), J2000_EPOCH.getAttoSeconds());
  235.     }

  236.     /** Build an instance from a location (parsed from a string) in a {@link TimeScale time scale}.
  237.      * <p>
  238.      * The supported formats for location are mainly the ones defined in ISO-8601 standard,
  239.      * the exact subset is explained in {@link DateTimeComponents#parseDateTime(String)},
  240.      * {@link DateComponents#parseDate(String)} and {@link TimeComponents#parseTime(String)}.
  241.      * </p>
  242.      * <p>
  243.      * As CCSDS ASCII calendar segmented time code is a trimmed down version of ISO-8601,
  244.      * it is also supported by this constructor.
  245.      * </p>
  246.      * @param location location in the time scale, must be in a supported format
  247.      * @param timeScale time scale
  248.      * @exception IllegalArgumentException if location string is not in a supported format
  249.      */
  250.     public AbsoluteDate(final String location, final TimeScale timeScale) {
  251.         this(DateTimeComponents.parseDateTime(location), timeScale);
  252.     }

  253.     /** Build an instance from a location in a {@link TimeScale time scale}.
  254.      * @param location location in the time scale
  255.      * @param timeScale time scale
  256.      */
  257.     public AbsoluteDate(final DateTimeComponents location, final TimeScale timeScale) {
  258.         this(location.getDate(), location.getTime(), timeScale);
  259.     }

  260.     /** Build an instance from a location in a {@link TimeScale time scale}.
  261.      * @param date date location in the time scale
  262.      * @param time time location in the time scale
  263.      * @param timeScale time scale
  264.      */
  265.     public AbsoluteDate(final DateComponents date, final TimeComponents time,
  266.                         final TimeScale timeScale) {
  267.         // epoch is at 12:00 (close to J2000.0, but in TAI scale), hence the subtraction of 720 minutes
  268.         super(new TimeOffset(60L * ((date.getJ2000Day() * 24L + time.getHour()) * 60L +
  269.                               time.getMinute() - time.getMinutesFromUTC() - 720L),
  270.                              0L),
  271.               time.getSplitSecond(),
  272.               timeScale.offsetToTAI(date, time));
  273.     }

  274.     /** Build an instance from a location in a {@link TimeScale time scale}.
  275.      * @param year year number (may be 0 or negative for BC years)
  276.      * @param month month number from 1 to 12
  277.      * @param day day number from 1 to 31
  278.      * @param hour hour number from 0 to 23
  279.      * @param minute minute number from 0 to 59
  280.      * @param second second number from 0.0 to 60.0 (excluded)
  281.      * @param timeScale time scale
  282.      * @exception IllegalArgumentException if inconsistent arguments
  283.      * are given (parameters out of range)
  284.      */
  285.     public AbsoluteDate(final int year, final int month, final int day,
  286.                         final int hour, final int minute, final double second,
  287.                         final TimeScale timeScale) throws IllegalArgumentException {
  288.         this(year, month, day, hour, minute, new TimeOffset(second), timeScale);
  289.     }

  290.     /** Build an instance from a location in a {@link TimeScale time scale}.
  291.      * @param year year number (may be 0 or negative for BC years)
  292.      * @param month month number from 1 to 12
  293.      * @param day day number from 1 to 31
  294.      * @param hour hour number from 0 to 23
  295.      * @param minute minute number from 0 to 59
  296.      * @param second second number from 0.0 to 60.0 (excluded)
  297.      * @param timeScale time scale
  298.      * @exception IllegalArgumentException if inconsistent arguments
  299.      * are given (parameters out of range)
  300.      * @since 13.0
  301.      */
  302.     public AbsoluteDate(final int year, final int month, final int day,
  303.                         final int hour, final int minute, final TimeOffset second,
  304.                         final TimeScale timeScale) throws IllegalArgumentException {
  305.         this(new DateComponents(year, month, day), new TimeComponents(hour, minute, second), timeScale);
  306.     }

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

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

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

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

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

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

  386.     /** Build an instance from an {@link Instant instant} in a {@link TimeScale time scale}.
  387.      *
  388.      * <p>This constructor is provided for those users who wish to provide their own time
  389.      * scale to control how an {@link Instant} is converted to an {@link AbsoluteDate}.
  390.      *
  391.      * <p>Note that {@link Instant} is documented to use the "Java Time Scale", which is a
  392.      * non-standard time scale that is not well defined, and Orekit does not support it.
  393.      * Notably it uses seconds that are not SI seconds. {@link Instant}'s time scale may
  394.      * vary based on many factors, but is documented to be within 1 s of UTC after
  395.      * 1972-11-04T12:00.
  396.      *
  397.      * @param instant in the {@code timeScale}.
  398.      * @param timeScale of the {@code Instant}.
  399.      * @since 12.0
  400.      * @see #AbsoluteDate(Instant, UTCScale)
  401.      * @see #AbsoluteDate(Instant)
  402.      */
  403.     public AbsoluteDate(final Instant instant, final TimeScale timeScale) {
  404.         this(new DateComponents(DateComponents.JAVA_EPOCH, (int) (instant.getEpochSecond() / 86400L)),
  405.                 new TimeComponents(TimeOffset.SECOND.multiply(instant.getEpochSecond() % 86400L).
  406.                         add(new TimeOffset(instant.getNano(), TimeUnit.NANOSECONDS))),
  407.                 timeScale);
  408.     }

  409.     /** Build an instance from an {@link Instant instant} in utc time scale.
  410.      * @param instant instant in the time scale
  411.      * @since 12.1
  412.      * @see #AbsoluteDate(Instant, UTCScale)
  413.      * @see #AbsoluteDate(Instant, TimeScale)
  414.      */
  415.     @DefaultDataContext
  416.     public AbsoluteDate(final Instant instant) {
  417.         this(instant, TimeScalesFactory.getUTC());
  418.     }

  419.     /** Build an instance from an {@link Instant instant} in the {@link UTCScale time scale}.
  420.      *
  421.      * <p>See the caveats of using {@link Instant} as described in the other {@link
  422.      * #AbsoluteDate(Instant, TimeScale) constructor}.
  423.      *
  424.      * @param instant instant in the time scale
  425.      * @param utcScale utc time scale
  426.      * @since 12.1
  427.      * @see #AbsoluteDate(Instant, TimeScale)
  428.      * @see #AbsoluteDate(Instant)
  429.      */
  430.     public AbsoluteDate(final Instant instant, final UTCScale utcScale) {
  431.         this(instant, (TimeScale) utcScale);
  432.     }

  433.     /** Build an instance from an elapsed duration since another instant.
  434.      * <p>It is important to note that the elapsed duration is <em>not</em>
  435.      * the difference between two readings on a time scale. As an example,
  436.      * the duration between the two instants leading to the readings
  437.      * 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the {@link UTCScale UTC}
  438.      * time scale is <em>not</em> 1 second, but a stop watch would have measured
  439.      * an elapsed duration of 2 seconds between these two instances because a leap
  440.      * second was introduced at the end of 2005 in this time scale.</p>
  441.      * <p>This constructor is the reverse of the {@link #durationFrom(AbsoluteDate)}
  442.      * method.</p>
  443.      * @param since start instant of the measured duration
  444.      * @param elapsedDuration physically elapsed duration from the <code>since</code>
  445.      * instant, as measured in a regular time scale
  446.      * @see #durationFrom(AbsoluteDate)
  447.      */
  448.     public AbsoluteDate(final AbsoluteDate since, final double elapsedDuration) {
  449.         this(since, new TimeOffset(elapsedDuration));
  450.     }

  451.     /** Build an instance from an elapsed duration since another instant.
  452.      * <p>It is important to note that the elapsed duration is <em>not</em>
  453.      * the difference between two readings on a time scale. As an example,
  454.      * the duration between the two instants leading to the readings
  455.      * 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the {@link UTCScale UTC}
  456.      * time scale is <em>not</em> 1 second, but a stop watch would have measured
  457.      * an elapsed duration of 2 seconds between these two instances because a leap
  458.      * second was introduced at the end of 2005 in this time scale.</p>
  459.      * <p>This constructor is the reverse of the {@link #durationFrom(AbsoluteDate)}
  460.      * method.</p>
  461.      * @param since start instant of the measured duration
  462.      * @param elapsedDuration physically elapsed duration from the <code>since</code>
  463.      * instant, as measured in a regular time scale
  464.      * @see #durationFrom(AbsoluteDate)
  465.      * @since 13.0
  466.      */
  467.     public AbsoluteDate(final AbsoluteDate since, final TimeOffset elapsedDuration) {
  468.         super(since, elapsedDuration);
  469.     }

  470.     /** Build an instance from an elapsed duration since another instant.
  471.      * <p>It is important to note that the elapsed duration is <em>not</em>
  472.      * the difference between two readings on a time scale. As an example,
  473.      * the duration between the two instants leading to the readings
  474.      * 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the {@link UTCScale UTC}
  475.      * time scale is <em>not</em> 1 second, but a stop watch would have measured
  476.      * an elapsed duration of 2 seconds between these two instances because a leap
  477.      * second was introduced at the end of 2005 in this time scale.</p>
  478.      * <p>This constructor is the reverse of the {@link #durationFrom(AbsoluteDate, TimeUnit)}
  479.      * method.</p>
  480.      * @param since start instant of the measured duration
  481.      * @param elapsedDuration physically elapsed duration from the <code>since</code>
  482.      * instant, as measured in a regular time scale
  483.      * @param timeUnit {@link TimeUnit} of the elapsedDuration
  484.      * @see #durationFrom(AbsoluteDate, TimeUnit)
  485.      * @since 12.1
  486.      */
  487.     public AbsoluteDate(final AbsoluteDate since, final long elapsedDuration, final TimeUnit timeUnit) {
  488.         this(since, new TimeOffset(elapsedDuration, timeUnit));
  489.     }

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

  510.     /** Build an instance from an apparent clock offset with respect to another
  511.      * instant <em>in the perspective of a specific {@link TimeScale time scale}</em>.
  512.      * <p>It is important to note that the apparent clock offset <em>is</em> the
  513.      * difference between two readings on a time scale and <em>not</em> an elapsed
  514.      * duration. As an example, the apparent clock offset between the two instants
  515.      * leading to the readings 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the
  516.      * {@link UTCScale UTC} time scale is 1 second, but the elapsed duration is 2
  517.      * seconds because a leap second has been introduced at the end of 2005 in this
  518.      * time scale.</p>
  519.      * <p>This constructor is the reverse of the {@link #offsetFrom(AbsoluteDate,
  520.      * TimeScale)} method.</p>
  521.      * @param reference reference instant
  522.      * @param apparentOffset apparent clock offset from the reference instant
  523.      * (difference between two readings in the specified time scale)
  524.      * @param timeScale time scale with respect to which the offset is defined
  525.      * @see #offsetFrom(AbsoluteDate, TimeScale)
  526.      * @since 13.0
  527.      */
  528.     public AbsoluteDate(final AbsoluteDate reference, final TimeOffset apparentOffset, final TimeScale timeScale) {
  529.         this(new DateTimeComponents(reference.getComponents(timeScale), apparentOffset),
  530.              timeScale);
  531.     }

  532.     /** Build a date from an offset since a reference epoch.
  533.      * @param offset offset since reference epoch 2000-01-01T12:00:00 TAI.
  534.      * (beware, it is not {@link #J2000_EPOCH} since it is in TAI and not in TT)
  535.      * @since 13.0
  536.      */
  537.     public AbsoluteDate(final TimeOffset offset) {
  538.         super(offset);
  539.     }

  540.     /** Build an instance from a CCSDS Unsegmented Time Code (CUC).
  541.      * <p>
  542.      * CCSDS Unsegmented Time Code is defined in the blue book:
  543.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  544.      * </p>
  545.      * <p>
  546.      * If the date to be parsed is formatted using version 3 of the standard
  547.      * (CCSDS 301.0-B-3 published in 2002) or if the extension of the preamble
  548.      * field introduced in version 4 of the standard is not used, then the
  549.      * {@code preambleField2} parameter can be set to 0.
  550.      * </p>
  551.      *
  552.      * <p>This method uses the {@link DataContext#getDefault() default data context} if
  553.      * the CCSDS epoch is used.
  554.      *
  555.      * @param preambleField1 first byte of the field specifying the format, often
  556.      * not transmitted in data interfaces, as it is constant for a given data interface
  557.      * @param preambleField2 second byte of the field specifying the format
  558.      * (added in revision 4 of the CCSDS standard in 2010), often not transmitted in data
  559.      * interfaces, as it is constant for a given data interface (value ignored if presence
  560.      * not signaled in {@code preambleField1})
  561.      * @param timeField byte array containing the time code
  562.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field
  563.      * specifies the {@link #CCSDS_EPOCH CCSDS reference epoch} is used (and hence
  564.      * may be null in this case)
  565.      * @return an instance corresponding to the specified date
  566.      * @see #parseCCSDSUnsegmentedTimeCode(byte, byte, byte[], AbsoluteDate, AbsoluteDate)
  567.      */
  568.     @DefaultDataContext
  569.     public static AbsoluteDate parseCCSDSUnsegmentedTimeCode(final byte preambleField1,
  570.                                                              final byte preambleField2,
  571.                                                              final byte[] timeField,
  572.                                                              final AbsoluteDate agencyDefinedEpoch) {
  573.         return parseCCSDSUnsegmentedTimeCode(preambleField1, preambleField2, timeField,
  574.                                              agencyDefinedEpoch,
  575.                                              DataContext.getDefault().getTimeScales().getCcsdsEpoch());
  576.     }

  577.     /**
  578.      * Build an instance from a CCSDS Unsegmented Time Code (CUC).
  579.      * <p>
  580.      * CCSDS Unsegmented Time Code is defined in the blue book: CCSDS Time Code Format
  581.      * (CCSDS 301.0-B-4) published in November 2010
  582.      * </p>
  583.      * <p>
  584.      * If the date to be parsed is formatted using version 3 of the standard (CCSDS
  585.      * 301.0-B-3 published in 2002) or if the extension of the preamble field introduced
  586.      * in version 4 of the standard is not used, then the {@code preambleField2} parameter
  587.      * can be set to 0.
  588.      * </p>
  589.      *
  590.      * @param preambleField1     first byte of the field specifying the format, often not
  591.      *                           transmitted in data interfaces, as it is constant for a
  592.      *                           given data interface
  593.      * @param preambleField2     second byte of the field specifying the format (added in
  594.      *                           revision 4 of the CCSDS standard in 2010), often not
  595.      *                           transmitted in data interfaces, as it is constant for a
  596.      *                           given data interface (value ignored if presence not
  597.      *                           signaled in {@code preambleField1})
  598.      * @param timeField          byte array containing the time code
  599.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field specifies
  600.      *                           the {@link DateComponents#CCSDS_EPOCH CCSDS reference epoch} is used
  601.      *                           (and hence may be null in this case, but then {@code ccsdsEpoch} must be non-null)
  602.      * @param ccsdsEpoch         reference epoch, ignored if the preamble field specifies
  603.      *                           the agency epoch is used (and hence may be null in this case,
  604.      *                           but then {@code agencyDefinedEpoch} must be non-null).
  605.      * @return an instance corresponding to the specified date
  606.      * @since 10.1
  607.      */
  608.     public static AbsoluteDate parseCCSDSUnsegmentedTimeCode(final byte preambleField1,
  609.                                                              final byte preambleField2,
  610.                                                              final byte[] timeField,
  611.                                                              final AbsoluteDate agencyDefinedEpoch,
  612.                                                              final AbsoluteDate ccsdsEpoch) {
  613.         final CcsdsUnsegmentedTimeCode<AbsoluteDate> timeCode =
  614.             new CcsdsUnsegmentedTimeCode<>(preambleField1, preambleField2, timeField, agencyDefinedEpoch, ccsdsEpoch);
  615.         return timeCode.getEpoch().shiftedBy(timeCode.getTime());

  616.     }

  617.     /** Build an instance from a CCSDS Day Segmented Time Code (CDS).
  618.      * <p>
  619.      * CCSDS Day Segmented Time Code is defined in the blue book:
  620.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  621.      * </p>
  622.      *
  623.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  624.      *
  625.      * @param preambleField field specifying the format, often not transmitted in
  626.      * data interfaces, as it is constant for a given data interface
  627.      * @param timeField byte array containing the time code
  628.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field
  629.      * specifies the {@link #CCSDS_EPOCH CCSDS reference epoch} is used (and hence
  630.      * may be null in this case)
  631.      * @return an instance corresponding to the specified date
  632.      * @see #parseCCSDSDaySegmentedTimeCode(byte, byte[], DateComponents, TimeScale)
  633.      */
  634.     @DefaultDataContext
  635.     public static AbsoluteDate parseCCSDSDaySegmentedTimeCode(final byte preambleField, final byte[] timeField,
  636.                                                               final DateComponents agencyDefinedEpoch) {
  637.         return parseCCSDSDaySegmentedTimeCode(preambleField, timeField,
  638.                                               agencyDefinedEpoch, DataContext.getDefault().getTimeScales().getUTC());
  639.     }

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

  659.         final CcsdsSegmentedTimeCode timeCode = new CcsdsSegmentedTimeCode(preambleField, timeField,
  660.                                                                            agencyDefinedEpoch);
  661.         return new AbsoluteDate(timeCode.getDate(), timeCode.getTime(), utc);
  662.     }

  663.     /** Build an instance from a CCSDS Calendar Segmented Time Code (CCS).
  664.      * <p>
  665.      * CCSDS Calendar Segmented Time Code is defined in the blue book:
  666.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  667.      * </p>
  668.      *
  669.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  670.      *
  671.      * @param preambleField field specifying the format, often not transmitted in
  672.      * data interfaces, as it is constant for a given data interface
  673.      * @param timeField byte array containing the time code
  674.      * @return an instance corresponding to the specified date
  675.      * @see #parseCCSDSCalendarSegmentedTimeCode(byte, byte[], TimeScale)
  676.      */
  677.     @DefaultDataContext
  678.     public static AbsoluteDate parseCCSDSCalendarSegmentedTimeCode(final byte preambleField, final byte[] timeField) {
  679.         return parseCCSDSCalendarSegmentedTimeCode(preambleField, timeField,
  680.                                                    DataContext.getDefault().getTimeScales().getUTC());
  681.     }

  682.     /** Build an instance from a CCSDS Calendar Segmented Time Code (CCS).
  683.      * <p>
  684.      * CCSDS Calendar Segmented Time Code is defined in the blue book:
  685.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  686.      * </p>
  687.      * @param preambleField field specifying the format, often not transmitted in
  688.      * data interfaces, as it is constant for a given data interface
  689.      * @param timeField byte array containing the time code
  690.      * @param utc      time scale used to compute date and time components.
  691.      * @return an instance corresponding to the specified date
  692.      * @since 10.1
  693.      */
  694.     public static AbsoluteDate parseCCSDSCalendarSegmentedTimeCode(final byte preambleField,
  695.                                                                    final byte[] timeField,
  696.                                                                    final TimeScale utc) {
  697.         final CcsdsSegmentedTimeCode timeCode = new CcsdsSegmentedTimeCode(preambleField, timeField);
  698.         return new AbsoluteDate(timeCode.getDate(), timeCode.getTime(), utc);
  699.     }

  700.     /** Build an instance corresponding to a Julian Day date.
  701.      * @param jd Julian day
  702.      * @param secondsSinceNoon seconds in the Julian day
  703.      * (BEWARE, Julian days start at noon, so 0.0 is noon)
  704.      * @param timeScale time scale in which the seconds in day are defined
  705.      * @return a new instant
  706.      */
  707.     public static AbsoluteDate createJDDate(final int jd, final double secondsSinceNoon,
  708.                                             final TimeScale timeScale) {
  709.         return new AbsoluteDate(new DateComponents(DateComponents.JULIAN_EPOCH, jd),
  710.                 TimeComponents.H12, timeScale).shiftedBy(secondsSinceNoon);
  711.     }

  712.     /** Build an instance corresponding to a Julian Day date.
  713.      * <p>
  714.      * This function should be preferred to {@link #createMJDDate(int, double, TimeScale)} when the target time scale
  715.      * has a non-constant offset with respect to TAI.
  716.      * </p>
  717.      * <p>
  718.      * The idea is to introduce a pivot time scale that is close to the target time scale but has a constant bias with TAI.
  719.      * </p>
  720.      * <p>
  721.      * For example, to get a date from an MJD in TDB time scale, it's advised to use the TT time scale
  722.      * as a pivot scale. TT is very close to TDB and has constant offset to TAI.
  723.      * </p>
  724.      * @param jd Julian day
  725.      * @param secondsSinceNoon seconds in the Julian day
  726.      * (BEWARE, Julian days start at noon, so 0.0 is noon)
  727.      * @param timeScale timescale in which the seconds in day are defined
  728.      * @param pivotTimeScale pivot timescale used as intermediate timescale
  729.      * @return a new instant
  730.      */
  731.     public static AbsoluteDate createJDDate(final int jd, final double secondsSinceNoon,
  732.                                             final TimeScale timeScale,
  733.                                             final TimeScale pivotTimeScale) {
  734.         // Get the date in pivot timescale
  735.         final AbsoluteDate dateInPivotTimeScale = createJDDate(jd, secondsSinceNoon, pivotTimeScale);

  736.         // Compare offsets to TAI of the two time scales
  737.         final TimeOffset offsetFromTAI = timeScale.
  738.                                         offsetFromTAI(dateInPivotTimeScale).
  739.                                         subtract(pivotTimeScale.offsetFromTAI(dateInPivotTimeScale));

  740.         // Return date in desired timescale
  741.         return new AbsoluteDate(dateInPivotTimeScale, offsetFromTAI.negate());
  742.     }

  743.     /** Build an instance corresponding to a Modified Julian Day date.
  744.      * @param mjd modified Julian day
  745.      * @param secondsInDay seconds in the day
  746.      * @param timeScale time scale in which the seconds in day are defined
  747.      * @return a new instant
  748.      * @exception OrekitIllegalArgumentException if seconds number is out of range
  749.      */
  750.     public static AbsoluteDate createMJDDate(final int mjd, final double secondsInDay,
  751.                                              final TimeScale timeScale)
  752.         throws OrekitIllegalArgumentException {
  753.         return createMJDDate(mjd, new TimeOffset(secondsInDay), timeScale);
  754.     }

  755.     /** Build an instance corresponding to a Modified Julian Day date.
  756.      * @param mjd modified Julian day
  757.      * @param secondsInDay seconds in the day
  758.      * @param timeScale time scale in which the seconds in day are defined
  759.      * @return a new instant
  760.      * @exception OrekitIllegalArgumentException if seconds number is out of range
  761.      * @since 13.0
  762.      */
  763.     public static AbsoluteDate createMJDDate(final int mjd, final TimeOffset secondsInDay,
  764.                                              final TimeScale timeScale)
  765.         throws OrekitIllegalArgumentException {
  766.         final DateComponents dc = new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd);
  767.         final TimeComponents tc;
  768.         if (secondsInDay.compareTo(TimeOffset.DAY) >= 0) {
  769.             // check we are really allowed to use this number of seconds
  770.             final TimeOffset secondsA = new TimeOffset(86399); // 23:59:59, i.e. 59s in the last minute of the day
  771.             final TimeOffset secondsB = secondsInDay.subtract(secondsA);
  772.             final TimeComponents safeTC = new TimeComponents(secondsA);
  773.             final AbsoluteDate safeDate = new AbsoluteDate(dc, safeTC, timeScale);
  774.             if (timeScale.minuteDuration(safeDate) > 59 + secondsB.toDouble()) {
  775.                 // we are within the last minute of the day, the number of seconds is OK
  776.                 return safeDate.shiftedBy(secondsB);
  777.             } else {
  778.                 // let TimeComponents trigger an OrekitIllegalArgumentException
  779.                 // for the wrong number of seconds
  780.                 tc = new TimeComponents(secondsA.add(secondsB));
  781.             }
  782.         } else {
  783.             tc = new TimeComponents(secondsInDay);
  784.         }

  785.         // create the date
  786.         return new AbsoluteDate(dc, tc, timeScale);

  787.     }

  788.     /** Create an instance as the median data between two existing instances.
  789.      * @param date1 first instance
  790.      * @param date2 second instance
  791.      * @return median date between first and second instance
  792.      * @since 13.0
  793.      */
  794.     public static AbsoluteDate createMedian(final AbsoluteDate date1, final AbsoluteDate date2) {
  795.         return new AbsoluteDate(date2.add(date1).divide(2));
  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}.</p>
  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}.</p>
  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().createBesselianEpoch(besselianEpoch);
  843.     }

  844.     /** {@inheritDoc} */
  845.     @Override
  846.     public AbsoluteDate shiftedBy(final double dt) {
  847.         return new AbsoluteDate(this, dt);
  848.     }

  849.     /** {@inheritDoc} */
  850.     @Override
  851.     public AbsoluteDate shiftedBy(final TimeOffset dt) {
  852.         return new AbsoluteDate(this, dt);
  853.     }

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

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

  889.     /** Compute the physically elapsed duration between two instants.
  890.      * <p>The returned duration is the number of seconds physically
  891.      * elapsed between the two instants, measured in a regular time
  892.      * scale with respect to surface of the Earth (i.e either the {@link
  893.      * TAIScale TAI scale}, the {@link TTScale TT scale} or the {@link
  894.      * GPSScale GPS scale}). It is the only method that gives a
  895.      * duration with a physical meaning.</p>
  896.      * <p>This method gives the same result (with less computation)
  897.      * as calling {@link #offsetFrom(AbsoluteDate, TimeScale)}
  898.      * with a second argument set to one of the regular scales cited
  899.      * above.</p>
  900.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  901.      * double)} constructor.</p>
  902.      * @param instant instant to subtract from the instance
  903.      * @return offset in seconds between the two instants (positive
  904.      * if the instance is posterior to the argument)
  905.      * @see #durationFrom(AbsoluteDate)
  906.      * @see #offsetFrom(AbsoluteDate, TimeScale)
  907.      * @see #AbsoluteDate(AbsoluteDate, double)
  908.      * @since 13.0
  909.      */
  910.     public TimeOffset accurateDurationFrom(final AbsoluteDate instant) {
  911.         return this.subtract(instant);
  912.     }

  913.     /** Compute the physically elapsed duration between two instants.
  914.      * <p>The returned duration is the duration physically
  915.      * elapsed between the two instants, using the given time unit and rounded to the nearest integer, measured in a regular time
  916.      * scale with respect to surface of the Earth (i.e either the {@link
  917.      * TAIScale TAI scale}, the {@link TTScale TT scale} or the {@link
  918.      * GPSScale GPS scale}). It is the only method that gives a
  919.      * duration with a physical meaning.</p>
  920.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  921.      * long, TimeUnit)} constructor.</p>
  922.      * @param instant instant to subtract from the instance
  923.      * @param timeUnit {@link TimeUnit} precision for the offset
  924.      * @return offset in the given timeunit between the two instants (positive
  925.      * if the instance is posterior to the argument), rounded to the nearest integer {@link TimeUnit}
  926.      * @see #AbsoluteDate(AbsoluteDate, long, TimeUnit)
  927.      * @since 12.1
  928.      */
  929.     public long durationFrom(final AbsoluteDate instant, final TimeUnit timeUnit) {
  930.         return accurateDurationFrom(instant).getRoundedTime(timeUnit);
  931.     }

  932.     /** Compute the apparent <em>clock</em> offset between two instant <em>in the
  933.      * perspective of a specific {@link TimeScale time scale}</em>.
  934.      * <p>The offset is the number of seconds counted in the given
  935.      * time scale between the locations of the two instants, with
  936.      * all time scale irregularities removed (i.e. considering all
  937.      * days are exactly 86400 seconds long). This method will give
  938.      * a result that may not have a physical meaning if the time scale
  939.      * is irregular. For example since a leap second was introduced at
  940.      * the end of 2005, the apparent clock offset between 2005-12-31T23:59:59
  941.      * and 2006-01-01T00:00:00 is 1 second and is the value this method
  942.      * will return. On the other hand, the physical duration
  943.      * of the corresponding time interval as returned by the {@link
  944.      * #durationFrom(AbsoluteDate)} method is 2 seconds.</p>
  945.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  946.      * double, TimeScale)} constructor.</p>
  947.      * @param instant instant to subtract from the instance
  948.      * @param timeScale time scale with respect to which the offset should
  949.      * be computed
  950.      * @return apparent clock offset in seconds between the two instants
  951.      * (positive if the instance is posterior to the argument)
  952.      * @see #durationFrom(AbsoluteDate)
  953.      * @see #accurateOffsetFrom(AbsoluteDate, TimeScale)
  954.      * @see #AbsoluteDate(AbsoluteDate, TimeOffset, TimeScale)
  955.      */
  956.     public double offsetFrom(final AbsoluteDate instant, final TimeScale timeScale) {
  957.         return accurateOffsetFrom(instant, timeScale).toDouble();
  958.     }

  959.     /** Compute the apparent <em>clock</em> offset between two instant <em>in the
  960.      * perspective of a specific {@link TimeScale time scale}</em>.
  961.      * <p>The offset is the number of seconds counted in the given
  962.      * time scale between the locations of the two instants, with
  963.      * all time scale irregularities removed (i.e. considering all
  964.      * days are exactly 86400 seconds long). This method will give
  965.      * a result that may not have a physical meaning if the time scale
  966.      * is irregular. For example since a leap second was introduced at
  967.      * the end of 2005, the apparent clock offset between 2005-12-31T23:59:59
  968.      * and 2006-01-01T00:00:00 is 1 second and is the value this method
  969.      * will return. On the other hand, the physical duration
  970.      * of the corresponding time interval as returned by the {@link
  971.      * #durationFrom(AbsoluteDate)} method is 2 seconds.</p>
  972.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  973.      * double, TimeScale)} constructor.</p>
  974.      * @param instant instant to subtract from the instance
  975.      * @param timeScale time scale with respect to which the offset should
  976.      * be computed
  977.      * @return apparent clock offset in seconds between the two instants
  978.      * (positive if the instance is posterior to the argument)
  979.      * @see #durationFrom(AbsoluteDate)
  980.      * @see #offsetFrom(AbsoluteDate, TimeScale)
  981.      * @see #AbsoluteDate(AbsoluteDate, TimeOffset, TimeScale)
  982.      * @since 13.0
  983.      */
  984.     public TimeOffset accurateOffsetFrom(final AbsoluteDate instant, final TimeScale timeScale) {
  985.         return new TimeOffset(this,
  986.                               timeScale.offsetFromTAI(this),
  987.                               instant.negate(),
  988.                               timeScale.offsetFromTAI(instant).negate());
  989.     }

  990.     /** Compute the offset between two time scales at the current instant.
  991.      * <p>The offset is defined as <i>l₁-l₂</i>
  992.      * where <i>l₁</i> is the location of the instant in
  993.      * the <code>scale1</code> time scale and <i>l₂</i> is the
  994.      * location of the instant in the <code>scale2</code> time scale.</p>
  995.      * @param scale1 first time scale
  996.      * @param scale2 second time scale
  997.      * @return offset in seconds between the two time scales at the
  998.      * current instant
  999.      */
  1000.     public double timeScalesOffset(final TimeScale scale1, final TimeScale scale2) {
  1001.         return scale1.offsetFromTAI(this).subtract(scale2.offsetFromTAI(this)).toDouble();
  1002.     }

  1003.     /** Convert the instance to a Java {@link java.util.Date Date}.
  1004.      * <p>Conversion to the Date class induces a loss of precision because
  1005.      * the Date class does not provide sub-millisecond information. Java Dates
  1006.      * are considered to be locations in some times scales.</p>
  1007.      * @param timeScale time scale to use
  1008.      * @return a {@link java.util.Date Date} instance representing the location
  1009.      * of the instant in the time scale
  1010.      */
  1011.     public Date toDate(final TimeScale timeScale) {
  1012.         final TimeOffset time = add(timeScale.offsetFromTAI(this));
  1013.         return new Date(FastMath.round((time.toDouble() + 10957.5 * Constants.JULIAN_DAY) * 1000));
  1014.     }

  1015.     /**
  1016.      * Convert the instance to a Java {@link java.time.Instant Instant}.
  1017.      * Nanosecond precision is preserved during this conversion
  1018.      *
  1019.      * @return a {@link java.time.Instant Instant} instance representing the location
  1020.      * of the instant in the utc time scale
  1021.      * @since 12.1
  1022.      */
  1023.     @DefaultDataContext
  1024.     public Instant toInstant() {
  1025.         return toInstant(TimeScalesFactory.getTimeScales());
  1026.     }

  1027.     /**
  1028.      * Convert the instance to a Java {@link java.time.Instant Instant}.
  1029.      * Nanosecond precision is preserved during this conversion
  1030.      *
  1031.      * @param timeScales the timescales to use
  1032.      * @return a {@link java.time.Instant Instant} instance representing the location
  1033.      * of the instant in the utc time scale
  1034.      * @since 12.1
  1035.      */
  1036.     public Instant toInstant(final TimeScales timeScales) {
  1037.         final UTCScale utc = timeScales.getUTC();
  1038.         final String stringWithoutUtcOffset = toStringWithoutUtcOffset(utc, 9);

  1039.         final LocalDateTime localDateTime = LocalDateTime.parse(stringWithoutUtcOffset, DateTimeFormatter.ISO_LOCAL_DATE_TIME);
  1040.         return localDateTime.toInstant(ZoneOffset.UTC);
  1041.     }

  1042.     /** Split the instance into date/time components.
  1043.      * @param timeScale time scale to use
  1044.      * @return date/time components
  1045.      */
  1046.     public DateTimeComponents getComponents(final TimeScale timeScale) {

  1047.         if (!isFinite()) {
  1048.             // special handling for NaN, past and future infinity
  1049.             if (isNaN()) {
  1050.                 return new DateTimeComponents(DateComponents.J2000_EPOCH, TimeComponents.NaN);
  1051.             } else if (isNegativeInfinity()) {
  1052.                 return new DateTimeComponents(DateComponents.MIN_EPOCH, TimeComponents.H00);
  1053.             } else {
  1054.                 // the fact we truncate at 59.999 seconds is for compatibility reasons
  1055.                 // with pre-13.0 Orekit versions. Indeed, this date is fake and more than
  1056.                 // 5 millions years in the future, so milliseconds are not really relevant
  1057.                 // truncating makes cleaner text output
  1058.                 return new DateTimeComponents(DateComponents.MAX_EPOCH,
  1059.                                               new TimeComponents(23, 59,
  1060.                                                                  new TimeOffset(59, TimeOffset.SECOND,
  1061.                                                                                 999, TimeOffset.MILLISECOND)));
  1062.             }
  1063.         }

  1064.         final TimeOffset sum = add(timeScale.offsetFromTAI(this));

  1065.         // split date and time
  1066.         final long offset2000A = sum.getSeconds() + 43200L;
  1067.         long time = offset2000A % 86400L;
  1068.         if (time < 0L) {
  1069.             time += 86400L;
  1070.         }
  1071.         final int date = (int) ((offset2000A - time) / 86400L);

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

  1074.         // extract time element, accounting for leap seconds
  1075.         final TimeOffset leap = timeScale.insideLeap(this) ? timeScale.getLeap(this) : TimeOffset.ZERO;
  1076.         final int minuteDuration = timeScale.minuteDuration(this);
  1077.         final TimeComponents timeComponents = new TimeComponents(new TimeOffset(time, sum.getAttoSeconds()),
  1078.                                                                  leap, minuteDuration);

  1079.         // build the components
  1080.         return new DateTimeComponents(dateComponents, timeComponents);

  1081.     }

  1082.     /** Split the instance into date/time components for a local time.
  1083.      *
  1084.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  1085.      *
  1086.      * @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
  1087.      * negative Westward UTC)
  1088.      * @return date/time components
  1089.      * @since 7.2
  1090.      * @see #getComponents(int, TimeScale)
  1091.      */
  1092.     @DefaultDataContext
  1093.     public DateTimeComponents getComponents(final int minutesFromUTC) {
  1094.         return getComponents(minutesFromUTC,
  1095.                 DataContext.getDefault().getTimeScales().getUTC());
  1096.     }

  1097.     /**
  1098.      * Split the instance into date/time components for a local time.
  1099.      *
  1100.      * @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
  1101.      *                       negative Westward UTC)
  1102.      * @param utc            time scale used to compute date and time components.
  1103.      * @return date/time components
  1104.      * @since 10.1
  1105.      */
  1106.     public DateTimeComponents getComponents(final int minutesFromUTC,
  1107.                                             final TimeScale utc) {

  1108.         final DateTimeComponents utcComponents = getComponents(utc);

  1109.         // shift the date according to UTC offset, but WITHOUT touching the seconds,
  1110.         // as they may exceed 60.0 during a leap seconds introduction,
  1111.         // and we want to preserve these special cases
  1112.         final TimeOffset seconds = utcComponents.getTime().getSplitSecond();

  1113.         int minute = utcComponents.getTime().getMinute() + minutesFromUTC;
  1114.         final int hourShift;
  1115.         if (minute < 0) {
  1116.             hourShift = (minute - 59) / 60;
  1117.         } else if (minute > 59) {
  1118.             hourShift = minute / 60;
  1119.         } else {
  1120.             hourShift = 0;
  1121.         }
  1122.         minute -= 60 * hourShift;

  1123.         int hour = utcComponents.getTime().getHour() + hourShift;
  1124.         final int dayShift;
  1125.         if (hour < 0) {
  1126.             dayShift = (hour - 23) / 24;
  1127.         } else if (hour > 23) {
  1128.             dayShift = hour / 24;
  1129.         } else {
  1130.             dayShift = 0;
  1131.         }
  1132.         hour -= 24 * dayShift;

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

  1135.     }

  1136.     /** Split the instance into date/time components for a time zone.
  1137.      *
  1138.      * <p>This method uses the {@link DataContext#getDefault() default data context}.
  1139.      *
  1140.      * @param timeZone time zone
  1141.      * @return date/time components
  1142.      * @since 7.2
  1143.      * @see #getComponents(TimeZone, TimeScale)
  1144.      */
  1145.     @DefaultDataContext
  1146.     public DateTimeComponents getComponents(final TimeZone timeZone) {
  1147.         return getComponents(timeZone, DataContext.getDefault().getTimeScales().getUTC());
  1148.     }

  1149.     /**
  1150.      * Split the instance into date/time components for a time zone.
  1151.      *
  1152.      * @param timeZone time zone
  1153.      * @param utc      time scale used to computed date and time components.
  1154.      * @return date/time components
  1155.      * @since 10.1
  1156.      */
  1157.     public DateTimeComponents getComponents(final TimeZone timeZone,
  1158.                                             final TimeScale utc) {
  1159.         final AbsoluteDate javaEpoch = new AbsoluteDate(DateComponents.JAVA_EPOCH, utc);
  1160.         final long milliseconds = FastMath.round(1000 * offsetFrom(javaEpoch, utc));
  1161.         return getComponents(timeZone.getOffset(milliseconds) / 60000, utc);
  1162.     }

  1163.     /** {@inheritDoc} */
  1164.     public AbsoluteDate getDate() {
  1165.         return this;
  1166.     }

  1167.     /** Check if the instance represents the same time as another.
  1168.      * @param other the instant to compare this date to
  1169.      * @return true if the instance and the argument refer to the same instant
  1170.      * @see #isCloseTo(TimeStamped, double)
  1171.      * @since 10.1
  1172.      */
  1173.     public boolean isEqualTo(final TimeStamped other) {
  1174.         return this.equals(other.getDate());
  1175.     }

  1176.     /** Check if the instance time is close to another.
  1177.      * @param other the instant to compare this date to
  1178.      * @param tolerance the separation, in seconds, under which the two instants will be considered close to each other
  1179.      * @return true if the duration between the instance and the argument is strictly below the tolerance
  1180.      * @see #isEqualTo(TimeStamped)
  1181.      * @since 10.1
  1182.      */
  1183.     public boolean isCloseTo(final TimeStamped other, final double tolerance) {
  1184.         return FastMath.abs(this.durationFrom(other.getDate())) < tolerance;
  1185.     }

  1186.     /** Check if the instance represents a time that is strictly before another.
  1187.      * @param other the instant to compare this date to
  1188.      * @return true if the instance is strictly before the argument when ordering chronologically
  1189.      * @see #isBeforeOrEqualTo(TimeStamped)
  1190.      * @since 10.1
  1191.      */
  1192.     public boolean isBefore(final TimeStamped other) {
  1193.         return this.compareTo(other.getDate()) < 0;
  1194.     }

  1195.     /** Check if the instance represents a time that is strictly after another.
  1196.      * @param other the instant to compare this date to
  1197.      * @return true if the instance is strictly after the argument when ordering chronologically
  1198.      * @see #isAfterOrEqualTo(TimeStamped)
  1199.      * @since 10.1
  1200.      */
  1201.     public boolean isAfter(final TimeStamped other) {
  1202.         return this.compareTo(other.getDate()) > 0;
  1203.     }

  1204.     /** Check if the instance represents a time that is before or equal to another.
  1205.      * @param other the instant to compare this date to
  1206.      * @return true if the instance is before (or equal to) the argument when ordering chronologically
  1207.      * @see #isBefore(TimeStamped)
  1208.      * @since 10.1
  1209.      */
  1210.     public boolean isBeforeOrEqualTo(final TimeStamped other) {
  1211.         return this.isEqualTo(other) || this.isBefore(other);
  1212.     }

  1213.     /** Check if the instance represents a time that is after or equal to another.
  1214.      * @param other the instant to compare this date to
  1215.      * @return true if the instance is after (or equal to) the argument when ordering chronologically
  1216.      * @see #isAfterOrEqualTo(TimeStamped)
  1217.      * @since 10.1
  1218.      */
  1219.     public boolean isAfterOrEqualTo(final TimeStamped other) {
  1220.         return this.isEqualTo(other) || this.isAfter(other);
  1221.     }

  1222.     /** Check if the instance represents a time that is strictly between two others representing
  1223.      * the boundaries of a time span. The two boundaries can be provided in any order: in other words,
  1224.      * whether <code>boundary</code> represents a time that is before or after <code>otherBoundary</code> will
  1225.      * not change the result of this method.
  1226.      * @param boundary one end of the time span
  1227.      * @param otherBoundary the other end of the time span
  1228.      * @return true if the instance is strictly between the two arguments when ordering chronologically
  1229.      * @see #isBetweenOrEqualTo(TimeStamped, TimeStamped)
  1230.      * @since 10.1
  1231.      */
  1232.     public boolean isBetween(final TimeStamped boundary, final TimeStamped otherBoundary) {
  1233.         final TimeStamped beginning;
  1234.         final TimeStamped end;
  1235.         if (boundary.getDate().isBefore(otherBoundary)) {
  1236.             beginning = boundary;
  1237.             end = otherBoundary;
  1238.         } else {
  1239.             beginning = otherBoundary;
  1240.             end = boundary;
  1241.         }
  1242.         return this.isAfter(beginning) && this.isBefore(end);
  1243.     }

  1244.     /** Check if the instance represents a time that is between two others representing
  1245.      * the boundaries of a time span, or equal to one of them. The two boundaries can be provided in any order:
  1246.      * in other words, whether <code>boundary</code> represents a time that is before or after
  1247.      * <code>otherBoundary</code> will not change the result of this method.
  1248.      * @param boundary one end of the time span
  1249.      * @param otherBoundary the other end of the time span
  1250.      * @return true if the instance is between the two arguments (or equal to at least one of them)
  1251.      * when ordering chronologically
  1252.      * @see #isBetween(TimeStamped, TimeStamped)
  1253.      * @since 10.1
  1254.      */
  1255.     public boolean isBetweenOrEqualTo(final TimeStamped boundary, final TimeStamped otherBoundary) {
  1256.         return this.isEqualTo(boundary) || this.isEqualTo(otherBoundary) || this.isBetween(boundary, otherBoundary);
  1257.     }

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

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

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

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

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

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

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

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

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

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

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

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

  1464.     /** Get day of year, preserving continuity as much as possible.
  1465.      * <p>
  1466.      * This is a continuous extension of the integer value returned by
  1467.      * {@link #getComponents(TimeZone) getComponents(utc)}{@link DateTimeComponents#getDate() .getDate()}{@link DateComponents#getDayOfYear() .getDayOfYear()}.
  1468.      * In order to have it remain as close as possible to its integer counterpart,
  1469.      * day 1.0 is considered to occur on January 1st at noon.
  1470.      * </p>
  1471.      * <p>
  1472.      * Continuity is preserved from day to day within a year, but of course
  1473.      * there is a discontinuity at year change, where it switches from 365.49999…
  1474.      * (or 366.49999… on leap years) to 0.5
  1475.      * </p>
  1476.      * @param utc time scale to compute date components
  1477.      * @return day of year, with day 1.0 occurring on January first at noon
  1478.      * @since 13.0
  1479.      */
  1480.     public double getDayOfYear(final TimeScale utc) {
  1481.         final int          year        = getComponents(utc).getDate().getYear();
  1482.         final AbsoluteDate newYearsEve = new AbsoluteDate(year - 1, 12, 31, 12, 0, 0.0, utc);
  1483.         return durationFrom(newYearsEve) / Constants.JULIAN_DAY;
  1484.     }

  1485. }