<commit_before>/* Copyright 2002-2008 CS Communication & Systèmes
 * Licensed to CS Communication & Systèmes (CS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * CS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.orekit.time;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.orekit.errors.OrekitException;

/** Coordinated Universal Time.
 * <p>UTC is related to TAI using step adjustments from time to time
 * according to IERS (International Earth Rotation Service) rules. These
 * adjustments require introduction of leap seconds.</p>
 * <p>Leap seconds are always inserted as additional seconds at the last minute
 * of the day, pushing the next day forward. Such minutes are therefore more
 * than 60 seconds long. As an example, when a one second leap was introduced
 * at the end of 2005, the UTC time sequence was 2005-12-31T23:59:59 UTC,
 * followed by 2005-12-31T23:59:60 UTC, followed by 2006-01-01T00:00:00 UTC.</p>
 * <p>The OREKIT library retrieves time steps data thanks to the {@link
 * org.orekit.data.DataDirectoryCrawler DataDirectoryCrawler} class.</p>
 * <p>This is a singleton class, so there is no public constructor.</p>
 * @author <NAME>
 * @see AbsoluteDate
 * @version $Revision:1665 $ $Date:2008-06-11 12:12:59 +0200 (mer., 11 juin 2008) $
 */
public class UTCScale implements TimeScale {

    /** Serializable UID. */
    private static final long serialVersionUID = -920132549431081663L;

    /** Time steps. */
    private UTCTAIOffset[] offsets;

    /** Current position. */
    private transient int current;

    /** Private constructor for the singleton.
     */
    private UTCScale() {
    }

    /** Set the time steps from the predefined entries and the history
     * files in IERS folders.
     * @exception OrekitException if the time steps cannot be read
     */
    private synchronized void setTimeSteps() throws OrekitException {

        // gather all entries, both predefined and user supplied
        final SortedMap<ChunkedDate, Integer> entries = createPredefinedEntries();
        entries.putAll(new UTCTAIHistoryFilesLoader().loadTimeSteps());
        offsets = new UTCTAIOffset[entries.size() + 1];
        current = 0;

        // set up a first entry covering the far past before first leap second
        UTCTAIOffset last = new UTCTAIOffset(AbsoluteDate.PAST_INFINITY, 0, 0);
        offsets[current++] = last;

        // add leap second entries in chronological order
        for (Map.Entry<ChunkedDate, Integer> entry : entries.entrySet()) {
            final double offset            = entry.getValue().doubleValue();
            final double leap              = offset - last.getOffset();
            final AbsoluteDate taiDayStart = new AbsoluteDate(entry.getKey(), TAIScale.getInstance());
            final AbsoluteDate leapDate    = new AbsoluteDate(taiDayStart, last.getOffset());
            last.setValidityEnd(leapDate);
            last = new UTCTAIOffset(leapDate, leap, offset);
            offsets[current++] = last;
        }

        // set the current index on the last known leap
        --current;

    }

    /** Create predefined entries for UTC-TAI history.
     * <p>The predefined entries guarantee that at least already known values are
     * supported even if user as not set up a proper configuration.</p>
     * @return predefined entries
     * (contains at least date between 1972-01-01 and 2009-01-01)
     */
    private SortedMap<ChunkedDate, Integer> createPredefinedEntries() {
        final SortedMap<ChunkedDate, Integer> entries = new TreeMap<ChunkedDate, Integer>();
        entries.put(new ChunkedDate(1972, 1, 1), 10);
        entries.put(new ChunkedDate(1972, 7, 1), 11);
        entries.put(new ChunkedDate(1973, 1, 1), 12);
        entries.put(new ChunkedDate(1974, 1, 1), 13);<commit_msg>improved javadoc<commit_after>/* Copyright 2002-2008 CS Communication & Systèmes
 * Licensed to CS Communication & Systèmes (CS) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * CS licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.orekit.time;

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.orekit.errors.OrekitException;

/** Coordinated Universal Time.
 * <p>UTC is related to TAI using step adjustments from time to time
 * according to IERS (International Earth Rotation Service) rules. These
 * adjustments require introduction of leap seconds.</p>
 * <p>Leap seconds are always inserted as additional seconds at the last minute
 * of the day, pushing the next day forward. Such minutes are therefore more
 * than 60 seconds long. As an example, when a one second leap was introduced
 * at the end of 2005, the UTC time sequence was 2005-12-31T23:59:59 UTC,
 * followed by 2005-12-31T23:59:60 UTC, followed by 2006-01-01T00:00:00 UTC.</p>
 * <p>The OREKIT library retrieves time steps data thanks to the {@link
 * org.orekit.data.DataDirectoryCrawler DataDirectoryCrawler} class.</p>
 * <p>This is a singleton class, so there is no public constructor.</p>
 * @author <NAME>
 * @see AbsoluteDate
 * @version $Revision:1665 $ $Date:2008-06-11 12:12:59 +0200 (mer., 11 juin 2008) $
 */
public class UTCScale implements TimeScale {

    /** Serializable UID. */
    private static final long serialVersionUID = -920132549431081663L;

    /** Time steps. */
    private UTCTAIOffset[] offsets;

    /** Current position. */
    private transient int current;

    /** Private constructor for the singleton.
     */
    private UTCScale() {
    }

    /** Set the time steps from the predefined entries and the history
     * files in IERS folders.
     * @exception OrekitException if the time steps cannot be read
     */
    private synchronized void setTimeSteps() throws OrekitException {

        // gather all entries, both predefined and user supplied
        final SortedMap<ChunkedDate, Integer> entries = createPredefinedEntries();
        entries.putAll(new UTCTAIHistoryFilesLoader().loadTimeSteps());
        offsets = new UTCTAIOffset[entries.size() + 1];
        current = 0;

        // set up a first entry covering the far past before first leap second
        UTCTAIOffset last = new UTCTAIOffset(AbsoluteDate.PAST_INFINITY, 0, 0);
        offsets[current++] = last;

        // add leap second entries in chronological order
        for (Map.Entry<ChunkedDate, Integer> entry : entries.entrySet()) {
            final double offset            = entry.getValue().doubleValue();
            final double leap              = offset - last.getOffset();
            final AbsoluteDate taiDayStart = new AbsoluteDate(entry.getKey(), TAIScale.getInstance());
            final AbsoluteDate leapDate    = new AbsoluteDate(taiDayStart, last.getOffset());
            last.setValidityEnd(leapDate);
            last = new UTCTAIOffset(leapDate, leap, offset);
            offsets[current++] = last;
        }

        // set the current index on the last known leap
        --current;

    }

    /** Create predefined entries for UTC-TAI history.
     * <p>The predefined entries guarantee that at least already known values are
     * supported even if user as not set up a proper configuration.</p>
     * @return predefined entries
     * (contains at least all leaps between 1972-01-01 and 2009-01-01)
     */
    private SortedMap<ChunkedDate, Integer> createPredefinedEntries() {
        final SortedMap<ChunkedDate, Integer> entries = new TreeMap<ChunkedDate, Integer>();
        entries.put(new ChunkedDate(1972, 1, 1), 10);
        entries.put(new ChunkedDate(1972, 7, 1), 11);
        entries.put(new ChunkedDate(1973, 1, 1), 12);
        entries.put(new ChunkedDate(1974, 1, 1), 13);
{'fraction_non_alphanumeric': 0.08858223903850435, 'fraction_numerical': 0.035165813487647454, 'mean_word_length': 3.197571228397945, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4882007', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2395, 'n_words': 957}