1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.server.ntp.messages;
22
23
24 import java.nio.ByteBuffer;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.Locale;
28 import java.util.TimeZone;
29
30
31
32
33
34
35
36
37
38
39 public class NtpTimeStamp
40 {
41
42
43
44
45 private static final long NTP_EPOCH_DIFFERENCE = -2208988800000L;
46
47 private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS z", Locale.ROOT );
48
49 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
50
51 static
52 {
53 DATE_FORMAT.setTimeZone( UTC_TIME_ZONE );
54 }
55
56 private long seconds = 0;
57 private long fraction = 0;
58
59
60
61
62
63 public NtpTimeStamp()
64 {
65 this( new Date() );
66 }
67
68
69
70
71
72
73
74 public NtpTimeStamp( Date date )
75 {
76 long msSinceStartOfNtpEpoch = date.getTime() - NTP_EPOCH_DIFFERENCE;
77
78 seconds = msSinceStartOfNtpEpoch / 1000;
79 fraction = ( ( msSinceStartOfNtpEpoch % 1000 ) * 0x100000000L ) / 1000;
80 }
81
82
83
84
85
86
87
88 public NtpTimeStamp( ByteBuffer data )
89 {
90 for ( int ii = 0; ii < 4; ii++ )
91 {
92 seconds = 256 * seconds + makePositive( data.get() );
93 }
94
95 for ( int ii = 4; ii < 8; ii++ )
96 {
97 fraction = 256 * fraction + makePositive( data.get() );
98 }
99 }
100
101
102
103
104
105
106
107 public void writeTo( ByteBuffer buffer )
108 {
109 byte[] bytes = new byte[8];
110
111 long temp = seconds;
112 for ( int ii = 3; ii >= 0; ii-- )
113 {
114 bytes[ii] = ( byte ) ( temp % 256 );
115 temp = temp / 256;
116 }
117
118 temp = fraction;
119 for ( int ii = 7; ii >= 4; ii-- )
120 {
121 bytes[ii] = ( byte ) ( temp % 256 );
122 temp = temp / 256;
123 }
124
125 buffer.put( bytes );
126 }
127
128
129 public String toString()
130 {
131 long msSinceStartOfNtpEpoch = seconds * 1000 + ( fraction * 1000 ) / 0x100000000L;
132 Date date = new Date( msSinceStartOfNtpEpoch + NTP_EPOCH_DIFFERENCE );
133
134 synchronized ( DATE_FORMAT )
135 {
136 return "org.apache.ntp.message.NtpTimeStamp[ date = " + DATE_FORMAT.format( date ) + " ]";
137 }
138 }
139
140
141
142
143
144 @Override
145 public int hashCode()
146 {
147 int hash = 37;
148 hash = hash * 17 + Long.valueOf( seconds ).hashCode();
149 hash = hash * 17 + Long.valueOf( fraction ).hashCode();
150
151 return hash;
152 }
153
154
155
156
157
158 @Override
159 public boolean equals( Object o )
160 {
161 if ( this == o )
162 {
163 return true;
164 }
165
166 if ( !( o instanceof NtpTimeStamp ) )
167 {
168 return false;
169 }
170
171 NtpTimeStamp/../../../../org/apache/directory/server/ntp/messages/NtpTimeStamp.html#NtpTimeStamp">NtpTimeStamp that = ( NtpTimeStamp ) o;
172 return ( this.seconds == that.seconds ) && ( this.fraction == that.fraction );
173 }
174
175
176 private int makePositive( byte b )
177 {
178 int byteAsInt = b;
179 return ( byteAsInt < 0 ) ? 256 + byteAsInt : byteAsInt;
180 }
181 }