TimeScale.java

  1. /* Copyright 2002-2018 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 org.hipparchus.RealFieldElement;

  20. /** Interface for time scales.
  21.  * <p>This is the interface representing all time scales. Time scales are related
  22.  * to each other by some offsets that may be discontinuous (for example
  23.  * the {@link UTCScale UTC scale} with respect to the {@link TAIScale
  24.  * TAI scale}).</p>
  25.  * @author Luc Maisonobe
  26.  * @see AbsoluteDate
  27.  */
  28. public interface TimeScale extends Serializable {

  29.     /** Get the offset to convert locations from {@link TAIScale} to instance.
  30.      * @param date conversion date
  31.      * @return offset in seconds to add to a location in <em>{@link TAIScale}
  32.      * time scale</em> to get a location in <em>instance time scale</em>
  33.      * @see #offsetToTAI(DateComponents, TimeComponents)
  34.      */
  35.     double offsetFromTAI(AbsoluteDate date);

  36.     /** Get the offset to convert locations from {@link TAIScale} to instance.
  37.      * @param date conversion date
  38.      * @param <T> type of the filed elements
  39.      * @return offset in seconds to add to a location in <em>{@link TAIScale}
  40.      * time scale</em> to get a location in <em>instance time scale</em>
  41.      * @see #offsetToTAI(DateComponents, TimeComponents)
  42.      * @since 9.0
  43.      */
  44.     <T extends RealFieldElement<T>> T offsetFromTAI(FieldAbsoluteDate<T> date);

  45.     /** Get the offset to convert locations from instance to {@link TAIScale}.
  46.      * @param date date location in the time scale
  47.      * @param time time location in the time scale
  48.      * @return offset in seconds to add to a location in <em>instance time scale</em>
  49.      * to get a location in <em>{@link TAIScale} time scale</em>
  50.      * @see #offsetFromTAI(AbsoluteDate)
  51.      */
  52.     default double offsetToTAI(final DateComponents date, final TimeComponents time) {
  53.         final AbsoluteDate reference = new AbsoluteDate(date, time, TimeScalesFactory.getTAI());
  54.         double offset = 0;
  55.         for (int i = 0; i < 8; i++) {
  56.             offset = -offsetFromTAI(reference.shiftedBy(offset));
  57.         }
  58.         return offset;
  59.     }

  60.     /** Check if date is within a leap second introduction <em>in this time scale</em>.
  61.      * <p>
  62.      * This method will return false for all time scales that do <em>not</em>
  63.      * implement leap seconds, even if the date corresponds to a leap second
  64.      * in {@link UTCScale UTC scale}.
  65.      * </p>
  66.      * @param date date to check
  67.      * @return true if time is within a leap second introduction
  68.      */
  69.     default boolean insideLeap(final AbsoluteDate date) {
  70.         return false;
  71.     }

  72.     /** Check if date is within a leap second introduction <em>in this time scale</em>.
  73.      * <p>
  74.      * This method will return false for all time scales that do <em>not</em>
  75.      * implement leap seconds, even if the date corresponds to a leap second
  76.      * in {@link UTCScale UTC scale}.
  77.      * </p>
  78.      * @param date date to check
  79.      * @param <T> type of the filed elements
  80.      * @return true if time is within a leap second introduction
  81.      * @since 9.0
  82.      */
  83.     default <T extends RealFieldElement<T>> boolean insideLeap(final FieldAbsoluteDate<T> date) {
  84.         return false;
  85.     }

  86.     /** Check length of the current minute <em>in this time scale</em>.
  87.      * <p>
  88.      * This method will return 60 for all time scales that do <em>not</em>
  89.      * implement leap seconds, even if the date corresponds to a leap second
  90.      * in {@link UTCScale UTC scale}, and 61 for time scales that do implement
  91.      * leap second when the current date is within the last minute before the
  92.      * leap, or during the leap itself.
  93.      * </p>
  94.      * @param date date to check
  95.      * @return 60 or 61 depending on leap seconds introduction
  96.      */
  97.     default int minuteDuration(final AbsoluteDate date) {
  98.         return 60;
  99.     }

  100.     /** Check length of the current minute <em>in this time scale</em>.
  101.      * <p>
  102.      * This method will return 60 for all time scales that do <em>not</em>
  103.      * implement leap seconds, even if the date corresponds to a leap second
  104.      * in {@link UTCScale UTC scale}, and 61 for time scales that do implement
  105.      * leap second when the current date is within the last minute before the
  106.      * leap, or during the leap itself.
  107.      * </p>
  108.      * @param date date to check
  109.      * @param <T> type of the filed elements
  110.      * @return 60 or 61 depending on leap seconds introduction
  111.      * @since 9.0
  112.      */
  113.     default <T extends RealFieldElement<T>> int minuteDuration(final FieldAbsoluteDate<T> date) {
  114.         return 60;
  115.     }

  116.     /** Get the value of the previous leap.
  117.      * <p>
  118.      * This method will return 0.0 for all time scales that do <em>not</em>
  119.      * implement leap seconds.
  120.      * </p>
  121.      * @param date date to check
  122.      * @return value of the previous leap
  123.      */
  124.     default double getLeap(final AbsoluteDate date) {
  125.         return 0;
  126.     }

  127.     /** Get the value of the previous leap.
  128.      * <p>
  129.      * This method will return 0.0 for all time scales that do <em>not</em>
  130.      * implement leap seconds.
  131.      * </p>
  132.      * @param date date to check
  133.      * @param <T> type of the filed elements
  134.      * @return value of the previous leap
  135.      * @since 9.0
  136.      */
  137.     default <T extends RealFieldElement<T>> T getLeap(final FieldAbsoluteDate<T> date) {
  138.         return date.getField().getZero();
  139.     }

  140.     /** Get the name time scale.
  141.      * @return name of the time scale
  142.      */
  143.     String getName();

  144. }