001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.server.kerberos.shared.messages.value; 021 022 023 import java.text.ParseException; 024 import java.text.SimpleDateFormat; 025 import java.util.Date; 026 import java.util.TimeZone; 027 028 029 /** 030 * Implementation of the time object for Kerberos. 031 * 032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 * @version $Rev: 557427 $, $Date: 2007-07-19 01:47:31 +0200 (Thu, 19 Jul 2007) $ 034 */ 035 public class KerberosTime implements Comparable<KerberosTime> 036 { 037 /** The number of milliseconds in a minute. */ 038 public static final int MINUTE = 60000; 039 040 /** The number of milliseconds in a day. */ 041 public static final int DAY = MINUTE * 1440; 042 043 /** The number of milliseconds in a week. */ 044 public static final int WEEK = MINUTE * 10080; 045 046 /** Constant for the {@link KerberosTime} "infinity." */ 047 public static final KerberosTime INFINITY = new KerberosTime( Long.MAX_VALUE ); 048 049 private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" ); 050 private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyyMMddHHmmss'Z'" ); 051 052 static 053 { 054 dateFormat.setTimeZone( UTC_TIME_ZONE ); 055 } 056 057 private long kerberosTime; 058 059 060 /** 061 * Creates a new instance of KerberosTime. 062 */ 063 public KerberosTime() 064 { 065 kerberosTime = System.currentTimeMillis(); 066 } 067 068 069 /** 070 * Creates a new instance of KerberosTime. 071 * 072 * @param time 073 */ 074 public KerberosTime( long time ) 075 { 076 kerberosTime = time; 077 } 078 079 080 /** 081 * Creates a new instance of KerberosTime. 082 * 083 * @param time 084 */ 085 public KerberosTime( Date time ) 086 { 087 kerberosTime = time.getTime(); 088 } 089 090 091 /** 092 * Returns the {@link KerberosTime} for a given zulu time. 093 * 094 * @param zuluTime 095 * @return The {@link KerberosTime}. 096 * @throws ParseException 097 */ 098 public static KerberosTime getTime( String zuluTime ) throws ParseException 099 { 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 }