DayOfYearTerm.java

  1. /* Copyright 2024-2025 The Johns Hopkins University Applied Physics Laboratory
  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.iirv.terms;

  18. import org.orekit.files.iirv.terms.base.LongValuedIIRVTerm;
  19. import org.orekit.time.AbsoluteDate;
  20. import org.orekit.time.DateComponents;
  21. import org.orekit.time.DateTimeComponents;
  22. import org.orekit.time.UTCScale;

  23. /**
  24.  * 3-character integer representing the day of the year.
  25.  * <p>
  26.  * Valid values: 001-366 (365 + 1 for leap year)
  27.  *
  28.  * @author Nick LaFarge
  29.  * @since 13.0
  30.  */
  31. public class DayOfYearTerm extends LongValuedIIRVTerm {

  32.     /** The length of the IIRV term within the message. */
  33.     public static final int DAY_OF_YEAR_LENGTH = 3;

  34.     /** Regular expression that ensures the validity of string values for this term. */
  35.     public static final String DAY_OF_YEAR_PATTERN = "(00[1-9]|0[1-9][0-9]|[1-2][0-9]{2}|3[0-5][0-9]|36[0-6])";

  36.     /**
  37.      * Constructor.
  38.      * <p>
  39.      * See {@link LongValuedIIRVTerm#LongValuedIIRVTerm(String, String, int, boolean)}
  40.      *
  41.      * @param stringValue Day of the year (001-366)
  42.      */
  43.     public DayOfYearTerm(final String stringValue) {
  44.         super(DAY_OF_YEAR_PATTERN, stringValue, DAY_OF_YEAR_LENGTH, false);
  45.     }

  46.     /**
  47.      * Constructor.
  48.      * <p>
  49.      * See {@link LongValuedIIRVTerm#LongValuedIIRVTerm(String, long, int, boolean)}
  50.      *
  51.      * @param value Day of the year (001-366)
  52.      */
  53.     public DayOfYearTerm(final long value) {
  54.         super(DAY_OF_YEAR_PATTERN, value, DAY_OF_YEAR_LENGTH, false);
  55.     }

  56.     /**
  57.      * Constructs a DayOfYearTerm object from an {@link AbsoluteDate} object.
  58.      *
  59.      * @param absoluteDate date object from which to infer the day of year
  60.      * @param utc          UTC time scale
  61.      */
  62.     public DayOfYearTerm(final AbsoluteDate absoluteDate, final UTCScale utc) {
  63.         this(fromAbsoluteDate(absoluteDate, utc));
  64.     }

  65.     /**
  66.      * Constructs an IIRV DayOfYear from an {@link AbsoluteDate} object.
  67.      *
  68.      * @param absoluteDate date object from which to infer the day of year
  69.      * @param utc          UTC time scale
  70.      * @return day of year associated with the inputted absolute date
  71.      */
  72.     private static String fromAbsoluteDate(final AbsoluteDate absoluteDate, final UTCScale utc) {
  73.         final DateTimeComponents components = absoluteDate.getComponents(utc);
  74.         final DateComponents date = components.getDate();

  75.         return IIRVTermUtils.addPadding(String.valueOf(date.getDayOfYear()), '0', 3, true);
  76.     }

  77.     /**
  78.      * Returns the {@link DateComponents} instance that corresponds this term's value.
  79.      *
  80.      * @param year year to associated with the created date components
  81.      * @return the date components associated with this term
  82.      */
  83.     public DateComponents getDateComponents(final int year) {
  84.         return new DateComponents(year, toInt());
  85.     }
  86. }