blob: 48b2081f8b07a64597d1c58feef435ecfb2099a5 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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
17package android.webkit;
18
19import android.text.format.Time;
20
21import java.util.Calendar;
22import java.util.regex.Matcher;
23import java.util.regex.Pattern;
24
25
26class HttpDateTime {
27
28 /*
29 * Regular expression for parsing HTTP-date.
30 *
31 * Wdy, DD Mon YYYY HH:MM:SS GMT
32 * RFC 822, updated by RFC 1123
33 *
34 * Weekday, DD-Mon-YY HH:MM:SS GMT
35 * RFC 850, obsoleted by RFC 1036
36 *
37 * Wdy Mon DD HH:MM:SS YYYY
38 * ANSI C's asctime() format
39 *
40 * with following variations
41 *
42 * Wdy, DD-Mon-YYYY HH:MM:SS GMT
43 * Wdy, (SP)D Mon YYYY HH:MM:SS GMT
44 * Wdy,DD Mon YYYY HH:MM:SS GMT
45 * Wdy, DD-Mon-YY HH:MM:SS GMT
46 * Wdy, DD Mon YYYY HH:MM:SS -HHMM
47 * Wdy, DD Mon YYYY HH:MM:SS
48 * Wdy Mon (SP)D HH:MM:SS YYYY
49 * Wdy Mon DD HH:MM:SS YYYY GMT
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070050 *
51 * HH can be H if the first digit is zero.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 */
53 private static final String HTTP_DATE_RFC_REGEXP =
54 "([0-9]{1,2})[- ]([A-Za-z]{3,3})[- ]([0-9]{2,4})[ ]"
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070055 + "([0-9]{1,2}:[0-9][0-9]:[0-9][0-9])";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
57 private static final String HTTP_DATE_ANSIC_REGEXP =
58 "[ ]([A-Za-z]{3,3})[ ]+([0-9]{1,2})[ ]"
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070059 + "([0-9]{1,2}:[0-9][0-9]:[0-9][0-9])[ ]([0-9]{2,4})";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060
61 /**
62 * The compiled version of the HTTP-date regular expressions.
63 */
64 private static final Pattern HTTP_DATE_RFC_PATTERN =
65 Pattern.compile(HTTP_DATE_RFC_REGEXP);
66 private static final Pattern HTTP_DATE_ANSIC_PATTERN =
67 Pattern.compile(HTTP_DATE_ANSIC_REGEXP);
68
69 private static class TimeOfDay {
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070070 TimeOfDay(int h, int m, int s) {
71 this.hour = h;
72 this.minute = m;
73 this.second = s;
74 }
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 int hour;
77 int minute;
78 int second;
79 }
80
81 public static Long parse(String timeString)
82 throws IllegalArgumentException {
83
84 int date = 1;
85 int month = Calendar.JANUARY;
86 int year = 1970;
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -070087 TimeOfDay timeOfDay;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89 Matcher rfcMatcher = HTTP_DATE_RFC_PATTERN.matcher(timeString);
90 if (rfcMatcher.find()) {
91 date = getDate(rfcMatcher.group(1));
92 month = getMonth(rfcMatcher.group(2));
93 year = getYear(rfcMatcher.group(3));
94 timeOfDay = getTime(rfcMatcher.group(4));
95 } else {
96 Matcher ansicMatcher = HTTP_DATE_ANSIC_PATTERN.matcher(timeString);
97 if (ansicMatcher.find()) {
98 month = getMonth(ansicMatcher.group(1));
99 date = getDate(ansicMatcher.group(2));
100 timeOfDay = getTime(ansicMatcher.group(3));
101 year = getYear(ansicMatcher.group(4));
102 } else {
103 throw new IllegalArgumentException();
104 }
105 }
106
107 // FIXME: Y2038 BUG!
108 if (year >= 2038) {
109 year = 2038;
110 month = Calendar.JANUARY;
111 date = 1;
112 }
113
114 Time time = new Time(Time.TIMEZONE_UTC);
115 time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date,
116 month, year);
117 return time.toMillis(false /* use isDst */);
118 }
119
120 private static int getDate(String dateString) {
121 if (dateString.length() == 2) {
122 return (dateString.charAt(0) - '0') * 10
123 + (dateString.charAt(1) - '0');
124 } else {
125 return (dateString.charAt(0) - '0');
126 }
127 }
128
129 /*
130 * jan = 9 + 0 + 13 = 22
131 * feb = 5 + 4 + 1 = 10
132 * mar = 12 + 0 + 17 = 29
133 * apr = 0 + 15 + 17 = 32
134 * may = 12 + 0 + 24 = 36
135 * jun = 9 + 20 + 13 = 42
136 * jul = 9 + 20 + 11 = 40
137 * aug = 0 + 20 + 6 = 26
138 * sep = 18 + 4 + 15 = 37
139 * oct = 14 + 2 + 19 = 35
140 * nov = 13 + 14 + 21 = 48
141 * dec = 3 + 4 + 2 = 9
142 */
143 private static int getMonth(String monthString) {
144 int hash = Character.toLowerCase(monthString.charAt(0)) +
145 Character.toLowerCase(monthString.charAt(1)) +
146 Character.toLowerCase(monthString.charAt(2)) - 3 * 'a';
147 switch (hash) {
148 case 22:
149 return Calendar.JANUARY;
150 case 10:
151 return Calendar.FEBRUARY;
152 case 29:
153 return Calendar.MARCH;
154 case 32:
155 return Calendar.APRIL;
156 case 36:
157 return Calendar.MAY;
158 case 42:
159 return Calendar.JUNE;
160 case 40:
161 return Calendar.JULY;
162 case 26:
163 return Calendar.AUGUST;
164 case 37:
165 return Calendar.SEPTEMBER;
166 case 35:
167 return Calendar.OCTOBER;
168 case 48:
169 return Calendar.NOVEMBER;
170 case 9:
171 return Calendar.DECEMBER;
172 default:
173 throw new IllegalArgumentException();
174 }
175 }
176
177 private static int getYear(String yearString) {
178 if (yearString.length() == 2) {
179 int year = (yearString.charAt(0) - '0') * 10
180 + (yearString.charAt(1) - '0');
181 if (year >= 70) {
182 return year + 1900;
183 } else {
184 return year + 2000;
185 }
186 } else
187 return (yearString.charAt(0) - '0') * 1000
188 + (yearString.charAt(1) - '0') * 100
189 + (yearString.charAt(2) - '0') * 10
190 + (yearString.charAt(3) - '0');
191 }
192
193 private static TimeOfDay getTime(String timeString) {
The Android Open Source Project7b0b1ed2009-03-18 22:20:26 -0700194 // HH might be H
195 int i = 0;
196 int hour = timeString.charAt(i++) - '0';
197 if (timeString.charAt(i) != ':')
198 hour = hour * 10 + (timeString.charAt(i++) - '0');
199 // Skip ':'
200 i++;
201
202 int minute = (timeString.charAt(i++) - '0') * 10
203 + (timeString.charAt(i++) - '0');
204 // Skip ':'
205 i++;
206
207 int second = (timeString.charAt(i++) - '0') * 10
208 + (timeString.charAt(i++) - '0');
209
210 return new TimeOfDay(hour, minute, second);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212}