1    package org.bouncycastle.asn1;
2    
3    import java.io.*;
4    import java.util.*;
5    import java.io.*;
6    import java.text.*;
7    
8    
11   public class DERGeneralizedTime
12       extends DERObject
13   {
14       String      time;
15   
16       
21       public static DERGeneralizedTime getInstance(
22           Object  obj)
23       {
24           if (obj == null || obj instanceof DERGeneralizedTime)
25           {
26               return (DERGeneralizedTime)obj;
27           }
28   
29           if (obj instanceof ASN1OctetString)
30           {
31               return new DERGeneralizedTime(((ASN1OctetString)obj).getOctets());
32           }
33   
34           throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
35       }
36   
37       
46       public static DERGeneralizedTime getInstance(
47           ASN1TaggedObject obj,
48           boolean          explicit)
49       {
50           return getInstance(obj.getObject());
51       }
52       
53       
61       public DERGeneralizedTime(
62           String  time)
63       {
64           this.time = time;
65       }
66   
67       
70       public DERGeneralizedTime(
71           Date time)
72       {
73           SimpleDateFormat dateF = new SimpleDateFormat("yyyyMMddHHmmss'Z'");
74   
75           dateF.setTimeZone(new SimpleTimeZone(0,"Z"));
76   
77           this.time = dateF.format(time);
78       }
79   
80       DERGeneralizedTime(
81           byte[]  bytes)
82       {
83           
84           
85           
86           char[]  dateC = new char[bytes.length];
87   
88           for (int i = 0; i != dateC.length; i++)
89           {
90               dateC[i] = (char)(bytes[i] & 0xff);
91           }
92   
93           this.time = new String(dateC);
94       }
95   
96       
108      public String getTime()
109      {
110          
111          
112          
113          if (time.length() == 15)
114          {
115              return time.substring(0, 14) + "GMT+00:00";
116          }
117          else if (time.length() == 17)
118          {
119              return time.substring(0, 14) + "GMT" + time.substring(15, 17) + ":" + time.substring(17, 19);
120          }
121  
122          return time;
123      }
124  
125      private byte[] getOctets()
126      {
127          char[]  cs = time.toCharArray();
128          byte[]  bs = new byte[cs.length];
129  
130          for (int i = 0; i != cs.length; i++)
131          {
132              bs[i] = (byte)cs[i];
133          }
134  
135          return bs;
136      }
137  
138  
139      void encode(
140          DEROutputStream  out)
141          throws IOException
142      {
143          out.writeEncoded(GENERALIZED_TIME, this.getOctets());
144      }
145      
146      public boolean equals(
147          Object  o)
148      {
149          if ((o == null) || !(o instanceof DERGeneralizedTime))
150          {
151              return false;
152          }
153  
154          return time.equals(((DERGeneralizedTime)o).time);
155      }
156  }
157