1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.coach.tracing.service.ntp; 26 27 import java.util.*; 28 29 37 public class TimeStamp { 38 private long integerPart; 39 private long fractionalPart; 40 private byte[] data; 41 private Date date; 42 static private TimeZone UTC = new SimpleTimeZone(0, "UTC"); 43 static private Calendar c = new GregorianCalendar(1900, Calendar.JANUARY, 1, 0, 0, 0); 44 static private Date startOfCentury; 45 static 46 { 47 c.setTimeZone(UTC); 48 startOfCentury = c.getTime(); 49 } 50 51 private static final byte[] emptyArray = { 0, 0, 0, 0, 0, 0, 0, 0 }; 52 55 public static final TimeStamp zero = new TimeStamp(emptyArray); 56 private int mp(byte b) { 57 int bb = b; 58 return (bb < 0) ? 256 + bb : bb; 59 } 60 61 public TimeStamp() { 62 this(new Date()); 63 } 64 65 public TimeStamp(Date date) { 66 data = new byte[8]; 67 this.date = date; 68 long msSinceStartOfCentury = date.getTime() - startOfCentury.getTime(); 69 integerPart = msSinceStartOfCentury / 1000; 70 fractionalPart = ((msSinceStartOfCentury % 1000) * 0x100000000L) / 1000; 71 long temp = integerPart; 72 73 for (int i = 3; i >= 0; i--) { 74 data[i] = (byte)(temp % 256); 75 temp = temp / 256; 76 } 77 temp = fractionalPart; 78 79 for (int i = 7; i >= 4; i--) { 80 data[i] = (byte)(temp % 256); 81 temp = temp / 256; 82 } 83 } 84 85 public TimeStamp(byte[] data) { 86 this.data = data; 87 integerPart = 0; 88 int u; 89 90 for (int i = 0; i <= 3; i++) { 91 integerPart = 256 * integerPart + mp(data[i]); 92 } 93 fractionalPart = 0; 94 95 for (int i = 4; i <= 7; i++) { 96 fractionalPart = 256 * fractionalPart + mp(data[i]); 97 } 98 long msSinceStartOfCentury = integerPart * 1000 + (fractionalPart * 1000) / 0x100000000L; 99 date = new Date(msSinceStartOfCentury + startOfCentury.getTime()); 100 } 101 102 public boolean equals(TimeStamp ts) { 103 boolean value = true; 104 byte[] tsData = ts.getData(); 105 for (int i = 0; i <= 7; i++) { 106 107 if (data[i] != tsData[i]) { 108 value = false; 109 } 110 } 111 return value; 112 } 113 114 public String toString() { 115 return "" + date + " + " + fractionalPart + "/" + 0x100000000L; 116 } 117 118 public byte[] getData() { 119 return data; 120 } 121 122 public Date getTime() { 123 return date; 124 } 125 } 126 | Popular Tags |