aboutsummaryrefslogtreecommitdiff
path: root/src/main/model/asn1/GeneralizedTime.java
blob: c97a51b25005667ea8e38f5440f56854a56da59a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package model.asn1;

import model.asn1.exceptions.ParseException;
import model.asn1.parsing.BytesReader;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;

/**
 * Represents the ASN.1 GeneralizedTime type. It encodes the time in "YYYYMMDDhhmm[ss]Z" string format, in UTC.
 */
public class GeneralizedTime extends ASN1Time {
    /**
     * The X.680 universal class tag assignment.
     */
    public static final Tag TAG = new Tag(TagClass.UNIVERSAL, false, 0x18);

    /**
     * Rather stupid impl ...
     */
    private static final DateTimeFormatter FORMATTER_NO_SECS = new DateTimeFormatterBuilder()
            .appendValue(ChronoField.YEAR, 4)
            .appendValue(ChronoField.MONTH_OF_YEAR, 2)
            .appendValue(ChronoField.DAY_OF_MONTH, 2)
            .appendValue(ChronoField.HOUR_OF_DAY, 2)
            .appendValue(ChronoField.MINUTE_OF_HOUR, 2)
            .appendLiteral('Z')
            .toFormatter()
            .withZone(ZoneId.of("UTC"));

    private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
            .appendValue(ChronoField.YEAR, 4)
            .appendValue(ChronoField.MONTH_OF_YEAR, 2)
            .appendValue(ChronoField.DAY_OF_MONTH, 2)
            .appendValue(ChronoField.HOUR_OF_DAY, 2)
            .appendValue(ChronoField.MINUTE_OF_HOUR, 2)
            .optionalStart()
            .appendValue(ChronoField.SECOND_OF_MINUTE, 2)
            .optionalEnd()
            .appendLiteral('Z')
            .toFormatter()
            .withZone(ZoneId.of("UTC"));

    /**
     * EFFECT: Construct the UTCTime with the given tag, parentTag, and timestamp. For tag and parentTag,
     * consult {@link ASN1Object}.
     * REQUIRES: timestamp must be in UTC.
     */
    public GeneralizedTime(Tag tag, Tag parentTag, ZonedDateTime timestamp) {
        super(tag, parentTag, timestamp);
    }

    /**
     * EFFECT: Parse the given DER input. Time will be assumed to be in UTC.
     * Throws {@link ParseException}:
     * - The time is not in the string format specified in class specification
     * - Other invalid input is found. See {@link ASN1Object} for more details on parsing
     */
    public GeneralizedTime(BytesReader encoded, boolean hasParentTag) throws ParseException {
        super(encoded, hasParentTag);
    }

    /**
     * EFFECT: Parse the string into time, in the format specified in class specification.
     * Throws {@link ParseException} if the input is malformed.
     */
    @Override
    public ZonedDateTime toDate(String str) throws ParseException {
        try {
            return ZonedDateTime.parse(str, FORMATTER);
        } catch (DateTimeParseException e) {
            throw new ParseException(e.getMessage());
        }
    }

    /**
     * EFFECT: Convert the time into format "YYYYMMDDhhmm[ss]Z".
     */
    @Override
    public String toString() {
        if (getTimestamp().getSecond() == 0) {
            return getTimestamp().format(FORMATTER_NO_SECS);
        }
        return getTimestamp().format(FORMATTER);
    }
}