KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > coach > tracing > service > ntp > TimeStamp


1 /***************************************************************************/
2 /* COACH: Component Based Open Source Architecture for */
3 /* Distributed Telecom Applications */
4 /* See: http://www.objectweb.org/ */
5 /* */
6 /* Copyright (C) 2003 Lucent Technologies Nederland BV */
7 /* Bell Labs Advanced Technologies - EMEA */
8 /* */
9 /* Initial developer(s): Harold Batteram */
10 /* */
11 /* This library is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public */
13 /* License as published by the Free Software Foundation; either */
14 /* version 2.1 of the License, or (at your option) any later version. */
15 /* */
16 /* This library is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
19 /* Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public */
22 /* License along with this library; if not, write to the Free Software */
23 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
24 /***************************************************************************/
25 package org.coach.tracing.service.ntp;
26
27 import java.util.*;
28
29 /**
30  * This class encapsulates the notion of a timestamp as in rfc2030.
31  * Logically it is the number of seconds since the beginning of the
32  * centure (in UTC time).
33  * It is represented as an 8 byte array, the first four bytes
34  * representing seconds and the next 4 bytes representing second
35  * fractions.
36 */

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     /**
53    * The timestamp corresponding to the beginning of the century.
54    */

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 JavaDoc 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