TimeConverter.java

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

  18. import org.orekit.time.AbsoluteDate;
  19. import org.orekit.time.DateTimeComponents;
  20. import org.orekit.time.SatelliteClockScale;
  21. import org.orekit.time.TimeScale;

  22. /** Dates reader/writer.
  23.  *
  24.  * @author Luc Maisonobe
  25.  * @since 11.0
  26.  */
  27. public class TimeConverter {

  28.     /** Base time scale. */
  29.     private final TimeScale timeScale;

  30.     /** Reference date for relative dates (may be null if no relative dates are used). */
  31.     private final AbsoluteDate referenceDate;

  32.     /** Build a time system.
  33.      * @param timeScale base time scale
  34.      * @param referenceDate reference date for relative dates (may be null if no relative dates are used)
  35.      */
  36.     public TimeConverter(final TimeScale timeScale, final AbsoluteDate referenceDate) {
  37.         this.timeScale     = timeScale;
  38.         this.referenceDate = referenceDate;
  39.     }

  40.     /** Parse a relative or absolute date.
  41.      * @param s string to parse
  42.      * @return parsed date
  43.      */
  44.     public AbsoluteDate parse(final String s) {

  45.         if (referenceDate != null && s.indexOf('T') < 0) {

  46.             // relative date
  47.             final double delta = Double.parseDouble(s);
  48.             if (timeScale instanceof SatelliteClockScale) {
  49.                 // satellite clock scale handles drifts internally
  50.                 return ((SatelliteClockScale) timeScale).dateAtCount(delta);
  51.             } else {
  52.                 // regular relative date from a known reference
  53.                 return referenceDate.shiftedBy(delta);
  54.             }

  55.         } else {

  56.             // absolute date
  57.             return new AbsoluteDate(s, timeScale);

  58.         }

  59.     }

  60.     /** Generate calendar components.
  61.      * @param date date to convert
  62.      * @return date components
  63.      */
  64.     public DateTimeComponents components(final AbsoluteDate date) {
  65.         return date.getComponents(timeScale);
  66.     }

  67.     /** Generate relative offset.
  68.      * @param date date to convert
  69.      * @return relative offset
  70.      */
  71.     public double offset(final AbsoluteDate date) {
  72.         return date.durationFrom(referenceDate);
  73.     }

  74.     /** Get the base time scale.
  75.      * @return base time scale
  76.      */
  77.     public TimeScale getTimeScale() {
  78.         return timeScale;
  79.     }

  80.     /** Get the reference date for relative dates (may be null if no relative dates are used).
  81.      * @return reference date for relative dates (may be null if no relative dates are used)
  82.      * @since 12.0
  83.      */
  84.     public AbsoluteDate getReferenceDate() {
  85.         return referenceDate;
  86.     }

  87. }