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 */ 020package org.apache.directory.shared.kerberos; 021 022 023import java.text.ParseException; 024 025import org.apache.directory.api.util.GeneralizedTime; 026 027 028/** 029 * An specialization of the ASN.1 GeneralTime. The Kerberos time contains date and 030 * time up to the seconds, but with no fractional seconds. It's also always 031 * expressed as UTC timeZone, thus the 'Z' at the end of its string representation. 032 * 033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 034 */ 035public class KerberosTime implements Comparable<KerberosTime>, java.io.Serializable 036{ 037 /** Serial version id */ 038 private static final long serialVersionUID = -7541256140193748103L; 039 040 /** Constant for the {@link KerberosTime} "infinity." */ 041 public static final KerberosTime INFINITY = new KerberosTime( Long.MAX_VALUE ); 042 043 /** The number of milliseconds in a minute. */ 044 public static final int MINUTE = 60000; 045 046 /** The number of milliseconds in a day. */ 047 public static final int DAY = MINUTE * 1440; 048 049 /** The number of milliseconds in a week. */ 050 public static final int WEEK = MINUTE * 10080; 051 052 /** Kerberos generalized time. */ 053 private GeneralizedTime generalizedTime; 054 055 056 /** 057 * Creates a new instance of a KerberosTime object 058 * 059 * @param date the KerberosTime to store 060 */ 061 public KerberosTime( String date ) 062 { 063 try 064 { 065 generalizedTime = new GeneralizedTime( date ); 066 } 067 catch ( ParseException pe ) 068 { 069 throw new IllegalArgumentException( "Bad time : " + date ); 070 } 071 } 072 073 074 /** 075 * Creates a new instance of a KerberosTime object 076 */ 077 public KerberosTime( long date ) 078 { 079 generalizedTime = new GeneralizedTime( date ); 080 } 081 082 083 /** 084 * Returns the {@link KerberosTime} for a given zulu time. 085 * 086 * @param zuluTime 087 * @return The {@link KerberosTime}. 088 * @throws ParseException 089 */ 090 public static KerberosTime getTime( String zuluTime ) throws ParseException 091 { 092 return new KerberosTime( zuluTime ); 093 } 094 095 096 /** 097 * @return The stored date 098 */ 099 public String getDate() 100 { 101 return generalizedTime.toGeneralizedTime( 102 GeneralizedTime.Format.YEAR_MONTH_DAY_HOUR_MIN_SEC, 103 GeneralizedTime.FractionDelimiter.DOT, 0, 104 GeneralizedTime.TimeZoneFormat.Z 105 ); 106 } 107 108 109 @Override 110 public int hashCode() 111 { 112 // leave out fraction (milliseconds) 113 return ( int ) ( generalizedTime.getTime() / 1000L ); 114 } 115 116 117 @Override 118 public boolean equals( Object obj ) 119 { 120 if ( this == obj ) 121 { 122 return true; 123 } 124 125 if ( !( obj instanceof KerberosTime ) ) 126 { 127 return true; 128 } 129 130 KerberosTime other = ( KerberosTime ) obj; 131 132 // compare without fraction (milliseconds) 133 return generalizedTime.getTime() / 1000L == other.generalizedTime.getTime() / 1000L; 134 } 135 136 137 /** 138 * compares current kerberos time with the given kerberos time 139 * @param that the kerberos time against which the current kerberos time is compared 140 * @return 0 if both times are equal,<br> 141 * -1 if current time is less than the given time and<br> 142 * 1 if the given time is greater than the current time 143 */ 144 public int compareTo( KerberosTime that ) 145 { 146 return generalizedTime.compareTo( that.generalizedTime ); 147 } 148 149 150 /** 151 * {@inheritDoc} 152 */ 153 public String toString() 154 { 155 return getDate(); 156 } 157}