1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.bluetooth.client.map.utils;
18 
19 import java.util.Calendar;
20 import java.util.Date;
21 import java.util.Locale;
22 import java.util.TimeZone;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 
26 public final class ObexTime {
27 
28     private Date mDate;
29 
ObexTime(String time)30     public ObexTime(String time) {
31         /*
32          * match OBEX time string: YYYYMMDDTHHMMSS with optional UTF offset
33          * +/-hhmm
34          */
35         Pattern p = Pattern
36                 .compile("(\\d{4})(\\d{2})(\\d{2})T(\\d{2})(\\d{2})(\\d{2})(([+-])(\\d{2})(\\d{2}))?");
37         Matcher m = p.matcher(time);
38 
39         if (m.matches()) {
40 
41             /*
42              * matched groups are numberes as follows: YYYY MM DD T HH MM SS +
43              * hh mm ^^^^ ^^ ^^ ^^ ^^ ^^ ^ ^^ ^^ 1 2 3 4 5 6 8 9 10 all groups
44              * are guaranteed to be numeric so conversion will always succeed
45              * (except group 8 which is either + or -)
46              */
47 
48             Calendar cal = Calendar.getInstance();
49             cal.set(Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)) - 1,
50                     Integer.parseInt(m.group(3)), Integer.parseInt(m.group(4)),
51                     Integer.parseInt(m.group(5)), Integer.parseInt(m.group(6)));
52 
53             /*
54              * if 7th group is matched then we have UTC offset information
55              * included
56              */
57             if (m.group(7) != null) {
58                 int ohh = Integer.parseInt(m.group(9));
59                 int omm = Integer.parseInt(m.group(10));
60 
61                 /* time zone offset is specified in miliseconds */
62                 int offset = (ohh * 60 + omm) * 60 * 1000;
63 
64                 if (m.group(8).equals("-")) {
65                     offset = -offset;
66                 }
67 
68                 TimeZone tz = TimeZone.getTimeZone("UTC");
69                 tz.setRawOffset(offset);
70 
71                 cal.setTimeZone(tz);
72             }
73 
74             mDate = cal.getTime();
75         }
76     }
77 
ObexTime(Date date)78     public ObexTime(Date date) {
79         mDate = date;
80     }
81 
getTime()82     public Date getTime() {
83         return mDate;
84     }
85 
86     @Override
toString()87     public String toString() {
88         if (mDate == null) {
89             return null;
90         }
91 
92         Calendar cal = Calendar.getInstance();
93         cal.setTime(mDate);
94 
95         /* note that months are numbered stating from 0 */
96         return String.format(Locale.US, "%04d%02d%02dT%02d%02d%02d",
97                 cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1,
98                 cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY),
99                 cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
100     }
101 }
102