blob: c6ec2d27cfee4ed3520915d262ccbfef44ad0991 [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
50 */
51 private static final String HTTP_DATE_RFC_REGEXP =
52 "([0-9]{1,2})[- ]([A-Za-z]{3,3})[- ]([0-9]{2,4})[ ]"
53 + "([0-9][0-9]:[0-9][0-9]:[0-9][0-9])";
54
55 private static final String HTTP_DATE_ANSIC_REGEXP =
56 "[ ]([A-Za-z]{3,3})[ ]+([0-9]{1,2})[ ]"
57 + "([0-9][0-9]:[0-9][0-9]:[0-9][0-9])[ ]([0-9]{2,4})";
58
59 /**
60 * The compiled version of the HTTP-date regular expressions.
61 */
62 private static final Pattern HTTP_DATE_RFC_PATTERN =
63 Pattern.compile(HTTP_DATE_RFC_REGEXP);
64 private static final Pattern HTTP_DATE_ANSIC_PATTERN =
65 Pattern.compile(HTTP_DATE_ANSIC_REGEXP);
66
67 private static class TimeOfDay {
68 int hour;
69 int minute;
70 int second;
71 }
72
73 public static Long parse(String timeString)
74 throws IllegalArgumentException {
75
76 int date = 1;
77 int month = Calendar.JANUARY;
78 int year = 1970;
79 TimeOfDay timeOfDay = new TimeOfDay();
80
81 Matcher rfcMatcher = HTTP_DATE_RFC_PATTERN.matcher(timeString);
82 if (rfcMatcher.find()) {
83 date = getDate(rfcMatcher.group(1));
84 month = getMonth(rfcMatcher.group(2));
85 year = getYear(rfcMatcher.group(3));
86 timeOfDay = getTime(rfcMatcher.group(4));
87 } else {
88 Matcher ansicMatcher = HTTP_DATE_ANSIC_PATTERN.matcher(timeString);
89 if (ansicMatcher.find()) {
90 month = getMonth(ansicMatcher.group(1));
91 date = getDate(ansicMatcher.group(2));
92 timeOfDay = getTime(ansicMatcher.group(3));
93 year = getYear(ansicMatcher.group(4));
94 } else {
95 throw new IllegalArgumentException();
96 }
97 }
98
99 // FIXME: Y2038 BUG!
100 if (year >= 2038) {
101 year = 2038;
102 month = Calendar.JANUARY;
103 date = 1;
104 }
105
106 Time time = new Time(Time.TIMEZONE_UTC);
107 time.set(timeOfDay.second, timeOfDay.minute, timeOfDay.hour, date,
108 month, year);
109 return time.toMillis(false /* use isDst */);
110 }
111
112 private static int getDate(String dateString) {
113 if (dateString.length() == 2) {
114 return (dateString.charAt(0) - '0') * 10
115 + (dateString.charAt(1) - '0');
116 } else {
117 return (dateString.charAt(0) - '0');
118 }
119 }
120
121 /*
122 * jan = 9 + 0 + 13 = 22
123 * feb = 5 + 4 + 1 = 10
124 * mar = 12 + 0 + 17 = 29
125 * apr = 0 + 15 + 17 = 32
126 * may = 12 + 0 + 24 = 36
127 * jun = 9 + 20 + 13 = 42
128 * jul = 9 + 20 + 11 = 40
129 * aug = 0 + 20 + 6 = 26
130 * sep = 18 + 4 + 15 = 37
131 * oct = 14 + 2 + 19 = 35
132 * nov = 13 + 14 + 21 = 48
133 * dec = 3 + 4 + 2 = 9
134 */
135 private static int getMonth(String monthString) {
136 int hash = Character.toLowerCase(monthString.charAt(0)) +
137 Character.toLowerCase(monthString.charAt(1)) +
138 Character.toLowerCase(monthString.charAt(2)) - 3 * 'a';
139 switch (hash) {
140 case 22:
141 return Calendar.JANUARY;
142 case 10:
143 return Calendar.FEBRUARY;
144 case 29:
145 return Calendar.MARCH;
146 case 32:
147 return Calendar.APRIL;
148 case 36:
149 return Calendar.MAY;
150 case 42:
151 return Calendar.JUNE;
152 case 40:
153 return Calendar.JULY;
154 case 26:
155 return Calendar.AUGUST;
156 case 37:
157 return Calendar.SEPTEMBER;
158 case 35:
159 return Calendar.OCTOBER;
160 case 48:
161 return Calendar.NOVEMBER;
162 case 9:
163 return Calendar.DECEMBER;
164 default:
165 throw new IllegalArgumentException();
166 }
167 }
168
169 private static int getYear(String yearString) {
170 if (yearString.length() == 2) {
171 int year = (yearString.charAt(0) - '0') * 10
172 + (yearString.charAt(1) - '0');
173 if (year >= 70) {
174 return year + 1900;
175 } else {
176 return year + 2000;
177 }
178 } else
179 return (yearString.charAt(0) - '0') * 1000
180 + (yearString.charAt(1) - '0') * 100
181 + (yearString.charAt(2) - '0') * 10
182 + (yearString.charAt(3) - '0');
183 }
184
185 private static TimeOfDay getTime(String timeString) {
186 TimeOfDay time = new TimeOfDay();
187 time.hour = (timeString.charAt(0) - '0') * 10
188 + (timeString.charAt(1) - '0');
189 time.minute = (timeString.charAt(3) - '0') * 10
190 + (timeString.charAt(4) - '0');
191 time.second = (timeString.charAt(6) - '0') * 10
192 + (timeString.charAt(7) - '0');
193 return time;
194 }
195}