1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18/**
19* @author Alexander Y. Kleymenov
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.x509;
24
25import org.apache.harmony.security.asn1.ASN1Choice;
26import org.apache.harmony.security.asn1.ASN1GeneralizedTime;
27import org.apache.harmony.security.asn1.ASN1Type;
28import org.apache.harmony.security.asn1.ASN1UTCTime;
29
30/**
31 * Class represents the work with the following X.509 structure:
32 * (as specified in RFC 3280 -
33 *  Internet X.509 Public Key Infrastructure.
34 *  Certificate and Certificate Revocation List (CRL) Profile.
35 *  http://www.ietf.org/rfc/rfc3280.txt):
36 *
37 * <pre>
38 * Time ::= CHOICE {
39 *       utcTime        UTCTime,
40 *       generalTime    GeneralizedTime
41 * }
42 * </pre>
43 */
44public class Time {
45
46    private static final long JAN_01_2050 = 2524608000000L;
47
48    public static final ASN1Choice ASN1 = new ASN1Choice(new ASN1Type[] {
49            ASN1GeneralizedTime.getInstance(), ASN1UTCTime.getInstance() }) {
50
51        public int getIndex(java.lang.Object object) {
52            // choose encoding method (see RFC 3280 p. 22)
53            if (((java.util.Date) object).getTime() < JAN_01_2050) {
54                return 1; // it is before 2050, so encode as UTCTime
55            } else {
56                return 0; // it is after 2050, encode as GeneralizedTime
57            }
58        }
59
60        public Object getObjectToEncode(Object object) {
61            return object;
62        }
63    };
64}
65
66