TimeScale.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 org.hipparchus.CalculusFieldElement;

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

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

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

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

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

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

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

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

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

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

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

  143. }