<commit_before>/*
 * Copyright 2010-11 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */

package org.epics.pvmanager.util;

import java.util.Date;

/**
 * Represent a time stamp at nanosecond accuracy. The time is internally stored
 * as two values: the UNIX timestamp (number of seconds since
 * 1/1/1970) and the nanoseconds past that timestamp. The UNIX timestamp is
 * stored as a signed long, which has the range of 292 billion years before
 * and another 292 past the epoch.
 * <p>
 * Note that while TimeStamp are usually created according to system clocks which
 * takes into account leap seconds, all the math operations on TimeStamps do
 * not take leap seconds into account.
 *
 * @author carcassi
 */
public class TimeStamp implements Comparable {

    /*
     * When the class is initialized, get the current timestamp and nanotime,
     * so that future instances can be calculated from this reference.
     */
    private static final TimeStamp base = TimeStamp.timestampOf(new Date());
    private static final long baseNano = System.nanoTime();

    /**
     * Unix timestamp
     */
    private final long unixSec;

    /**
     * Nanoseconds past the timestamp. Must be 0 < nanoSec < 999,999,999
     */
    private final long nanoSec;

    private TimeStamp(long unixSec, long nanoSec) {
        if (nanoSec < 0 || nanoSec > 999999999)
            throw new IllegalArgumentException("Nanoseconds cannot be between 0 and 999,999,999");
        this.unixSec = unixSec;
        this.nanoSec = nanoSec;
    }

    /**
     * Unix time; seconds from midnight 1/1/1970.
     * @return unix time
     */
    public long getSec() {
        return unixSec;
    }

    /**
     * Nanoseconds within the given second.
     * @return nanoseconds (0 < nanoSec < 999,999,999)
     */
    public long getNanoSec() {
        return nanoSec;
    }

    /**
     * Returns a new timestamp from UNIX time.
     *
     * @param unixSec number of seconds in the UNIX epoch.
     * @param nanoSec nanoseconds past the given seconds (must be 0 < nanoSec < 999,999,999)
     * @return a new timestamp
     */
    public static TimeStamp time(long unixSec, long nanoSec) {
        return new TimeStamp(unixSec, nanoSec);
    }

    /**
     * Converts a {@link java.util.Date} to a timestamp. Date is accurate to
     * milliseconds, so the last 6 digits are always going to be zeros.
     *
     * @param date the date to convert
     * @return a new timestamp
     */
    public static TimeStamp timestampOf(Date date) {
        long time = date.getTime();
        long nanoSec = (time % 1000) * 1000000;
        long epicsSec = (time / 1000);
        return time(epicsSec, nanoSec);
    }

    /**
     * Returns a new timestamp for the current instant. The timestamp is calculated
     * using {@link java.lang.System#nanoTime()}, so it has the accuracy given
     * by that function.
     *
     * @return a new timestamp
     */
    public static TimeStamp now() {
        return base.plus(TimeDuration.nanos(System.nanoTime() - baseNano));
    }

    /**
     * Converts the time stamp to a standard Date. The conversion is done once,
     * and it trims all precision below milliSec.
     *
     * @return a date
     */
    public Date asDate() {
        return new Date((unixSec)*1000+nanoSec/1000000);
    }

    @Override
    public int hashCode() {
        return Long.valueOf(nanoSec).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TimeStamp) {
            TimeStamp other = (TimeStamp) obj;
            return other.nanoSec == nanoSec && other.unixSec == unixSec;
        }

        return false;
    }

    /**
     * Defines the natural ordering for timestamp as forward in time.
     *
     * @param o another object
     * @return comparison result
     */
    @Override
    public int compareTo(Object o) {
        TimeStamp other = (TimeStamp) o;
	if (unixSec < other.unixSec) {
            return -1;
        } else if (unixSec == other.unixSec) {
            if (nanoSec < other.nanoSec) {
                return -1;
            } else if (nanoSec == other.nanoSec) {
                return 0;
            } else {
                return 1;
            }
        } else {
            return 1;
        }
    }

    /**
     * Adds the given duration to this timestamp and returns the result.
     * @param duration a time duration
     * @return a new timestamp
     */
    public TimeStamp plus(TimeDuration duration) {
        return createWithCarry(unixSec, nanoSec + duration.getNanoSec());
    }

    /**<commit_msg>TimeStamp implementation of Comparable didn't use generics.<commit_after>/*
 * Copyright 2010-11 Brookhaven National Laboratory
 * All rights reserved. Use is subject to license terms.
 */

