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  
19  import org.orekit.files.iirv.terms.base.LongValuedIIRVTerm;
20  import org.orekit.time.AbsoluteDate;
21  import org.orekit.time.DateComponents;
22  import org.orekit.time.DateTimeComponents;
23  import org.orekit.time.UTCScale;
24  
25  /**
26   * 3-character integer representing the day of the year.
27   * <p>
28   * Valid values: 001-366 (365 + 1 for leap year)
29   *
30   * @author Nick LaFarge
31   * @since 13.0
32   */
33  public class DayOfYearTerm extends LongValuedIIRVTerm {
34  
35      /** The length of the IIRV term within the message. */
36      public static final int DAY_OF_YEAR_LENGTH = 3;
37  
38      /** Regular expression that ensures the validity of string values for this term. */
39      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])";
40  
41      /**
42       * Constructor.
43       * <p>
44       * See {@link LongValuedIIRVTerm#LongValuedIIRVTerm(String, String, int, boolean)}
45       *
46       * @param stringValue Day of the year (001-366)
47       */
48      public DayOfYearTerm(final String stringValue) {
49          super(DAY_OF_YEAR_PATTERN, stringValue, DAY_OF_YEAR_LENGTH, false);
50      }
51  
52      /**
53       * Constructor.
54       * <p>
55       * See {@link LongValuedIIRVTerm#LongValuedIIRVTerm(String, long, int, boolean)}
56       *
57       * @param value Day of the year (001-366)
58       */
59      public DayOfYearTerm(final long value) {
60          super(DAY_OF_YEAR_PATTERN, value, DAY_OF_YEAR_LENGTH, false);
61      }
62  
63      /**
64       * Constructs a DayOfYearTerm object from an {@link AbsoluteDate} object.
65       *
66       * @param absoluteDate date object from which to infer the day of year
67       * @param utc          UTC time scale
68       */
69      public DayOfYearTerm(final AbsoluteDate absoluteDate, final UTCScale utc) {
70          this(fromAbsoluteDate(absoluteDate, utc));
71      }
72  
73      /**
74       * Constructs an IIRV DayOfYear from an {@link AbsoluteDate} object.
75       *
76       * @param absoluteDate date object from which to infer the day of year
77       * @param utc          UTC time scale
78       * @return day of year associated with the inputted absolute date
79       */
80      private static String fromAbsoluteDate(final AbsoluteDate absoluteDate, final UTCScale utc) {
81          final DateTimeComponents components = absoluteDate.getComponents(utc);
82          final DateComponents date = components.getDate();
83  
84          return IIRVTermUtils.addPadding(String.valueOf(date.getDayOfYear()), '0', 3, true);
85      }
86  
87      /**
88       * Returns the {@link DateComponents} instance that corresponds this term's value.
89       *
90       * @param year year to associated with the created date components
91       * @return the date components associated with this term
92       */
93      public DateComponents getDateComponents(final int year) {
94          return new DateComponents(year, toInt());
95      }
96  }