1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20 package org.apache.directory.server.kerberos.shared.messages.value;
21
22
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.TimeZone;
27
28
29 /**
30 * Implementation of the time object for Kerberos.
31 *
32 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33 * @version $Rev: 557427 $, $Date: 2007-07-19 01:47:31 +0200 (Do, 19 Jul 2007) $
34 */
35 public class KerberosTime implements Comparable<KerberosTime>
36 {
37 /** The number of milliseconds in a minute. */
38 public static final int MINUTE = 60000;
39
40 /** The number of milliseconds in a day. */
41 public static final int DAY = MINUTE * 1440;
42
43 /** The number of milliseconds in a week. */
44 public static final int WEEK = MINUTE * 10080;
45
46 /** Constant for the {@link KerberosTime} "infinity." */
47 public static final KerberosTime INFINITY = new KerberosTime( Long.MAX_VALUE );
48
49 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
50 private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss'Z'" );
51
52 static
53 {
54 dateFormat.setTimeZone( UTC_TIME_ZONE );
55 }
56
57 private long kerberosTime;
58
59
60 /**
61 * Creates a new instance of KerberosTime.
62 */
63 public KerberosTime()
64 {
65 kerberosTime = System.currentTimeMillis();
66 }
67
68
69 /**
70 * Creates a new instance of KerberosTime.
71 *
72 * @param time
73 */
74 public KerberosTime( long time )
75 {
76 kerberosTime = time;
77 }
78
79
80 /**
81 * Creates a new instance of KerberosTime.
82 *
83 * @param time
84 */
85 public KerberosTime( Date time )
86 {
87 kerberosTime = time.getTime();
88 }
89
90
91 /**
92 * Returns the {@link KerberosTime} for a given zulu time.
93 *
94 * @param zuluTime
95 * @return The {@link KerberosTime}.
96 * @throws ParseException
97 */
98 public static KerberosTime getTime( String zuluTime ) throws ParseException
99 {
100 Date date = null;
101 synchronized ( dateFormat )
102 {
103 date = dateFormat.parse( zuluTime );
104 }
105 return new KerberosTime( date );
106 }
107
108
109 public int compareTo( KerberosTime that )
110 {
111 final int BEFORE = -1;
112 final int EQUAL = 0;
113 final int AFTER = 1;
114
115 // this optimization is usually worthwhile, and can always be added
116 if ( this == that )
117 {
118 return EQUAL;
119 }
120
121 // primitive numbers follow this form
122 if ( this.kerberosTime < that.kerberosTime )
123 {
124 return BEFORE;
125 }
126
127 if ( this.kerberosTime > that.kerberosTime )
128 {
129 return AFTER;
130 }
131
132 return EQUAL;
133 }
134
135
136 /**
137 * Returns the {@link KerberosTime} as a long.
138 *
139 * @return The {@link KerberosTime} as a long.
140 */
141 public long getTime()
142 {
143 return kerberosTime;
144 }
145
146
147 /**
148 * Returns the {@link KerberosTime} as a {@link Date}.
149 *
150 * @return The {@link KerberosTime} as a {@link Date}.
151 */
152 public Date toDate()
153 {
154 return new Date( kerberosTime );
155 }
156
157
158 /**
159 * Returns whether this {@link KerberosTime} is within the given clockskew.
160 *
161 * @param clockSkew
162 * @return true if this {@link KerberosTime} is within the given clockskew.
163 */
164 public boolean isInClockSkew( long clockSkew )
165 {
166 return Math.abs( kerberosTime - System.currentTimeMillis() ) < clockSkew;
167 }
168
169
170 /**
171 * Returns whether this {@link KerberosTime} is greater than a given {@link KerberosTime}.
172 *
173 * @param time
174 * @return true if this {@link KerberosTime} is greater than a given {@link KerberosTime}.
175 */
176 public boolean greaterThan( KerberosTime time )
177 {
178 return kerberosTime > time.kerberosTime;
179 }
180
181
182 /**
183 * Returns whether this {@link KerberosTime} is less than a given {@link KerberosTime}.
184 *
185 * @param time
186 * @return true if this {@link KerberosTime} is less than a given {@link KerberosTime}.
187 */
188 public boolean lessThan( KerberosTime time )
189 {
190 return kerberosTime < time.kerberosTime;
191 }
192
193
194 /**
195 * Returns whether this {@link KerberosTime} is equal to another {@link KerberosTime}.
196 *
197 * @param time
198 * @return true if the two {@link KerberosTime}s are equal.
199 */
200 public boolean equals( KerberosTime time )
201 {
202 return kerberosTime == time.kerberosTime;
203 }
204
205
206 /**
207 * Returns whether this {@link KerberosTime} is zero.
208 *
209 * @return true if this {@link KerberosTime} is zero.
210 */
211 public boolean isZero()
212 {
213 return kerberosTime == 0;
214 }
215
216
217 public String toString()
218 {
219 Date kerberosDate = new Date( kerberosTime );
220
221 synchronized ( dateFormat )
222 {
223 return dateFormat.format( kerberosDate );
224 }
225 }
226 }