Class TimeUtils


  • public class TimeUtils
    extends Object
    There are various versions of timestamps in the archiver appliance. The most commonly used one is java.sql.Timestamp and is the one exposed in the APIs. The other versions are follows
    1. A long epoch seconds + optional nanos - The seconds part is the java epoch seconds, as returned by System.currentMillis()/1000
    2. A long epoch milliseconds - This is the java epoch milliseconds, as returned by System.currentMillis()
    3. A JCA timestamp - This is what comes out of JCA
    4. A year/secondsintoyear/nanos combination - This is what is used in the protocol buffer storage plugin.
    5. A ISO 8601 date time - We use JODA to convert from/to a ISO 8601 string
    This class contains utilities to convert various forms of timestamps to/from the java.sql.Timestamp.
    Author:
    mshankar
    • Field Detail

      • EPICS_EPOCH_2_JAVA_EPOCH_OFFSET

        public static final long EPICS_EPOCH_2_JAVA_EPOCH_OFFSET
        EPICS epoch starts at January 1, 1990 UTC. This constant contains the offset that must be added to epicstimestamps to generate java timestamps.
      • logger

        public static org.apache.log4j.Logger logger
    • Constructor Detail

      • TimeUtils

        public TimeUtils()
    • Method Detail

      • convertFromEpochSeconds

        public static Timestamp convertFromEpochSeconds​(long epochSeconds,
                                                        int nanos)
      • convertFromEpochMillis

        public static Timestamp convertFromEpochMillis​(long epochMillis)
      • convertFromJCATimeStamp

        public static Timestamp convertFromJCATimeStamp​(gov.aps.jca.dbr.TimeStamp jcats)
      • convertFromISO8601String

        public static Timestamp convertFromISO8601String​(String tsstr)
      • convertFromDateTimeStringWithOffset

        public static Timestamp convertFromDateTimeStringWithOffset​(String tsstr)
      • convertToEpochSeconds

        public static long convertToEpochSeconds​(Timestamp ts)
      • convertToEpochMillis

        public static long convertToEpochMillis​(Timestamp ts)
      • convertToYearSecondTimestamp

        public static YearSecondTimestamp convertToYearSecondTimestamp​(gov.aps.jca.dbr.TimeStamp jcats)
      • convertToYearSecondTimestamp

        public static YearSecondTimestamp convertToYearSecondTimestamp​(long epochSeconds)
      • convertToISO8601String

        public static String convertToISO8601String​(Timestamp ts)
      • convertToISO8601String

        public static String convertToISO8601String​(long epochSeconds)
      • convertToHumanReadableString

        public static String convertToHumanReadableString​(Timestamp ts)
      • convertToHumanReadableString

        public static String convertToHumanReadableString​(long epochSeconds)
      • convertToLocalEpochMillis

        public static long convertToLocalEpochMillis​(long epochMillis)
      • getStartOfCurrentYearInSeconds

        public static long getStartOfCurrentYearInSeconds()
      • getStartOfYearInSeconds

        public static long getStartOfYearInSeconds​(int year)
      • getStartOfYear

        public static Timestamp getStartOfYear​(int year)
      • getStartOfYearInSeconds

        public static long getStartOfYearInSeconds​(long epochseconds)
      • getStartOfYearInSeconds

        public static long getStartOfYearInSeconds​(short year)
        In the protocol buffer storage plugin, we send the year as a short
        Parameters:
        year - Year
        Returns:
        startOfYearInEpochSeconds  
      • getEndOfYear

        public static Timestamp getEndOfYear​(int year)
      • getStartOfCurrentDayInEpochSeconds

        public static long getStartOfCurrentDayInEpochSeconds()
      • getSecondsIntoYear

        public static int getSecondsIntoYear​(long epochseconds,
                                             long startOfYearInSeconds)
        Convert Java EPOCH seconds to a seconds into year given the start of the year in EPOCH seconds
        Parameters:
        epochseconds -  
        startOfYearInSeconds -  
        Returns:
        SecondsIntoYear The difference in Seconds
      • getSecondsIntoYear

        public static int getSecondsIntoYear​(long epochseconds)
        Convert Java EPOCH seconds to a seconds into year
        Parameters:
        epochseconds -  
        Returns:
        SecondsIntoYear The difference in Seconds
      • computeYearForEpochSeconds

        public static short computeYearForEpochSeconds​(long epochseconds)
        Determine year from java epoch seconds.
        Parameters:
        epochseconds -  
        Returns:
        YearForEpochSeconds  
      • getCurrentYear

        public static short getCurrentYear()
        Get the current year in the UTC timezone
        Returns:
        CurrentYear  
      • getCurrentEpochSeconds

        public static long getCurrentEpochSeconds()
        Gets the current epoch seconds in the UTC timezone
        Returns:
        currentEpochSeconds  
      • getCurrentEpochMilliSeconds

        public static long getCurrentEpochMilliSeconds()
        Gets the current epoch milli seconds in the UTC timezone
        Returns:
        currentEpochMilliSeconds  
      • now

        public static Timestamp now()
        Gets "now" as a Timestamp in the UTC timezone
        Returns:
        now A Timestamp in the UTC timezone
      • nowYTS

        public static YearSecondTimestamp nowYTS()
        Gets "now" as a YearSecondTimestamp in the UTC timezone
        Returns:
        now A YearSecondTimestamp in the UTC timezone
      • breakIntoYearlyTimeSpans

        public static List<TimeSpan> breakIntoYearlyTimeSpans​(Timestamp start,
                                                              Timestamp end)
        Given a start time and an end time, this method breaks this span into a sequence of spans each of which fits within a year. Used where data is partitioned by year....
        Parameters:
        start - The start time
        end - The end time
        Returns:
        breakIntoYearlyTimeSpans  
      • getPartitionName

        public static String getPartitionName​(long epochSeconds,
                                              PartitionGranularity granularity)
        Returns a partition name for the given epoch second based on the partition granularity.
        Parameters:
        epochSeconds -  
        granularity - Partition granularity of the file.
        Returns:
        PartitionName  
      • getNextPartitionFirstSecond

        public static long getNextPartitionFirstSecond​(long epochSeconds,
                                                       PartitionGranularity granularity)
        Given an epoch seconds and a granularity, this method gives you the first second in the next partition as epoch seconds.
        Parameters:
        epochSeconds -  
        granularity - Partition granularity of the file.
        Returns:
        NextPartitionFirstSecond  
      • getPreviousPartitionLastSecond

        public static long getPreviousPartitionLastSecond​(long epochSeconds,
                                                          PartitionGranularity granularity)
        Given an epoch seconds and a granularity, this method gives you the last second in the previous partition as epoch seconds.
        Parameters:
        epochSeconds -  
        granularity - Partition granularity of the file.
        Returns:
        PreviousPartitionLastSecond  
      • convertToTenthsOfASecond

        public static long convertToTenthsOfASecond​(long epochSeconds,
                                                    int nanos)
                                             throws NumberFormatException
        Event rate rate limiting uses a tenths of a seconds units to cater to monitor intervals of 0.1 seconds, 0.5 seconds etc.. This converts a epochSeconds+nanos to time in terms of tenths of a second.
        Parameters:
        epochSeconds -  
        nanos -  
        Returns:
        TenthsOfASecond  
        Throws:
        NumberFormatException -  
      • isDST

        public static boolean isDST​(Timestamp ts)
        Whether we are in DST for a particular time in the servers default timezone. Mostly used by Matlab.
        Parameters:
        ts - Timestamp
        Returns:
        boolean True or False
      • breakIntoIntervals

        public static List<TimeSpan> breakIntoIntervals​(Timestamp start,
                                                        Timestamp end,
                                                        long binSizeInSeconds)
        Break a time span into smaller time spans according to binSize The first time span has the start time and the end of the first bin. The next one has the end of the first bin and the start of the second bin. The last time span has the end as its end. This is sometimes used to try to speed up retrieval when using post processors over a large time span.
        Parameters:
        start - Timestamp start
        end - Timestamp end
        binSizeInSeconds -  
        Returns:
        TimeSpan The list of smaller time spans according to binSize