blob: e292aa5316ffed8193d5db55773f81d5f5a85e02 [file] [log] [blame]
slowr60d4d102017-08-16 18:33:58 -07001/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08004package org.onosproject.xran.asn1lib.ber.types;
slowr60d4d102017-08-16 18:33:58 -07005
Dimitrios Mavrommatis96b255a2017-12-06 13:09:25 -08006import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream;
7import org.onosproject.xran.asn1lib.ber.BerTag;
8import org.onosproject.xran.asn1lib.ber.types.string.BerVisibleString;
slowr60d4d102017-08-16 18:33:58 -07009
10import java.io.IOException;
11import java.io.InputStream;
12import java.text.ParseException;
13import java.util.Calendar;
14import java.util.Date;
15import java.util.TimeZone;
16import java.util.regex.Matcher;
17import java.util.regex.Pattern;
18
19public class BerUtcTime extends BerVisibleString {
20
21 public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.UTC_TIME_TAG);
22 private static final long serialVersionUID = 1L;
23 /*
24 * UTC time is one of the following (ITU-T X.680 08/2015): YYMMDDhhmm[ss]Z YYMMDDhhmm[ss](+|-)hhmm Regexp: ^
25 * (?<year>\\d{2}) YY (?<month>\\d{2}) MM (?<day>\\d{2}) DD (?<hour>\\d{2}) hh (?<minute>\\d{2}) mm
26 * (?<second>\\d{2})? ss (?<timezone> Z | Z or (+|-)hhmm ( [+-]\\d{4} (+|-)hhmm ) ) $
27 */
28 private final static String UTC_TIME_PATTERN = "^(?<year>\\d{2})(?<month>\\d{2})(?<day>\\d{2})(?<hour>\\d{2})(?<minute>\\d{2})(?<second>\\d{2})?(?<timezone>Z|([+-]\\d{4}))$";
29 private final static Pattern utcTimePattern = Pattern.compile(UTC_TIME_PATTERN);
30
31 public BerUtcTime() {
32 }
33
34 public BerUtcTime(byte[] value) {
35 this.value = value;
36 }
37
38 public BerUtcTime(String valueAsString) {
39 super(valueAsString);
40 }
41
42 @Override
43 public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException {
44
45 int codeLength = super.encode(os, false);
46
47 if (withTag) {
48 codeLength += tag.encode(os);
49 }
50
51 return codeLength;
52 }
53
54 @Override
55 public int decode(InputStream is, boolean withTag) throws IOException {
56
57 int codeLength = 0;
58
59 if (withTag) {
60 codeLength += tag.decodeAndCheck(is);
61 }
62
63 codeLength += super.decode(is, false);
64
65 return codeLength;
66 }
67
68 @SuppressWarnings("WeakerAccess")
69 Calendar asCalendar() throws ParseException {
70
71 Matcher matcher = utcTimePattern.matcher(toString());
72
73 if (!matcher.find())
74 throw new ParseException("", 0);
75
76 String mg;
77 int year = Integer.valueOf(matcher.group("year"));
78 int month = Integer.valueOf(matcher.group("month"));
79 month -= 1; // java.util.Calendar's month goes from 0 to 11
80 int day = Integer.valueOf(matcher.group("day"));
81 int hour = Integer.valueOf(matcher.group("hour"));
82 int minute = Integer.valueOf(matcher.group("minute"));
83 mg = matcher.group("second");
84 int second = mg == null ? 0 : Integer.valueOf(mg);
85
86 mg = matcher.group("timezone");
87 String timeZoneStr = mg.equals("Z") ? "UTC" : "GMT" + mg;
88 TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr);
89
90 Calendar calendar = Calendar.getInstance(timeZone);
91
92 // Add 2000 to the year
93 int century = (calendar.get(Calendar.YEAR) / 100) * 100;
94 year += century;
95
96 // noinspection MagicConstant
97 calendar.set(year, month, day, hour, minute, second);
98 calendar.set(Calendar.MILLISECOND, 0);
99
100 return calendar;
101 }
102
103 Date asDate() throws ParseException {
104 return asCalendar().getTime();
105 }
106
107}