1 package org.apache.commons.net.ntp; 2 17 18 19 import java.util.TimeZone ; 20 import java.util.Date ; 21 import java.util.Locale ; 22 import java.lang.ref.SoftReference ; 23 import java.text.SimpleDateFormat ; 24 import java.text.DateFormat ; 25 26 44 public class TimeStamp implements java.io.Serializable , Comparable 45 { 46 47 50 protected static final long msb0baseTime = 2085978496000L; 51 52 55 protected static final long msb1baseTime = -2208988800000L; 56 57 61 public final static String NTP_DATE_FORMAT = "EEE, MMM dd yyyy HH:mm:ss.SSS"; 62 63 66 private static SoftReference simpleFormatter = null; 67 private static SoftReference utcFormatter = null; 68 69 74 private long ntpTime; 75 76 private static final long serialVersionUID = 8139806907588338737L; 77 78 91 92 96 public TimeStamp(long ntpTime) 97 { 98 this.ntpTime = ntpTime; 99 } 100 101 108 public TimeStamp(String s) throws NumberFormatException 109 { 110 ntpTime = decodeNtpHexString(s); 111 } 112 113 119 public TimeStamp(Date d) 120 { 121 ntpTime = (d == null) ? 0 : toNtpTime(d.getTime()); 122 } 123 124 129 public long ntpValue() 130 { 131 return ntpTime; 132 } 133 134 139 public long getSeconds() 140 { 141 return (ntpTime >>> 32) & 0xffffffffL; 142 } 143 144 149 public long getFraction() 150 { 151 return ntpTime & 0xffffffffL; 152 } 153 154 159 public long getTime() 160 { 161 return getTime(ntpTime); 162 } 163 164 169 public Date getDate() 170 { 171 long time = getTime(ntpTime); 172 return new Date (time); 173 } 174 175 188 public static long getTime(long ntpTimeValue) 189 { 190 long seconds = (ntpTimeValue >>> 32) & 0xffffffffL; long fraction = ntpTimeValue & 0xffffffffL; 193 fraction = Math.round(1000D * fraction / 0x100000000L); 195 196 205 long msb = seconds & 0x80000000L; 206 if (msb == 0) { 207 return msb0baseTime + (seconds * 1000) + fraction; 209 } else { 210 return msb1baseTime + (seconds * 1000) + fraction; 212 } 213 } 214 215 225 public static TimeStamp getNtpTime(long date) 226 { 227 return new TimeStamp(toNtpTime(date)); 228 } 229 230 237 public static TimeStamp getCurrentTime() 238 { 239 return getNtpTime(System.currentTimeMillis()); 240 } 241 242 249 protected static long decodeNtpHexString(String s) 250 throws NumberFormatException 251 { 252 if (s == null) { 253 throw new NumberFormatException ("null"); 254 } 255 int ind = s.indexOf('.'); 256 if (ind == -1) { 257 if (s.length() == 0) return 0; 258 return Long.parseLong(s, 16) << 32; } 260 261 return Long.parseLong(s.substring(0, ind), 16) << 32 | 262 Long.parseLong(s.substring(ind + 1), 16); 263 } 264 265 273 public static TimeStamp parseNtpString(String s) 274 throws NumberFormatException 275 { 276 return new TimeStamp(decodeNtpHexString(s)); 277 } 278 279 285 protected static long toNtpTime(long t) 286 { 287 boolean useBase1 = t < msb0baseTime; long baseTime; 289 if (useBase1) { 290 baseTime = t - msb1baseTime; } else { 292 baseTime = t - msb0baseTime; 294 } 295 296 long seconds = baseTime / 1000; 297 long fraction = ((baseTime % 1000) * 0x100000000L) / 1000; 298 299 if (useBase1) { 300 seconds |= 0x80000000L; } 302 303 long time = seconds << 32 | fraction; 304 return time; 305 } 306 307 318 public int hashCode() 319 { 320 return (int) (ntpTime ^ (ntpTime >>> 32)); 321 } 322 323 333 public boolean equals(Object obj) 334 { 335 if (obj instanceof TimeStamp) { 336 return ntpTime == ((TimeStamp) obj).ntpValue(); 337 } 338 return false; 339 } 340 341 350 public String toString() 351 { 352 return toString(ntpTime); 353 } 354 355 361 private static void appendHexString(StringBuffer buf, long l) 362 { 363 String s = Long.toHexString(l); 364 for (int i = s.length(); i < 8; i++) 365 buf.append('0'); 366 buf.append(s); 367 } 368 369 378 public static String toString(long ntpTime) 379 { 380 StringBuffer buf = new StringBuffer (); 381 appendHexString(buf, (ntpTime >>> 32) & 0xffffffffL); 383 384 buf.append('.'); 386 appendHexString(buf, ntpTime & 0xffffffffL); 387 388 return buf.toString(); 389 } 390 391 400 public String toDateString() 401 { 402 DateFormat formatter = null; 403 if (simpleFormatter != null) { 404 formatter = (DateFormat ) simpleFormatter.get(); 405 } 406 if (formatter == null) { 407 formatter = new SimpleDateFormat (NTP_DATE_FORMAT, Locale.US); 409 formatter.setTimeZone(TimeZone.getDefault()); 410 simpleFormatter = new SoftReference (formatter); 411 } 412 Date ntpDate = getDate(); 413 synchronized (formatter) { 414 return formatter.format(ntpDate); 415 } 416 } 417 418 427 public String toUTCString() 428 { 429 DateFormat formatter = null; 430 if (utcFormatter != null) 431 formatter = (DateFormat ) utcFormatter.get(); 432 if (formatter == null) { 433 formatter = new SimpleDateFormat (NTP_DATE_FORMAT + " 'UTC'", 435 Locale.US); 436 formatter.setTimeZone(TimeZone.getTimeZone("UTC")); 437 utcFormatter = new SoftReference (formatter); 438 } 439 Date ntpDate = getDate(); 440 synchronized (formatter) { 441 return formatter.format(ntpDate); 442 } 443 } 444 445 456 public int compareTo(TimeStamp anotherTimeStamp) 457 { 458 long thisVal = this.ntpTime; 459 long anotherVal = anotherTimeStamp.ntpTime; 460 return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0 : 1)); 461 } 462 463 480 public int compareTo(Object o) 481 { 482 return compareTo((TimeStamp) o); 483 } 484 485 } 486 | Popular Tags |