Class TimeUtils

  • All Implemented Interfaces:

    
    public class TimeUtils
    
                        

    Provides utility methods for converting between different time formats. Two of the methods are taken from the Apache Commons Net package, and are copied here to avoid pulling in the whole package as a dependency.

    Author:

    Boris Grozev

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      TimeUtils()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static long toNtpTime(long t) Taken from from org.apache.commons.net.ntp.TimeStamp#toNtpTime(long) Converts Java time to 64-bit NTP time representation.
      static long getTime(long ntpTimeValue) Taken from from org.apache.commons.net.ntp.TimeStamp#toNtpTime(long) Convert 64-bit NTP timestamp to Java standard time.
      static long toNtpShortFormat(long ntpTime) Converts the given timestamp in NTP Timestamp Format into NTP Short Format (see "http://tools.ietf.
      static long ntpShortToMs(long ntpShortTime) Converts a timestamp in NTP Short Format (Q16.
      static long constructNtp(long msw, long lsw) Constructs a long representation of a timestamp in NTP Timestamp Format (see "http://tools.ietf.
      static long getMsw(long ntpTime) Gets the most significant word (32bits) from an NTP Timestamp represented as a long.
      static long getLsw(long ntpTime) Gets the least significant word (32bits) from an NTP Timestamp represented as a long.
      static String formatTimeAsFullMillis(long secs, int nanos) Formats a time -- represented by (long seconds, int nanos) -- as a String of floating-point milliseconds, in full precision.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TimeUtils

        TimeUtils()
    • Method Detail

      • toNtpTime

         static long toNtpTime(long t)

        Taken from from org.apache.commons.net.ntp.TimeStamp#toNtpTime(long) Converts Java time to 64-bit NTP time representation.

        Parameters:
        t - Java time
        Returns:

        NTP timestamp representation of Java time value.

      • getTime

         static long getTime(long ntpTimeValue)

        Taken from from org.apache.commons.net.ntp.TimeStamp#toNtpTime(long) Convert 64-bit NTP timestamp to Java standard time. Note that java time (milliseconds) by definition has less precision then NTP time (picoseconds) so converting NTP timestamp to java time and back to NTP timestamp loses precision. For example, Tue, Dec 17 2002 09:07:24.810 EST is represented by a single Java-based time value of f22cd1fc8a, but its NTP equivalent are all values ranging from c1a9ae1c.cf5c28f5 to c1a9ae1c.cf9db22c.

        Parameters:
        ntpTimeValue - the input time
        Returns:

        the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this NTP timestamp value.

      • toNtpShortFormat

         static long toNtpShortFormat(long ntpTime)

        Converts the given timestamp in NTP Timestamp Format into NTP Short Format (see "http://tools.ietf.org/html/rfc5905#section-6").

        Parameters:
        ntpTime - the timestamp to convert.
        Returns:

        the NTP Short Format timestamp, represented as a long.

      • ntpShortToMs

         static long ntpShortToMs(long ntpShortTime)

        Converts a timestamp in NTP Short Format (Q16.16, see "http://tools.ietf.org/html/rfc5905#section-6") into milliseconds.

        Parameters:
        ntpShortTime - the timestamp in NTP Short Format to convert.
        Returns:

        the number of milliseconds.

      • constructNtp

         static long constructNtp(long msw, long lsw)

        Constructs a long representation of a timestamp in NTP Timestamp Format (see "http://tools.ietf.org/html/rfc5905#section-6").

        Parameters:
        msw - The most significant word (32bits) represented as a long.
        lsw - The least significant word (32bits) represented as a long.
        Returns:

        the NTP timestamp constructed from msw and lsw.

      • getMsw

         static long getMsw(long ntpTime)

        Gets the most significant word (32bits) from an NTP Timestamp represented as a long.

        Parameters:
        ntpTime - the timestamp in NTP Timestamp Format.
        Returns:

        the MSW of ntpTime.

      • getLsw

         static long getLsw(long ntpTime)

        Gets the least significant word (32bits) from an NTP Timestamp represented as a long.

        Parameters:
        ntpTime - the timestamp in NTP Timestamp Format.
        Returns:

        the LSW of ntpTime.

      • formatTimeAsFullMillis

         static String formatTimeAsFullMillis(long secs, int nanos)

        Formats a time -- represented by (long seconds, int nanos) -- as a String of floating-point milliseconds, in full precision. This is designed to format the java.time.Duration and java.time.Interval classes, without being dependent on them. This should return a correct result for every valid (secs, nanos) pair.