slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 1 | /* 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 Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 4 | package org.onosproject.xran.asn1lib.ber.types; |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 5 | |
Dimitrios Mavrommatis | 96b255a | 2017-12-06 13:09:25 -0800 | [diff] [blame] | 6 | import org.onosproject.xran.asn1lib.ber.BerByteArrayOutputStream; |
| 7 | import org.onosproject.xran.asn1lib.ber.BerTag; |
| 8 | import org.onosproject.xran.asn1lib.ber.types.string.BerVisibleString; |
slowr | 60d4d10 | 2017-08-16 18:33:58 -0700 | [diff] [blame] | 9 | |
| 10 | import java.io.IOException; |
| 11 | import java.io.InputStream; |
| 12 | import java.text.ParseException; |
| 13 | import java.util.Calendar; |
| 14 | import java.util.Date; |
| 15 | import java.util.TimeZone; |
| 16 | import java.util.regex.Matcher; |
| 17 | import java.util.regex.Pattern; |
| 18 | |
| 19 | public class BerGeneralizedTime extends BerVisibleString { |
| 20 | |
| 21 | public final static BerTag tag = new BerTag(BerTag.UNIVERSAL_CLASS, BerTag.PRIMITIVE, BerTag.GENERALIZED_TIME_TAG); |
| 22 | private static final long serialVersionUID = 1L; |
| 23 | /* |
| 24 | * Generalized time is one of the following (ITU-T X.680 08/2015): YYYYMMDDHH[MM[SS]][.fff] LocalTime |
| 25 | * YYYYMMDDHH[MM[SS]][.fff]Z UTC YYYYMMDDHH[MM[SS]][.fff]+-HH[MM] local time with time zone |
| 26 | * |
| 27 | * Regexp: ^ (?<year>\\d{4}) YYYY (?<month>\\d{2}) MM (?<day>\\d{2}) DD (?<hour>\\d{2}) HH ( [MM[SS]] |
| 28 | * (?<minute>\\d{2}) MM (?<second>\\d{2})? [SS] )? ([.,](?<frac>\\d+))? [.fff] (or [,fff]) (?<timezone> "" or "Z" or |
| 29 | * "+-HH[MM]" Z | ( "+-HH[MM]" [+-] "+-" \\d{2}(?<tzmin>\\d{2})? HH[MM] ) )? $ |
| 30 | */ |
| 31 | private final static String GENERALIZED_TIME_PATTERN = "^(?<year>\\d{4})(?<month>\\d{2})(?<day>\\d{2})(?<hour>\\d{2})((?<minute>\\d{2})(?<second>\\d{2})?)?([.,](?<frac>\\d+))?(?<timezone>Z|([+-]\\d{2}(?<tzmin>\\d{2})?))?$"; |
| 32 | private final static Pattern generalizedTimePattern = Pattern.compile(GENERALIZED_TIME_PATTERN); |
| 33 | |
| 34 | public BerGeneralizedTime() { |
| 35 | } |
| 36 | |
| 37 | public BerGeneralizedTime(byte[] value) { |
| 38 | super(value); |
| 39 | } |
| 40 | |
| 41 | public BerGeneralizedTime(String valueAsString) { |
| 42 | super(valueAsString); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public int encode(BerByteArrayOutputStream os, boolean withTag) throws IOException { |
| 47 | |
| 48 | int codeLength = super.encode(os, false); |
| 49 | |
| 50 | if (withTag) { |
| 51 | codeLength += tag.encode(os); |
| 52 | } |
| 53 | |
| 54 | return codeLength; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public int decode(InputStream is, boolean withTag) throws IOException { |
| 59 | |
| 60 | int codeLength = 0; |
| 61 | |
| 62 | if (withTag) { |
| 63 | codeLength += tag.decodeAndCheck(is); |
| 64 | } |
| 65 | |
| 66 | codeLength += super.decode(is, false); |
| 67 | |
| 68 | return codeLength; |
| 69 | } |
| 70 | |
| 71 | Calendar asCalendar() throws ParseException { |
| 72 | |
| 73 | Matcher matcher = generalizedTimePattern.matcher(toString()); |
| 74 | |
| 75 | if (!matcher.find()) { |
| 76 | throw new ParseException("", 0); |
| 77 | } |
| 78 | |
| 79 | String mg, mgf; |
| 80 | int year = Integer.valueOf(matcher.group("year")); |
| 81 | int month = Integer.valueOf(matcher.group("month")); |
| 82 | month -= 1; // java.util.Calendar's month goes from 0 to 11 |
| 83 | int day = Integer.valueOf(matcher.group("day")); |
| 84 | int hour = Integer.valueOf(matcher.group("hour")); |
| 85 | |
| 86 | mg = matcher.group("minute"); |
| 87 | mgf = matcher.group("frac"); |
| 88 | int minute = 0, second = 0, millisec = 0; |
| 89 | double frac = mgf == null ? 0 : Double.valueOf("0." + mgf); |
| 90 | if (mg == null) { |
| 91 | // Missing minutes and seconds |
| 92 | if (mgf != null) { |
| 93 | // frac is a fraction of a hour |
| 94 | millisec = (int) Math.round(1000 * 60 * 60 * frac); |
| 95 | } |
| 96 | } else { |
| 97 | minute = Integer.valueOf(mg); |
| 98 | mg = matcher.group("second"); |
| 99 | if (mg == null) { |
| 100 | // Missing seconds |
| 101 | if (mgf != null) { |
| 102 | // frac is a fraction of a minute |
| 103 | millisec = (int) Math.round(1000 * 60 * frac); |
| 104 | } |
| 105 | } else { |
| 106 | second = Integer.valueOf(mg); |
| 107 | if (mgf != null) { |
| 108 | // frac is a fraction of a second |
| 109 | millisec = (int) Math.round(1000 * frac); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | mg = matcher.group("timezone"); |
| 115 | String mgt = matcher.group("tzmin"); |
| 116 | String timeZoneStr = mg == null ? TimeZone.getDefault().getID() |
| 117 | : (mg.equals("Z") ? "UTC" : (mgt == null ? "GMT" + mg + "00" : "GMT" + mg)); |
| 118 | TimeZone timeZone = TimeZone.getTimeZone(timeZoneStr); |
| 119 | |
| 120 | Calendar calendar = Calendar.getInstance(); |
| 121 | calendar.setLenient(true); // accept millisec greater than 999 |
| 122 | calendar.set(year, month, day, hour, minute, second); |
| 123 | calendar.set(Calendar.MILLISECOND, millisec); |
| 124 | calendar.setTimeZone(timeZone); |
| 125 | |
| 126 | return calendar; |
| 127 | } |
| 128 | |
| 129 | Date asDate() throws ParseException { |
| 130 | return asCalendar().getTime(); |
| 131 | } |
| 132 | |
| 133 | } |