ASN1Time.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
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 Stepan M. Mishura
20* @version $Revision$
21*/
22
23package org.apache.harmony.security.asn1;
24
25import java.io.IOException;
26import java.util.Calendar;
27import java.util.GregorianCalendar;
28import java.util.TimeZone;
29
30
31/**
32 * Abstract class to represent ASN.1 time types
33 *
34 * @see <a href="http://asn1.elibel.tm.fr/en/standards/index.htm">ASN.1</a>
35 */
36
37public abstract class ASN1Time extends ASN1StringType {
38
39    /**
40     * TODO Put ctor description here
41     *
42     * @param tagNumber
43     */
44    public ASN1Time(int tagNumber) {
45        super(tagNumber);
46    }
47
48    public Object getDecodedObject(BerInputStream in) throws IOException {
49
50        // TODO optimize me:
51        // It makes sense use calendar instance instead of times array
52        GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone("GMT")); //$NON-NLS-1$
53
54        c.set(Calendar.YEAR, in.times[0]);
55        c.set(Calendar.MONTH, in.times[1]-1);
56        c.set(Calendar.DAY_OF_MONTH, in.times[2]);
57        c.set(Calendar.HOUR_OF_DAY, in.times[3]);
58        c.set(Calendar.MINUTE, in.times[4]);
59        c.set(Calendar.SECOND, in.times[5]);
60        c.set(Calendar.MILLISECOND, in.times[6]);
61
62        return c.getTime();
63    }
64}
65