Fields Summary |
---|
protected static final long | msb0baseTimebaseline NTP time if bit-0=0 -> 7-Feb-2036 @ 06:28:16 UTC |
protected static final long | msb1baseTimebaseline NTP time if bit-0=1 -> 1-Jan-1900 @ 01:00:00 UTC |
public static final String | NTP_DATE_FORMATDefault NTP date string format. E.g. Fri, Sep 12 2003 21:06:23.860.
See java.text.SimpleDateFormat for code descriptions. |
private static SoftReference | simpleFormatter |
private static SoftReference | utcFormatter |
private long | ntpTimeNTP timestamp value: 64-bit unsigned fixed-point number as defined in RFC-1305
with high-order 32 bits the seconds field and the low-order 32-bits the
fractional field. |
private static final long | serialVersionUID |
Constructors Summary |
---|
public TimeStamp(long ntpTime)Constructs a newly allocated NTP timestamp object
that represents the native 64-bit long argument.
// initialization of static time bases
/*
static {
TimeZone utcZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(utcZone);
calendar.set(1900, Calendar.JANUARY, 1, 0, 0, 0);
calendar.set(Calendar.MILLISECOND, 0);
msb1baseTime = calendar.getTime().getTime();
calendar.set(2036, Calendar.FEBRUARY, 7, 6, 28, 16);
calendar.set(Calendar.MILLISECOND, 0);
msb0baseTime = calendar.getTime().getTime();
}
*/
this.ntpTime = ntpTime;
|
public TimeStamp(String s)Constructs a newly allocated NTP timestamp object
that represents the value represented by the string
in hexdecimal form (e.g. "c1a089bd.fc904f6d").
ntpTime = decodeNtpHexString(s);
|
public TimeStamp(Date d)Constructs a newly allocated NTP timestamp object
that represents the Java Date argument.
ntpTime = (d == null) ? 0 : toNtpTime(d.getTime());
|
Methods Summary |
---|
private static void | appendHexString(java.lang.StringBuffer buf, long l)Left-pad 8-character hex string with 0's
String s = Long.toHexString(l);
for (int i = s.length(); i < 8; i++)
buf.append('0");
buf.append(s);
|
public int | compareTo(org.apache.commons.net.ntp.TimeStamp anotherTimeStamp)Compares two Timestamps numerically.
long thisVal = this.ntpTime;
long anotherVal = anotherTimeStamp.ntpTime;
return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1));
|
public int | compareTo(java.lang.Object o)Compares this TimeStamp to another Object. If the Object is a TimeStamp,
this function behaves like compareTo(TimeStamp) . Otherwise,
it throws a ClassCastException (as TimeStamps are comparable
only to other TimeStamps).
return compareTo((TimeStamp) o);
|
protected static long | decodeNtpHexString(java.lang.String s)Convert NTP timestamp hexstring (e.g. "c1a089bd.fc904f6d") to the NTP
64-bit unsigned fixed-point number.
if (s == null) {
throw new NumberFormatException("null");
}
int ind = s.indexOf('.");
if (ind == -1) {
if (s.length() == 0) return 0;
return Long.parseLong(s, 16) << 32; // no decimal
}
return Long.parseLong(s.substring(0, ind), 16) << 32 |
Long.parseLong(s.substring(ind + 1), 16);
|
public boolean | equals(java.lang.Object obj)Compares this object against the specified object.
The result is true if and only if the argument is
not null and is a Long object that
contains the same long value as this object.
if (obj instanceof TimeStamp) {
return ntpTime == ((TimeStamp) obj).ntpValue();
}
return false;
|
public static org.apache.commons.net.ntp.TimeStamp | getCurrentTime()Constructs a NTP timestamp object and initializes it so that
it represents the time at which it was allocated, measured to the
nearest millisecond.
return getNtpTime(System.currentTimeMillis());
|
public java.util.Date | getDate()Convert NTP timestamp to Java Date object.
long time = getTime(ntpTime);
return new Date(time);
|
public long | getFraction()Returns low-order 32-bits representing the fractional seconds.
return ntpTime & 0xffffffffL;
|
public static org.apache.commons.net.ntp.TimeStamp | getNtpTime(long date)Helper method to convert Java time to NTP timestamp object.
Note that Java time (milliseconds) by definition has less precision
then NTP time (picoseconds) so converting Ntptime to Javatime and back
to Ntptime loses precision. For example, Tue, Dec 17 2002 09:07:24.810
is represented by a single Java-based time value of f22cd1fc8a, but its
NTP equivalent are all values from c1a9ae1c.cf5c28f5 to c1a9ae1c.cf9db22c.
return new TimeStamp(toNtpTime(date));
|
public long | getSeconds()Returns high-order 32-bits representing the seconds of this NTP timestamp.
return (ntpTime >>> 32) & 0xffffffffL;
|
public long | getTime()Convert NTP timestamp to Java standard time.
return getTime(ntpTime);
|
public static long | getTime(long ntpTimeValue)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.
long seconds = (ntpTimeValue >>> 32) & 0xffffffffL; // high-order 32-bits
long fraction = ntpTimeValue & 0xffffffffL; // low-order 32-bits
// Use round-off on fractional part to preserve going to lower precision
fraction = Math.round(1000D * fraction / 0x100000000L);
/*
* If the most significant bit (MSB) on the seconds field is set we use
* a different time base. The following text is a quote from RFC-2030 (SNTP v4):
*
* If bit 0 is set, the UTC time is in the range 1968-2036 and UTC time
* is reckoned from 0h 0m 0s UTC on 1 January 1900. If bit 0 is not set,
* the time is in the range 2036-2104 and UTC time is reckoned from
* 6h 28m 16s UTC on 7 February 2036.
*/
long msb = seconds & 0x80000000L;
if (msb == 0) {
// use base: 7-Feb-2036 @ 06:28:16 UTC
return msb0baseTime + (seconds * 1000) + fraction;
} else {
// use base: 1-Jan-1900 @ 01:00:00 UTC
return msb1baseTime + (seconds * 1000) + fraction;
}
|
public int | hashCode()Computes a hashcode for this Timestamp. The result is the exclusive
OR of the two halves of the primitive long value
represented by this TimeStamp object. That is, the hashcode
is the value of the expression:
(int)(this.ntpValue()^(this.ntpValue() >>> 32))
return (int) (ntpTime ^ (ntpTime >>> 32));
|
public long | ntpValue()Returns the value of this Timestamp as a long value.
return ntpTime;
|
public static org.apache.commons.net.ntp.TimeStamp | parseNtpString(java.lang.String s)Parses the string argument as a NTP hexidecimal timestamp representation string
(e.g. "c1a089bd.fc904f6d").
return new TimeStamp(decodeNtpHexString(s));
|
public java.lang.String | toDateString()Converts this TimeStamp object to a String
of the form:
EEE, MMM dd yyyy HH:mm:ss.SSS
See java.text.SimpleDataFormat for code descriptions.
DateFormat formatter = null;
if (simpleFormatter != null) {
formatter = (DateFormat) simpleFormatter.get();
}
if (formatter == null) {
// No cache yet, or cached formatter GC'd
formatter = new SimpleDateFormat(NTP_DATE_FORMAT, Locale.US);
formatter.setTimeZone(TimeZone.getDefault());
simpleFormatter = new SoftReference(formatter);
}
Date ntpDate = getDate();
synchronized (formatter) {
return formatter.format(ntpDate);
}
|
protected static long | toNtpTime(long t)Converts Java time to 64-bit NTP time representation.
boolean useBase1 = t < msb0baseTime; // time < Feb-2036
long baseTime;
if (useBase1) {
baseTime = t - msb1baseTime; // dates <= Feb-2036
} else {
// if base0 needed for dates >= Feb-2036
baseTime = t - msb0baseTime;
}
long seconds = baseTime / 1000;
long fraction = ((baseTime % 1000) * 0x100000000L) / 1000;
if (useBase1) {
seconds |= 0x80000000L; // set high-order bit if msb1baseTime 1900 used
}
long time = seconds << 32 | fraction;
return time;
|
public java.lang.String | toString()Converts this TimeStamp object to a String .
The NTP timestamp 64-bit long value is represented as hex string with
seconds separated by fractional seconds by a decimal point;
e.g. c1a089bd.fc904f6d <=> Tue, Dec 10 2002 10:41:49.986
return toString(ntpTime);
|
public static java.lang.String | toString(long ntpTime)Converts 64-bit NTP timestamp value to a String .
The NTP timestamp value is represented as hex string with
seconds separated by fractional seconds by a decimal point;
e.g. c1a089bd.fc904f6d <=> Tue, Dec 10 2002 10:41:49.986
StringBuffer buf = new StringBuffer();
// high-order second bits (32..63) as hexstring
appendHexString(buf, (ntpTime >>> 32) & 0xffffffffL);
// low-order fractional seconds bits (0..31) as hexstring
buf.append('.");
appendHexString(buf, ntpTime & 0xffffffffL);
return buf.toString();
|
public java.lang.String | toUTCString()Converts this TimeStamp object to a String
of the form:
EEE, MMM dd yyyy HH:mm:ss.SSS UTC
See java.text.SimpleDataFormat for code descriptions.
DateFormat formatter = null;
if (utcFormatter != null)
formatter = (DateFormat) utcFormatter.get();
if (formatter == null) {
// No cache yet, or cached formatter GC'd
formatter = new SimpleDateFormat(NTP_DATE_FORMAT + " 'UTC'",
Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
utcFormatter = new SoftReference(formatter);
}
Date ntpDate = getDate();
synchronized (formatter) {
return formatter.format(ntpDate);
}
|