AbsoluteDate.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.time;

  18. import java.io.Serializable;
  19. import java.util.Date;
  20. import java.util.TimeZone;

  21. import org.hipparchus.util.FastMath;
  22. import org.hipparchus.util.MathArrays;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitIllegalArgumentException;
  25. import org.orekit.errors.OrekitMessages;
  26. import org.orekit.utils.Constants;


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

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

  96.     /** Reference epoch for julian dates: -4712-01-01T12:00:00 Terrestrial Time.
  97.      * <p>Both <code>java.util.Date</code> and {@link DateComponents} classes
  98.      * follow the astronomical conventions and consider a year 0 between
  99.      * years -1 and +1, hence this reference date lies in year -4712 and not
  100.      * in year -4713 as can be seen in other documents or programs that obey
  101.      * a different convention (for example the <code>convcal</code> utility).</p>
  102.      */
  103.     public static final AbsoluteDate JULIAN_EPOCH =
  104.         new AbsoluteDate(DateComponents.JULIAN_EPOCH, TimeComponents.H12, TimeScalesFactory.getTT());

  105.     /** Reference epoch for modified julian dates: 1858-11-17T00:00:00 Terrestrial Time. */
  106.     public static final AbsoluteDate MODIFIED_JULIAN_EPOCH =
  107.         new AbsoluteDate(DateComponents.MODIFIED_JULIAN_EPOCH, TimeComponents.H00, TimeScalesFactory.getTT());

  108.     /** Reference epoch for 1950 dates: 1950-01-01T00:00:00 Terrestrial Time. */
  109.     public static final AbsoluteDate FIFTIES_EPOCH =
  110.         new AbsoluteDate(DateComponents.FIFTIES_EPOCH, TimeComponents.H00, TimeScalesFactory.getTT());

  111.     /** Reference epoch for CCSDS Time Code Format (CCSDS 301.0-B-4):
  112.      * 1958-01-01T00:00:00 International Atomic Time (<em>not</em> UTC). */
  113.     public static final AbsoluteDate CCSDS_EPOCH =
  114.         new AbsoluteDate(DateComponents.CCSDS_EPOCH, TimeComponents.H00, TimeScalesFactory.getTAI());

  115.     /** Reference epoch for Galileo System Time: 1999-08-22T00:00:00 UTC. */
  116.     public static final AbsoluteDate GALILEO_EPOCH =
  117.         new AbsoluteDate(DateComponents.GALILEO_EPOCH, new TimeComponents(0, 0, 32),
  118.                          TimeScalesFactory.getTAI());

  119.     /** Reference epoch for GPS weeks: 1980-01-06T00:00:00 GPS time. */
  120.     public static final AbsoluteDate GPS_EPOCH =
  121.         new AbsoluteDate(DateComponents.GPS_EPOCH, TimeComponents.H00, TimeScalesFactory.getGPS());

  122.     /** J2000.0 Reference epoch: 2000-01-01T12:00:00 Terrestrial Time (<em>not</em> UTC).
  123.      * @see #createJulianEpoch(double)
  124.      * @see #createBesselianEpoch(double)
  125.      */
  126.     public static final AbsoluteDate J2000_EPOCH =
  127.         new AbsoluteDate(DateComponents.J2000_EPOCH, TimeComponents.H12, TimeScalesFactory.getTT());

  128.     /** Java Reference epoch: 1970-01-01T00:00:00 Universal Time Coordinate.
  129.      * <p>
  130.      * Between 1968-02-01 and 1972-01-01, UTC-TAI = 4.213 170 0s + (MJD - 39 126) x 0.002 592s.
  131.      * As on 1970-01-01 MJD = 40587, UTC-TAI = 8.000082s
  132.      * </p>
  133.      */
  134.     public static final AbsoluteDate JAVA_EPOCH =
  135.         new AbsoluteDate(DateComponents.JAVA_EPOCH, TimeScalesFactory.getTAI()).shiftedBy(8.000082);

  136.     /** Dummy date at infinity in the past direction. */
  137.     public static final AbsoluteDate PAST_INFINITY = JAVA_EPOCH.shiftedBy(Double.NEGATIVE_INFINITY);

  138.     /** Dummy date at infinity in the future direction. */
  139.     public static final AbsoluteDate FUTURE_INFINITY = JAVA_EPOCH.shiftedBy(Double.POSITIVE_INFINITY);

  140.     /** Serializable UID. */
  141.     private static final long serialVersionUID = 617061803741806846L;

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

  145.     /** Offset from the reference epoch in seconds. */
  146.     private final double offset;

  147.     /** Create an instance with a default value ({@link #J2000_EPOCH}).
  148.      */
  149.     public AbsoluteDate() {
  150.         epoch  = J2000_EPOCH.epoch;
  151.         offset = J2000_EPOCH.offset;
  152.     }

  153.     /** Build an instance from a location (parsed from a string) in a {@link TimeScale time scale}.
  154.      * <p>
  155.      * The supported formats for location are mainly the ones defined in ISO-8601 standard,
  156.      * the exact subset is explained in {@link DateTimeComponents#parseDateTime(String)},
  157.      * {@link DateComponents#parseDate(String)} and {@link TimeComponents#parseTime(String)}.
  158.      * </p>
  159.      * <p>
  160.      * As CCSDS ASCII calendar segmented time code is a trimmed down version of ISO-8601,
  161.      * it is also supported by this constructor.
  162.      * </p>
  163.      * @param location location in the time scale, must be in a supported format
  164.      * @param timeScale time scale
  165.      * @exception IllegalArgumentException if location string is not in a supported format
  166.      */
  167.     public AbsoluteDate(final String location, final TimeScale timeScale) {
  168.         this(DateTimeComponents.parseDateTime(location), timeScale);
  169.     }

  170.     /** Build an instance from a location in a {@link TimeScale time scale}.
  171.      * @param location location in the time scale
  172.      * @param timeScale time scale
  173.      */
  174.     public AbsoluteDate(final DateTimeComponents location, final TimeScale timeScale) {
  175.         this(location.getDate(), location.getTime(), timeScale);
  176.     }

  177.     /** Build an instance from a location in a {@link TimeScale time scale}.
  178.      * @param date date location in the time scale
  179.      * @param time time location in the time scale
  180.      * @param timeScale time scale
  181.      */
  182.     public AbsoluteDate(final DateComponents date, final TimeComponents time,
  183.                         final TimeScale timeScale) {

  184.         final double seconds  = time.getSecond();
  185.         final double tsOffset = timeScale.offsetToTAI(date, time);

  186.         // compute sum exactly, using Møller-Knuth TwoSum algorithm without branching
  187.         // the following statements must NOT be simplified, they rely on floating point
  188.         // arithmetic properties (rounding and representable numbers)
  189.         // at the end, the EXACT result of addition seconds + tsOffset
  190.         // is sum + residual, where sum is the closest representable number to the exact
  191.         // result and residual is the missing part that does not fit in the first number
  192.         final double sum      = seconds + tsOffset;
  193.         final double sPrime   = sum - tsOffset;
  194.         final double tPrime   = sum - sPrime;
  195.         final double deltaS   = seconds  - sPrime;
  196.         final double deltaT   = tsOffset - tPrime;
  197.         final double residual = deltaS   + deltaT;
  198.         final long   dl       = (long) FastMath.floor(sum);

  199.         offset = (sum - dl) + residual;
  200.         epoch  = 60l * ((date.getJ2000Day() * 24l + time.getHour()) * 60l +
  201.                         time.getMinute() - time.getMinutesFromUTC() - 720l) + dl;

  202.     }

  203.     /** Build an instance from a location in a {@link TimeScale time scale}.
  204.      * @param year year number (may be 0 or negative for BC years)
  205.      * @param month month number from 1 to 12
  206.      * @param day day number from 1 to 31
  207.      * @param hour hour number from 0 to 23
  208.      * @param minute minute number from 0 to 59
  209.      * @param second second number from 0.0 to 60.0 (excluded)
  210.      * @param timeScale time scale
  211.      * @exception IllegalArgumentException if inconsistent arguments
  212.      * are given (parameters out of range)
  213.      */
  214.     public AbsoluteDate(final int year, final int month, final int day,
  215.                         final int hour, final int minute, final double second,
  216.                         final TimeScale timeScale) throws IllegalArgumentException {
  217.         this(new DateComponents(year, month, day), new TimeComponents(hour, minute, second), timeScale);
  218.     }

  219.     /** Build an instance from a location in a {@link TimeScale time scale}.
  220.      * @param year year number (may be 0 or negative for BC years)
  221.      * @param month month enumerate
  222.      * @param day day number from 1 to 31
  223.      * @param hour hour number from 0 to 23
  224.      * @param minute minute number from 0 to 59
  225.      * @param second second number from 0.0 to 60.0 (excluded)
  226.      * @param timeScale time scale
  227.      * @exception IllegalArgumentException if inconsistent arguments
  228.      * are given (parameters out of range)
  229.      */
  230.     public AbsoluteDate(final int year, final Month month, final int day,
  231.                         final int hour, final int minute, final double second,
  232.                         final TimeScale timeScale) throws IllegalArgumentException {
  233.         this(new DateComponents(year, month, day), new TimeComponents(hour, minute, second), timeScale);
  234.     }

  235.     /** Build an instance from a location in a {@link TimeScale time scale}.
  236.      * <p>The hour is set to 00:00:00.000.</p>
  237.      * @param date date location in the time scale
  238.      * @param timeScale time scale
  239.      * @exception IllegalArgumentException if inconsistent arguments
  240.      * are given (parameters out of range)
  241.      */
  242.     public AbsoluteDate(final DateComponents date, final TimeScale timeScale)
  243.         throws IllegalArgumentException {
  244.         this(date, TimeComponents.H00, timeScale);
  245.     }

  246.     /** Build an instance from a location in a {@link TimeScale time scale}.
  247.      * <p>The hour is set to 00:00:00.000.</p>
  248.      * @param year year number (may be 0 or negative for BC years)
  249.      * @param month month number from 1 to 12
  250.      * @param day day number from 1 to 31
  251.      * @param timeScale time scale
  252.      * @exception IllegalArgumentException if inconsistent arguments
  253.      * are given (parameters out of range)
  254.      */
  255.     public AbsoluteDate(final int year, final int month, final int day,
  256.                         final TimeScale timeScale) throws IllegalArgumentException {
  257.         this(new DateComponents(year, month, day), TimeComponents.H00, timeScale);
  258.     }

  259.     /** Build an instance from a location in a {@link TimeScale time scale}.
  260.      * <p>The hour is set to 00:00:00.000.</p>
  261.      * @param year year number (may be 0 or negative for BC years)
  262.      * @param month month enumerate
  263.      * @param day day number from 1 to 31
  264.      * @param timeScale time scale
  265.      * @exception IllegalArgumentException if inconsistent arguments
  266.      * are given (parameters out of range)
  267.      */
  268.     public AbsoluteDate(final int year, final Month month, final int day,
  269.                         final TimeScale timeScale) throws IllegalArgumentException {
  270.         this(new DateComponents(year, month, day), TimeComponents.H00, 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 Date location, final TimeScale timeScale) {
  277.         this(new DateComponents(DateComponents.JAVA_EPOCH,
  278.                                 (int) (location.getTime() / 86400000l)),
  279.                                 new TimeComponents(0.001 * (location.getTime() % 86400000l)),
  280.              timeScale);
  281.     }

  282.     /** Build an instance from an elapsed duration since to another instant.
  283.      * <p>It is important to note that the elapsed duration is <em>not</em>
  284.      * the difference between two readings on a time scale. As an example,
  285.      * the duration between the two instants leading to the readings
  286.      * 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the {@link UTCScale UTC}
  287.      * time scale is <em>not</em> 1 second, but a stop watch would have measured
  288.      * an elapsed duration of 2 seconds between these two instances because a leap
  289.      * second was introduced at the end of 2005 in this time scale.</p>
  290.      * <p>This constructor is the reverse of the {@link #durationFrom(AbsoluteDate)}
  291.      * method.</p>
  292.      * @param since start instant of the measured duration
  293.      * @param elapsedDuration physically elapsed duration from the <code>since</code>
  294.      * instant, as measured in a regular time scale
  295.      * @see #durationFrom(AbsoluteDate)
  296.      */
  297.     public AbsoluteDate(final AbsoluteDate since, final double elapsedDuration) {

  298.         final double sum = since.offset + elapsedDuration;
  299.         if (Double.isInfinite(sum)) {
  300.             offset = sum;
  301.             epoch  = (sum < 0) ? Long.MIN_VALUE : Long.MAX_VALUE;
  302.         } else {
  303.             // compute sum exactly, using Møller-Knuth TwoSum algorithm without branching
  304.             // the following statements must NOT be simplified, they rely on floating point
  305.             // arithmetic properties (rounding and representable numbers)
  306.             // at the end, the EXACT result of addition since.offset + elapsedDuration
  307.             // is sum + residual, where sum is the closest representable number to the exact
  308.             // result and residual is the missing part that does not fit in the first number
  309.             final double oPrime   = sum - elapsedDuration;
  310.             final double dPrime   = sum - oPrime;
  311.             final double deltaO   = since.offset - oPrime;
  312.             final double deltaD   = elapsedDuration - dPrime;
  313.             final double residual = deltaO + deltaD;
  314.             final long   dl       = (long) FastMath.floor(sum);
  315.             offset = (sum - dl) + residual;
  316.             epoch  = since.epoch  + dl;
  317.         }
  318.     }

  319.     /** Build an instance from an apparent clock offset with respect to another
  320.      * instant <em>in the perspective of a specific {@link TimeScale time scale}</em>.
  321.      * <p>It is important to note that the apparent clock offset <em>is</em> the
  322.      * difference between two readings on a time scale and <em>not</em> an elapsed
  323.      * duration. As an example, the apparent clock offset between the two instants
  324.      * leading to the readings 2005-12-31T23:59:59 and 2006-01-01T00:00:00 in the
  325.      * {@link UTCScale UTC} time scale is 1 second, but the elapsed duration is 2
  326.      * seconds because a leap second has been introduced at the end of 2005 in this
  327.      * time scale.</p>
  328.      * <p>This constructor is the reverse of the {@link #offsetFrom(AbsoluteDate,
  329.      * TimeScale)} method.</p>
  330.      * @param reference reference instant
  331.      * @param apparentOffset apparent clock offset from the reference instant
  332.      * (difference between two readings in the specified time scale)
  333.      * @param timeScale time scale with respect to which the offset is defined
  334.      * @see #offsetFrom(AbsoluteDate, TimeScale)
  335.      */
  336.     public AbsoluteDate(final AbsoluteDate reference, final double apparentOffset,
  337.                         final TimeScale timeScale) {
  338.         this(new DateTimeComponents(reference.getComponents(timeScale), apparentOffset),
  339.              timeScale);
  340.     }

  341.     /** Build a date from its internal components.
  342.      * <p>
  343.      * This method is reserved for internal used (for example by {@link FieldAbsoluteDate}).
  344.      * </p>
  345.      * @param epoch reference epoch in seconds from 2000-01-01T12:00:00 TAI.
  346.      * (beware, it is not {@link #J2000_EPOCH} since it is in TAI and not in TT)
  347.      * @param offset offset from the reference epoch in seconds (must be
  348.      * between 0.0 included and 1.0 excluded)
  349.      * @since 9.0
  350.      */
  351.     AbsoluteDate(final long epoch, final double offset) {
  352.         this.epoch  = epoch;
  353.         this.offset = offset;
  354.     }

  355.     /** Get the reference epoch in seconds from 2000-01-01T12:00:00 TAI.
  356.      * <p>
  357.      * This method is reserved for internal used (for example by {@link FieldAbsoluteDate}).
  358.      * </p>
  359.      * <p>
  360.      * Beware, it is not {@link #J2000_EPOCH} since it is in TAI and not in TT.
  361.      * </p>
  362.      * @return reference epoch in seconds from 2000-01-01T12:00:00 TAI
  363.      * @since 9.0
  364.      */
  365.     long getEpoch() {
  366.         return epoch;
  367.     }

  368.     /** Get the offset from the reference epoch in seconds.
  369.      * <p>
  370.      * This method is reserved for internal used (for example by {@link FieldAbsoluteDate}).
  371.      * </p>
  372.      * @return offset from the reference epoch in seconds
  373.      * @since 9.0
  374.      */
  375.     double getOffset() {
  376.         return offset;
  377.     }

  378.     /** Build an instance from a CCSDS Unsegmented Time Code (CUC).
  379.      * <p>
  380.      * CCSDS Unsegmented Time Code is defined in the blue book:
  381.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  382.      * </p>
  383.      * <p>
  384.      * If the date to be parsed is formatted using version 3 of the standard
  385.      * (CCSDS 301.0-B-3 published in 2002) or if the extension of the preamble
  386.      * field introduced in version 4 of the standard is not used, then the
  387.      * {@code preambleField2} parameter can be set to 0.
  388.      * </p>
  389.      * @param preambleField1 first byte of the field specifying the format, often
  390.      * not transmitted in data interfaces, as it is constant for a given data interface
  391.      * @param preambleField2 second byte of the field specifying the format
  392.      * (added in revision 4 of the CCSDS standard in 2010), often not transmitted in data
  393.      * interfaces, as it is constant for a given data interface (value ignored if presence
  394.      * not signaled in {@code preambleField1})
  395.      * @param timeField byte array containing the time code
  396.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field
  397.      * specifies the {@link #CCSDS_EPOCH CCSDS reference epoch} is used (and hence
  398.      * may be null in this case)
  399.      * @return an instance corresponding to the specified date
  400.      */
  401.     public static AbsoluteDate parseCCSDSUnsegmentedTimeCode(final byte preambleField1,
  402.                                                              final byte preambleField2,
  403.                                                              final byte[] timeField,
  404.                                                              final AbsoluteDate agencyDefinedEpoch) {

  405.         // time code identification and reference epoch
  406.         final AbsoluteDate epoch;
  407.         switch (preambleField1 & 0x70) {
  408.             case 0x10:
  409.                 // the reference epoch is CCSDS epoch 1958-01-01T00:00:00 TAI
  410.                 epoch = CCSDS_EPOCH;
  411.                 break;
  412.             case 0x20:
  413.                 // the reference epoch is agency defined
  414.                 if (agencyDefinedEpoch == null) {
  415.                     throw new OrekitException(OrekitMessages.CCSDS_DATE_MISSING_AGENCY_EPOCH);
  416.                 }
  417.                 epoch = agencyDefinedEpoch;
  418.                 break;
  419.             default :
  420.                 throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
  421.                                           formatByte(preambleField1));
  422.         }

  423.         // time field lengths
  424.         int coarseTimeLength = 1 + ((preambleField1 & 0x0C) >>> 2);
  425.         int fineTimeLength   = preambleField1 & 0x03;

  426.         if ((preambleField1 & 0x80) != 0x0) {
  427.             // there is an additional octet in preamble field
  428.             coarseTimeLength += (preambleField2 & 0x60) >>> 5;
  429.             fineTimeLength   += (preambleField2 & 0x1C) >>> 2;
  430.         }

  431.         if (timeField.length != coarseTimeLength + fineTimeLength) {
  432.             throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_LENGTH_TIME_FIELD,
  433.                                       timeField.length, coarseTimeLength + fineTimeLength);
  434.         }

  435.         double seconds = 0;
  436.         for (int i = 0; i < coarseTimeLength; ++i) {
  437.             seconds = seconds * 256 + toUnsigned(timeField[i]);
  438.         }
  439.         double subseconds = 0;
  440.         for (int i = timeField.length - 1; i >= coarseTimeLength; --i) {
  441.             subseconds = (subseconds + toUnsigned(timeField[i])) / 256;
  442.         }

  443.         return new AbsoluteDate(epoch, seconds).shiftedBy(subseconds);

  444.     }

  445.     /** Build an instance from a CCSDS Day Segmented Time Code (CDS).
  446.      * <p>
  447.      * CCSDS Day Segmented Time Code is defined in the blue book:
  448.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  449.      * </p>
  450.      * @param preambleField field specifying the format, often not transmitted in
  451.      * data interfaces, as it is constant for a given data interface
  452.      * @param timeField byte array containing the time code
  453.      * @param agencyDefinedEpoch reference epoch, ignored if the preamble field
  454.      * specifies the {@link #CCSDS_EPOCH CCSDS reference epoch} is used (and hence
  455.      * may be null in this case)
  456.      * @return an instance corresponding to the specified date
  457.      */
  458.     public static AbsoluteDate parseCCSDSDaySegmentedTimeCode(final byte preambleField, final byte[] timeField,
  459.                                                               final DateComponents agencyDefinedEpoch) {

  460.         // time code identification
  461.         if ((preambleField & 0xF0) != 0x40) {
  462.             throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
  463.                                       formatByte(preambleField));
  464.         }

  465.         // reference epoch
  466.         final DateComponents epoch;
  467.         if ((preambleField & 0x08) == 0x00) {
  468.             // the reference epoch is CCSDS epoch 1958-01-01T00:00:00 TAI
  469.             epoch = DateComponents.CCSDS_EPOCH;
  470.         } else {
  471.             // the reference epoch is agency defined
  472.             if (agencyDefinedEpoch == null) {
  473.                 throw new OrekitException(OrekitMessages.CCSDS_DATE_MISSING_AGENCY_EPOCH);
  474.             }
  475.             epoch = agencyDefinedEpoch;
  476.         }

  477.         // time field lengths
  478.         final int daySegmentLength = ((preambleField & 0x04) == 0x0) ? 2 : 3;
  479.         final int subMillisecondLength = (preambleField & 0x03) << 1;
  480.         if (subMillisecondLength == 6) {
  481.             throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
  482.                                       formatByte(preambleField));
  483.         }
  484.         if (timeField.length != daySegmentLength + 4 + subMillisecondLength) {
  485.             throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_LENGTH_TIME_FIELD,
  486.                                       timeField.length, daySegmentLength + 4 + subMillisecondLength);
  487.         }


  488.         int i   = 0;
  489.         int day = 0;
  490.         while (i < daySegmentLength) {
  491.             day = day * 256 + toUnsigned(timeField[i++]);
  492.         }

  493.         long milliInDay = 0l;
  494.         while (i < daySegmentLength + 4) {
  495.             milliInDay = milliInDay * 256 + toUnsigned(timeField[i++]);
  496.         }
  497.         final int milli   = (int) (milliInDay % 1000l);
  498.         final int seconds = (int) ((milliInDay - milli) / 1000l);

  499.         double subMilli = 0;
  500.         double divisor  = 1;
  501.         while (i < timeField.length) {
  502.             subMilli = subMilli * 256 + toUnsigned(timeField[i++]);
  503.             divisor *= 1000;
  504.         }

  505.         final DateComponents date = new DateComponents(epoch, day);
  506.         final TimeComponents time = new TimeComponents(seconds);
  507.         return new AbsoluteDate(date, time, TimeScalesFactory.getUTC()).shiftedBy(milli * 1.0e-3 + subMilli / divisor);

  508.     }

  509.     /** Build an instance from a CCSDS Calendar Segmented Time Code (CCS).
  510.      * <p>
  511.      * CCSDS Calendar Segmented Time Code is defined in the blue book:
  512.      * CCSDS Time Code Format (CCSDS 301.0-B-4) published in November 2010
  513.      * </p>
  514.      * @param preambleField field specifying the format, often not transmitted in
  515.      * data interfaces, as it is constant for a given data interface
  516.      * @param timeField byte array containing the time code
  517.      * @return an instance corresponding to the specified date
  518.      */
  519.     public static AbsoluteDate parseCCSDSCalendarSegmentedTimeCode(final byte preambleField, final byte[] timeField) {

  520.         // time code identification
  521.         if ((preambleField & 0xF0) != 0x50) {
  522.             throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
  523.                                       formatByte(preambleField));
  524.         }

  525.         // time field length
  526.         final int length = 7 + (preambleField & 0x07);
  527.         if (length == 14) {
  528.             throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_PREAMBLE_FIELD,
  529.                                       formatByte(preambleField));
  530.         }
  531.         if (timeField.length != length) {
  532.             throw new OrekitException(OrekitMessages.CCSDS_DATE_INVALID_LENGTH_TIME_FIELD,
  533.                                       timeField.length, length);
  534.         }

  535.         // date part in the first four bytes
  536.         final DateComponents date;
  537.         if ((preambleField & 0x08) == 0x00) {
  538.             // month of year and day of month variation
  539.             date = new DateComponents(toUnsigned(timeField[0]) * 256 + toUnsigned(timeField[1]),
  540.                                       toUnsigned(timeField[2]),
  541.                                       toUnsigned(timeField[3]));
  542.         } else {
  543.             // day of year variation
  544.             date = new DateComponents(toUnsigned(timeField[0]) * 256 + toUnsigned(timeField[1]),
  545.                                       toUnsigned(timeField[2]) * 256 + toUnsigned(timeField[3]));
  546.         }

  547.         // time part from bytes 5 to last (between 7 and 13 depending on precision)
  548.         final TimeComponents time = new TimeComponents(toUnsigned(timeField[4]),
  549.                                                        toUnsigned(timeField[5]),
  550.                                                        toUnsigned(timeField[6]));
  551.         double subSecond = 0;
  552.         double divisor   = 1;
  553.         for (int i = 7; i < length; ++i) {
  554.             subSecond = subSecond * 100 + toUnsigned(timeField[i]);
  555.             divisor *= 100;
  556.         }

  557.         return new AbsoluteDate(date, time, TimeScalesFactory.getUTC()).shiftedBy(subSecond / divisor);

  558.     }

  559.     /** Decode a signed byte as an unsigned int value.
  560.      * @param b byte to decode
  561.      * @return an unsigned int value
  562.      */
  563.     private static int toUnsigned(final byte b) {
  564.         final int i = (int) b;
  565.         return (i < 0) ? 256 + i : i;
  566.     }

  567.     /** Format a byte as an hex string for error messages.
  568.      * @param data byte to format
  569.      * @return a formatted string
  570.      */
  571.     private static String formatByte(final byte data) {
  572.         return "0x" + Integer.toHexString(data).toUpperCase();
  573.     }

  574.     /** Build an instance corresponding to a Julian Day date.
  575.      * @param jd Julian day
  576.      * @param secondsSinceNoon seconds in the Julian day
  577.      * (BEWARE, Julian days start at noon, so 0.0 is noon)
  578.      * @param timeScale time scale in which the seconds in day are defined
  579.      * @return a new instant
  580.      */
  581.     public static AbsoluteDate createJDDate(final int jd, final double secondsSinceNoon,
  582.                                              final TimeScale timeScale) {
  583.         return new AbsoluteDate(new DateComponents(DateComponents.JULIAN_EPOCH, jd),
  584.                                 TimeComponents.H12, timeScale).shiftedBy(secondsSinceNoon);
  585.     }

  586.     /** Build an instance corresponding to a Modified Julian Day date.
  587.      * @param mjd modified Julian day
  588.      * @param secondsInDay seconds in the day
  589.      * @param timeScale time scale in which the seconds in day are defined
  590.      * @return a new instant
  591.      * @exception OrekitIllegalArgumentException if seconds number is out of range
  592.      */
  593.     public static AbsoluteDate createMJDDate(final int mjd, final double secondsInDay,
  594.                                              final TimeScale timeScale)
  595.         throws OrekitIllegalArgumentException {
  596.         final DateComponents dc = new DateComponents(DateComponents.MODIFIED_JULIAN_EPOCH, mjd);
  597.         final TimeComponents tc;
  598.         if (secondsInDay >= Constants.JULIAN_DAY) {
  599.             // check we are really allowed to use this number of seconds
  600.             final int    secondsA = 86399; // 23:59:59, i.e. 59s in the last minute of the day
  601.             final double secondsB = secondsInDay - secondsA;
  602.             final TimeComponents safeTC = new TimeComponents(secondsA, 0.0);
  603.             final AbsoluteDate safeDate = new AbsoluteDate(dc, safeTC, timeScale);
  604.             if (timeScale.minuteDuration(safeDate) > 59 + secondsB) {
  605.                 // we are within the last minute of the day, the number of seconds is OK
  606.                 return safeDate.shiftedBy(secondsB);
  607.             } else {
  608.                 // let TimeComponents trigger an OrekitIllegalArgumentException
  609.                 // for the wrong number of seconds
  610.                 tc = new TimeComponents(secondsA, secondsB);
  611.             }
  612.         } else {
  613.             tc = new TimeComponents(secondsInDay);
  614.         }

  615.         // create the date
  616.         return new AbsoluteDate(dc, tc, timeScale);

  617.     }


  618.     /** Build an instance corresponding to a GPS date.
  619.      * <p>GPS dates are provided as a week number starting at
  620.      * {@link #GPS_EPOCH GPS epoch} and as a number of milliseconds
  621.      * since week start.</p>
  622.      * @param weekNumber week number since {@link #GPS_EPOCH GPS epoch}
  623.      * @param milliInWeek number of milliseconds since week start
  624.      * @return a new instant
  625.      * @deprecated as of 9.3, replaced by {@link GPSDate#GPSDate(int, double)}.{@link GPSDate#getDate()}
  626.      */
  627.     @Deprecated
  628.     public static AbsoluteDate createGPSDate(final int weekNumber, final double milliInWeek) {
  629.         return new GPSDate(weekNumber, milliInWeek).getDate();
  630.     }

  631.     /** Build an instance corresponding to a Julian Epoch (JE).
  632.      * <p>According to Lieske paper: <a
  633.      * href="http://articles.adsabs.harvard.edu/cgi-bin/nph-iarticle_query?1979A%26A....73..282L&amp;defaultprint=YES&amp;filetype=.pdf.">
  634.      * Precession Matrix Based on IAU (1976) System of Astronomical Constants</a>, Astronomy and Astrophysics,
  635.      * vol. 73, no. 3, Mar. 1979, p. 282-284, Julian Epoch is related to Julian Ephemeris Date as:</p>
  636.      * <pre>
  637.      * JE = 2000.0 + (JED - 2451545.0) / 365.25
  638.      * </pre>
  639.      * <p>
  640.      * This method reverts the formula above and computes an {@code AbsoluteDate} from the Julian Epoch.
  641.      * </p>
  642.      * @param julianEpoch Julian epoch, like 2000.0 for defining the classical reference J2000.0
  643.      * @return a new instant
  644.      * @see #J2000_EPOCH
  645.      * @see #createBesselianEpoch(double)
  646.      */
  647.     public static AbsoluteDate createJulianEpoch(final double julianEpoch) {
  648.         return new AbsoluteDate(J2000_EPOCH,
  649.                                 Constants.JULIAN_YEAR * (julianEpoch - 2000.0));
  650.     }

  651.     /** Build an instance corresponding to a Besselian Epoch (BE).
  652.      * <p>According to Lieske paper: <a
  653.      * href="http://articles.adsabs.harvard.edu/cgi-bin/nph-iarticle_query?1979A%26A....73..282L&amp;defaultprint=YES&amp;filetype=.pdf.">
  654.      * Precession Matrix Based on IAU (1976) System of Astronomical Constants</a>, Astronomy and Astrophysics,
  655.      * vol. 73, no. 3, Mar. 1979, p. 282-284, Besselian Epoch is related to Julian Ephemeris Date as:</p>
  656.      * <pre>
  657.      * BE = 1900.0 + (JED - 2415020.31352) / 365.242198781
  658.      * </pre>
  659.      * <p>
  660.      * This method reverts the formula above and computes an {@code AbsoluteDate} from the Besselian Epoch.
  661.      * </p>
  662.      * @param besselianEpoch Besselian epoch, like 1950 for defining the classical reference B1950.0
  663.      * @return a new instant
  664.      * @see #createJulianEpoch(double)
  665.      */
  666.     public static AbsoluteDate createBesselianEpoch(final double besselianEpoch) {
  667.         return new AbsoluteDate(J2000_EPOCH,
  668.                                 MathArrays.linearCombination(Constants.BESSELIAN_YEAR, besselianEpoch - 1900,
  669.                                                              Constants.JULIAN_DAY, -36525,
  670.                                                              Constants.JULIAN_DAY, 0.31352));
  671.     }

  672.     /** Get a time-shifted date.
  673.      * <p>
  674.      * Calling this method is equivalent to call <code>new AbsoluteDate(this, dt)</code>.
  675.      * </p>
  676.      * @param dt time shift in seconds
  677.      * @return a new date, shifted with respect to instance (which is immutable)
  678.      * @see org.orekit.utils.PVCoordinates#shiftedBy(double)
  679.      * @see org.orekit.attitudes.Attitude#shiftedBy(double)
  680.      * @see org.orekit.orbits.Orbit#shiftedBy(double)
  681.      * @see org.orekit.propagation.SpacecraftState#shiftedBy(double)
  682.      */
  683.     public AbsoluteDate shiftedBy(final double dt) {
  684.         return new AbsoluteDate(this, dt);
  685.     }

  686.     /** Compute the physically elapsed duration between two instants.
  687.      * <p>The returned duration is the number of seconds physically
  688.      * elapsed between the two instants, measured in a regular time
  689.      * scale with respect to surface of the Earth (i.e either the {@link
  690.      * TAIScale TAI scale}, the {@link TTScale TT scale} or the {@link
  691.      * GPSScale GPS scale}). It is the only method that gives a
  692.      * duration with a physical meaning.</p>
  693.      * <p>This method gives the same result (with less computation)
  694.      * as calling {@link #offsetFrom(AbsoluteDate, TimeScale)}
  695.      * with a second argument set to one of the regular scales cited
  696.      * above.</p>
  697.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  698.      * double)} constructor.</p>
  699.      * @param instant instant to subtract from the instance
  700.      * @return offset in seconds between the two instants (positive
  701.      * if the instance is posterior to the argument)
  702.      * @see #offsetFrom(AbsoluteDate, TimeScale)
  703.      * @see #AbsoluteDate(AbsoluteDate, double)
  704.      */
  705.     public double durationFrom(final AbsoluteDate instant) {
  706.         return (epoch - instant.epoch) + (offset - instant.offset);
  707.     }

  708.     /** Compute the apparent clock offset between two instant <em>in the
  709.      * perspective of a specific {@link TimeScale time scale}</em>.
  710.      * <p>The offset is the number of seconds counted in the given
  711.      * time scale between the locations of the two instants, with
  712.      * all time scale irregularities removed (i.e. considering all
  713.      * days are exactly 86400 seconds long). This method will give
  714.      * a result that may not have a physical meaning if the time scale
  715.      * is irregular. For example since a leap second was introduced at
  716.      * the end of 2005, the apparent offset between 2005-12-31T23:59:59
  717.      * and 2006-01-01T00:00:00 is 1 second, but the physical duration
  718.      * of the corresponding time interval as returned by the {@link
  719.      * #durationFrom(AbsoluteDate)} method is 2 seconds.</p>
  720.      * <p>This method is the reverse of the {@link #AbsoluteDate(AbsoluteDate,
  721.      * double, TimeScale)} constructor.</p>
  722.      * @param instant instant to subtract from the instance
  723.      * @param timeScale time scale with respect to which the offset should
  724.      * be computed
  725.      * @return apparent clock offset in seconds between the two instants
  726.      * (positive if the instance is posterior to the argument)
  727.      * @see #durationFrom(AbsoluteDate)
  728.      * @see #AbsoluteDate(AbsoluteDate, double, TimeScale)
  729.      */
  730.     public double offsetFrom(final AbsoluteDate instant, final TimeScale timeScale) {
  731.         final long   elapsedDurationA = epoch - instant.epoch;
  732.         final double elapsedDurationB = (offset         + timeScale.offsetFromTAI(this)) -
  733.                                         (instant.offset + timeScale.offsetFromTAI(instant));
  734.         return  elapsedDurationA + elapsedDurationB;
  735.     }

  736.     /** Compute the offset between two time scales at the current instant.
  737.      * <p>The offset is defined as <i>l₁-l₂</i>
  738.      * where <i>l₁</i> is the location of the instant in
  739.      * the <code>scale1</code> time scale and <i>l₂</i> is the
  740.      * location of the instant in the <code>scale2</code> time scale.</p>
  741.      * @param scale1 first time scale
  742.      * @param scale2 second time scale
  743.      * @return offset in seconds between the two time scales at the
  744.      * current instant
  745.      */
  746.     public double timeScalesOffset(final TimeScale scale1, final TimeScale scale2) {
  747.         return scale1.offsetFromTAI(this) - scale2.offsetFromTAI(this);
  748.     }

  749.     /** Convert the instance to a Java {@link java.util.Date Date}.
  750.      * <p>Conversion to the Date class induces a loss of precision because
  751.      * the Date class does not provide sub-millisecond information. Java Dates
  752.      * are considered to be locations in some times scales.</p>
  753.      * @param timeScale time scale to use
  754.      * @return a {@link java.util.Date Date} instance representing the location
  755.      * of the instant in the time scale
  756.      */
  757.     public Date toDate(final TimeScale timeScale) {
  758.         final double time = epoch + (offset + timeScale.offsetFromTAI(this));
  759.         return new Date(FastMath.round((time + 10957.5 * 86400.0) * 1000));
  760.     }

  761.     /** Split the instance into date/time components.
  762.      * @param timeScale time scale to use
  763.      * @return date/time components
  764.      */
  765.     public DateTimeComponents getComponents(final TimeScale timeScale) {

  766.         if (Double.isInfinite(offset)) {
  767.             // special handling for past and future infinity
  768.             if (offset < 0) {
  769.                 return new DateTimeComponents(DateComponents.MIN_EPOCH, TimeComponents.H00);
  770.             } else {
  771.                 return new DateTimeComponents(DateComponents.MAX_EPOCH,
  772.                                               new TimeComponents(23, 59, 59.999));
  773.             }
  774.         }

  775.         // compute offset from 2000-01-01T00:00:00 in specified time scale exactly,
  776.         // using Møller-Knuth TwoSum algorithm without branching
  777.         // the following statements must NOT be simplified, they rely on floating point
  778.         // arithmetic properties (rounding and representable numbers)
  779.         // at the end, the EXACT result of addition offset + timeScale.offsetFromTAI(this)
  780.         // is sum + residual, where sum is the closest representable number to the exact
  781.         // result and residual is the missing part that does not fit in the first number
  782.         final double taiOffset = timeScale.offsetFromTAI(this);
  783.         final double sum       = offset + taiOffset;
  784.         final double oPrime    = sum - taiOffset;
  785.         final double dPrime    = sum - oPrime;
  786.         final double deltaO    = offset - oPrime;
  787.         final double deltaD    = taiOffset - dPrime;
  788.         final double residual  = deltaO + deltaD;

  789.         // split date and time
  790.         final long   carry = (long) FastMath.floor(sum);
  791.         double offset2000B = (sum - carry) + residual;
  792.         long   offset2000A = epoch + carry + 43200l;
  793.         if (offset2000B < 0) {
  794.             offset2000A -= 1;
  795.             offset2000B += 1;
  796.         }
  797.         long time = offset2000A % 86400l;
  798.         if (time < 0l) {
  799.             time += 86400l;
  800.         }
  801.         final int date = (int) ((offset2000A - time) / 86400l);

  802.         // extract calendar elements
  803.         final DateComponents dateComponents = new DateComponents(DateComponents.J2000_EPOCH, date);
  804.         TimeComponents timeComponents = new TimeComponents((int) time, offset2000B);

  805.         if (timeScale.insideLeap(this)) {
  806.             // fix the seconds number to take the leap into account
  807.             timeComponents = new TimeComponents(timeComponents.getHour(), timeComponents.getMinute(),
  808.                                                 timeComponents.getSecond() + timeScale.getLeap(this));
  809.         }

  810.         // build the components
  811.         return new DateTimeComponents(dateComponents, timeComponents);

  812.     }

  813.     /** Split the instance into date/time components for a local time.
  814.      * @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
  815.      * negative Westward UTC)
  816.      * @return date/time components
  817.           * @since 7.2
  818.      */
  819.     public DateTimeComponents getComponents(final int minutesFromUTC) {

  820.         final DateTimeComponents utcComponents = getComponents(TimeScalesFactory.getUTC());

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

  825.         int minute = utcComponents.getTime().getMinute() + minutesFromUTC;
  826.         final int hourShift;
  827.         if (minute < 0) {
  828.             hourShift = (minute - 59) / 60;
  829.         } else if (minute > 59) {
  830.             hourShift = minute / 60;
  831.         } else {
  832.             hourShift = 0;
  833.         }
  834.         minute -= 60 * hourShift;

  835.         int hour = utcComponents.getTime().getHour() + hourShift;
  836.         final int dayShift;
  837.         if (hour < 0) {
  838.             dayShift = (hour - 23) / 24;
  839.         } else if (hour > 23) {
  840.             dayShift = hour / 24;
  841.         } else {
  842.             dayShift = 0;
  843.         }
  844.         hour -= 24 * dayShift;

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

  847.     }

  848.     /** Split the instance into date/time components for a time zone.
  849.      * @param timeZone time zone
  850.      * @return date/time components
  851.           * @since 7.2
  852.      */
  853.     public DateTimeComponents getComponents(final TimeZone timeZone) {
  854.         final long milliseconds = FastMath.round(1000 * offsetFrom(JAVA_EPOCH, TimeScalesFactory.getUTC()));
  855.         return getComponents(timeZone.getOffset(milliseconds) / 60000);
  856.     }

  857.     /** Compare the instance with another date.
  858.      * @param date other date to compare the instance to
  859.      * @return a negative integer, zero, or a positive integer as this date
  860.      * is before, simultaneous, or after the specified date.
  861.      */
  862.     public int compareTo(final AbsoluteDate date) {
  863.         return Double.compare(durationFrom(date),  0);
  864.     }

  865.     /** {@inheritDoc} */
  866.     public AbsoluteDate getDate() {
  867.         return this;
  868.     }

  869.     /** Check if the instance represent the same time as another instance.
  870.      * @param date other date
  871.      * @return true if the instance and the other date refer to the same instant
  872.      */
  873.     public boolean equals(final Object date) {

  874.         if (date == this) {
  875.             // first fast check
  876.             return true;
  877.         }

  878.         if ((date != null) && (date instanceof AbsoluteDate)) {
  879.             return durationFrom((AbsoluteDate) date) == 0;
  880.         }

  881.         return false;

  882.     }

  883.     /** Get a hashcode for this date.
  884.      * @return hashcode
  885.      */
  886.     public int hashCode() {
  887.         final long l = Double.doubleToLongBits(durationFrom(J2000_EPOCH));
  888.         return (int) (l ^ (l >>> 32));
  889.     }

  890.     /** Get a String representation of the instant location in UTC time scale.
  891.      * @return a string representation of the instance,
  892.      * in ISO-8601 format with milliseconds accuracy
  893.      */
  894.     public String toString() {
  895.         return toString(TimeScalesFactory.getUTC());
  896.     }

  897.     /** Get a String representation of the instant location.
  898.      * @param timeScale time scale to use
  899.      * @return a string representation of the instance,
  900.      * in ISO-8601 format with milliseconds accuracy
  901.      */
  902.     public String toString(final TimeScale timeScale) {
  903.         return getComponents(timeScale).toString(timeScale.minuteDuration(this));
  904.     }

  905.     /** Get a String representation of the instant location for a local time.
  906.      * @param minutesFromUTC offset in <em>minutes</em> from UTC (positive Eastwards UTC,
  907.      * negative Westward UTC).
  908.      * @return string representation of the instance,
  909.      * in ISO-8601 format with milliseconds accuracy
  910.           * @since 7.2
  911.      */
  912.     public String toString(final int minutesFromUTC) {
  913.         final int minuteDuration = TimeScalesFactory.getUTC().minuteDuration(this);
  914.         return getComponents(minutesFromUTC).toString(minuteDuration);
  915.     }

  916.     /** Get a String representation of the instant location for a time zone.
  917.      * @param timeZone time zone
  918.      * @return string representation of the instance,
  919.      * in ISO-8601 format with milliseconds accuracy
  920.           * @since 7.2
  921.      */
  922.     public String toString(final TimeZone timeZone) {
  923.         final int minuteDuration = TimeScalesFactory.getUTC().minuteDuration(this);
  924.         return getComponents(timeZone).toString(minuteDuration);
  925.     }

  926. }