package org.epics.pvmanager.util;

import java.util.Date;

/**
 * Represent a time stamp at nanosecond accuracy. The time is internally stored
 * as two values: the UNIX timestamp (number of seconds since
 * 1/1/1970) and the nanoseconds past that timestamp. The UNIX timestamp is
 * stored as a signed long, which has the range of 292 billion years before
 * and another 292 past the epoch.
 * <p>
 * Note that while TimeStamp are usually created according to system clocks which
 * takes into account leap seconds, all the math operations on TimeStamps do
 * not take leap seconds into account.
 *
 * @author carcassi
 */
public class TimeStamp implements Comparable<TimeStamp> {

    /*
     * When the class is initialized, get the current timestamp and nanotime,
     * so that future instances can be calculated from this reference.
     */
    private static final TimeStamp base = TimeStamp.timestampOf(new Date());
    private static final long baseNano = System.nanoTime();

    /**
     * Unix timestamp
     */
    private final long unixSec;

    /**
     * Nanoseconds past the timestamp. Must be 0 < nanoSec < 999,999,999
     */
    private final long nanoSec;

    private TimeStamp(long unixSec, long nanoSec) {
        if (nanoSec < 0 || nanoSec > 999999999)
            throw new IllegalArgumentException("Nanoseconds cannot be between 0 and 999,999,999");
        this.unixSec = unixSec;
        this.nanoSec = nanoSec;
    }

    /**
     * Unix time; seconds from midnight 1/1/1970.
     * @return unix time
     */
    public long getSec() {
        return unixSec;
    }

    /**
     * Nanoseconds within the given second.
     * @return nanoseconds (0 < nanoSec < 999,999,999)
     */
    public long getNanoSec() {
        return nanoSec;
    }

    /**
     * Returns a new timestamp from UNIX time.
     *
     * @param unixSec number of seconds in the UNIX epoch.
     * @param nanoSec nanoseconds past the given seconds (must be 0 < nanoSec < 999,999,999)
     * @return a new timestamp
     */
    public static TimeStamp time(long unixSec, long nanoSec) {
        return new TimeStamp(unixSec, nanoSec);
    }

    /**
     * Converts a {@link java.util.Date} to a timestamp. Date is accurate to
     * milliseconds, so the last 6 digits are always going to be zeros.
     *
     * @param date the date to convert
     * @return a new timestamp
     */
    public static TimeStamp timestampOf(Date date) {
        long time = date.getTime();
        long nanoSec = (time % 1000) * 1000000;
        long epicsSec = (time / 1000);
        return time(epicsSec, nanoSec);
    }

    /**
     * Returns a new timestamp for the current instant. The timestamp is calculated
     * using {@link java.lang.System#nanoTime()}, so it has the accuracy given
     * by that function.
     *
     * @return a new timestamp
     */
    public static TimeStamp now() {
        return base.plus(TimeDuration.nanos(System.nanoTime() - baseNano));
    }

    /**
     * Converts the time stamp to a standard Date. The conversion is done once,
     * and it trims all precision below milliSec.
     *
     * @return a date
     */
    public Date asDate() {
        return new Date((unixSec)*1000+nanoSec/1000000);
    }

    @Override
    public int hashCode() {
        return Long.valueOf(nanoSec).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof TimeStamp) {
            TimeStamp other = (TimeStamp) obj;
            return other.nanoSec == nanoSec && other.unixSec == unixSec;
        }

        return false;
    }

    /**
     * Defines the natural ordering for timestamp as forward in time.
     *
     * @param o another object
     * @return comparison result
     */
    @Override
    public int compareTo(TimeStamp other) {
	if (unixSec < other.unixSec) {
            return -1;
        } else if (unixSec == other.unixSec) {
            if (nanoSec < other.nanoSec) {
                return -1;
            } else if (nanoSec == other.nanoSec) {
                return 0;
            } else {
                return 1;
            }
        } else {
            return 1;
        }
    }

    /**
     * Adds the given duration to this timestamp and returns the result.
     * @param duration a time duration
     * @return a new timestamp
     */
    public TimeStamp plus(TimeDuration duration) {
        return createWithCarry(unixSec, nanoSec + duration.getNanoSec());
    }

    /**
{'fraction_non_alphanumeric': 0.08737759604003013, 'fraction_numerical': 0.022812869902076833, 'mean_word_length': 2.2081463583016916, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '2891298', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2600, 'n_words': 966}