blob: f7360f8c0231cc168a1c6cd64f3f1f355e9d97aa [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_DATEPARSER_INL_H_
6#define V8_DATEPARSER_INL_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "src/dateparser.h"
Steve Block3ce2e202009-11-05 08:53:23 +00009
Steve Blocka7e24c12009-10-30 11:49:00 +000010namespace v8 {
11namespace internal {
12
13template <typename Char>
Ben Murdoch8b112d22011-06-08 16:22:53 +010014bool DateParser::Parse(Vector<Char> str,
15 FixedArray* out,
16 UnicodeCache* unicode_cache) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017 DCHECK(out->length() >= OUTPUT_SIZE);
Ben Murdoch8b112d22011-06-08 16:22:53 +010018 InputReader<Char> in(unicode_cache, str);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000019 DateStringTokenizer<Char> scanner(&in);
Steve Blocka7e24c12009-10-30 11:49:00 +000020 TimeZoneComposer tz;
21 TimeComposer time;
22 DayComposer day;
23
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000024 // Specification:
25 // Accept ES5 ISO 8601 date-time-strings or legacy dates compatible
26 // with Safari.
27 // ES5 ISO 8601 dates:
28 // [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]]
29 // where yyyy is in the range 0000..9999 and
30 // +/-yyyyyy is in the range -999999..+999999 -
31 // but -000000 is invalid (year zero must be positive),
32 // MM is in the range 01..12,
33 // DD is in the range 01..31,
34 // MM and DD defaults to 01 if missing,,
35 // HH is generally in the range 00..23, but can be 24 if mm, ss
36 // and sss are zero (or missing), representing midnight at the
37 // end of a day,
38 // mm and ss are in the range 00..59,
39 // sss is in the range 000..999,
40 // hh is in the range 00..23,
41 // mm, ss, and sss default to 00 if missing, and
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 // timezone defaults to Z if missing
43 // (following Safari, ISO actually demands local time).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000044 // Extensions:
45 // We also allow sss to have more or less than three digits (but at
46 // least one).
47 // We allow hh:mm to be specified as hhmm.
48 // Legacy dates:
49 // Any unrecognized word before the first number is ignored.
50 // Parenthesized text is ignored.
51 // An unsigned number followed by ':' is a time value, and is
52 // added to the TimeComposer. A number followed by '::' adds a second
53 // zero as well. A number followed by '.' is also a time and must be
54 // followed by milliseconds.
55 // Any other number is a date component and is added to DayComposer.
56 // A month name (or really: any word having the same first three letters
57 // as a month name) is recorded as a named month in the Day composer.
58 // A word recognizable as a time-zone is recorded as such, as is
59 // '(+|-)(hhmm|hh:)'.
60 // Legacy dates don't allow extra signs ('+' or '-') or umatched ')'
61 // after a number has been read (before the first number, any garbage
62 // is allowed).
63 // Intersection of the two:
64 // A string that matches both formats (e.g. 1970-01-01) will be
65 // parsed as an ES5 date-time string - which means it will default
66 // to UTC time-zone. That's unavoidable if following the ES5
67 // specification.
68 // After a valid "T" has been read while scanning an ES5 datetime string,
69 // the input can no longer be a valid legacy date, since the "T" is a
70 // garbage string after a number has been read.
71
72 // First try getting as far as possible with as ES5 Date Time String.
73 DateToken next_unhandled_token = ParseES5DateTime(&scanner, &day, &time, &tz);
74 if (next_unhandled_token.IsInvalid()) return false;
75 bool has_read_number = !day.IsEmpty();
76 // If there's anything left, continue with the legacy parser.
77 for (DateToken token = next_unhandled_token;
78 !token.IsEndOfInput();
79 token = scanner.Next()) {
80 if (token.IsNumber()) {
81 has_read_number = true;
82 int n = token.number();
83 if (scanner.SkipSymbol(':')) {
84 if (scanner.SkipSymbol(':')) {
Steve Blocka7e24c12009-10-30 11:49:00 +000085 // n + "::"
86 if (!time.IsEmpty()) return false;
87 time.Add(n);
88 time.Add(0);
89 } else {
90 // n + ":"
91 if (!time.Add(n)) return false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000092 if (scanner.Peek().IsSymbol('.')) scanner.Next();
Steve Blocka7e24c12009-10-30 11:49:00 +000093 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000094 } else if (scanner.SkipSymbol('.') && time.IsExpecting(n)) {
Steve Block6ded16b2010-05-10 14:33:55 +010095 time.Add(n);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000096 if (!scanner.Peek().IsNumber()) return false;
97 int n = ReadMilliseconds(scanner.Next());
98 if (n < 0) return false;
Steve Block6ded16b2010-05-10 14:33:55 +010099 time.AddFinal(n);
Steve Blocka7e24c12009-10-30 11:49:00 +0000100 } else if (tz.IsExpecting(n)) {
101 tz.SetAbsoluteMinute(n);
102 } else if (time.IsExpecting(n)) {
103 time.AddFinal(n);
Steve Block59151502010-09-22 15:07:15 +0100104 // Require end, white space, "Z", "+" or "-" immediately after
105 // finalizing time.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000106 DateToken peek = scanner.Peek();
107 if (!peek.IsEndOfInput() &&
108 !peek.IsWhiteSpace() &&
109 !peek.IsKeywordZ() &&
110 !peek.IsAsciiSign()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000111 } else {
112 if (!day.Add(n)) return false;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000113 scanner.SkipSymbol('-');
Steve Blocka7e24c12009-10-30 11:49:00 +0000114 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000115 } else if (token.IsKeyword()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 // Parse a "word" (sequence of chars. >= 'A').
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000117 KeywordType type = token.keyword_type();
118 int value = token.keyword_value();
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 if (type == AM_PM && !time.IsEmpty()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000120 time.SetHourOffset(value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 } else if (type == MONTH_NAME) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000122 day.SetNamedMonth(value);
123 scanner.SkipSymbol('-');
124 } else if (type == TIME_ZONE_NAME && has_read_number) {
125 tz.Set(value);
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 } else {
127 // Garbage words are illegal if a number has been read.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000128 if (has_read_number) return false;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 // The first number has to be separated from garbage words by
130 // whitespace or other separators.
131 if (scanner.Peek().IsNumber()) return false;
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000133 } else if (token.IsAsciiSign() && (tz.IsUTC() || !time.IsEmpty())) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000134 // Parse UTC offset (only after UTC or time).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000135 tz.SetSign(token.ascii_sign());
136 // The following number may be empty.
137 int n = 0;
138 if (scanner.Peek().IsNumber()) {
139 n = scanner.Next().number();
140 }
141 has_read_number = true;
142
143 if (scanner.Peek().IsSymbol(':')) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 tz.SetAbsoluteHour(n);
145 tz.SetAbsoluteMinute(kNone);
146 } else {
147 tz.SetAbsoluteHour(n / 100);
148 tz.SetAbsoluteMinute(n % 100);
149 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000150 } else if ((token.IsAsciiSign() || token.IsSymbol(')')) &&
151 has_read_number) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 // Extra sign or ')' is illegal if a number has been read.
153 return false;
154 } else {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000155 // Ignore other characters and whitespace.
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 }
157 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000158
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 return day.Write(out) && time.Write(out) && tz.Write(out);
160}
161
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000162
163template<typename CharType>
164DateParser::DateToken DateParser::DateStringTokenizer<CharType>::Scan() {
165 int pre_pos = in_->position();
166 if (in_->IsEnd()) return DateToken::EndOfInput();
167 if (in_->IsAsciiDigit()) {
168 int n = in_->ReadUnsignedNumeral();
169 int length = in_->position() - pre_pos;
170 return DateToken::Number(n, length);
171 }
172 if (in_->Skip(':')) return DateToken::Symbol(':');
173 if (in_->Skip('-')) return DateToken::Symbol('-');
174 if (in_->Skip('+')) return DateToken::Symbol('+');
175 if (in_->Skip('.')) return DateToken::Symbol('.');
176 if (in_->Skip(')')) return DateToken::Symbol(')');
177 if (in_->IsAsciiAlphaOrAbove()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 DCHECK(KeywordTable::kPrefixLength == 3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000179 uint32_t buffer[3] = {0, 0, 0};
180 int length = in_->ReadWord(buffer, 3);
181 int index = KeywordTable::Lookup(buffer, length);
182 return DateToken::Keyword(KeywordTable::GetType(index),
183 KeywordTable::GetValue(index),
184 length);
185 }
186 if (in_->SkipWhiteSpace()) {
187 return DateToken::WhiteSpace(in_->position() - pre_pos);
188 }
189 if (in_->SkipParentheses()) {
190 return DateToken::Unknown();
191 }
192 in_->Next();
193 return DateToken::Unknown();
194}
195
196
197template <typename Char>
198DateParser::DateToken DateParser::ParseES5DateTime(
199 DateStringTokenizer<Char>* scanner,
200 DayComposer* day,
201 TimeComposer* time,
202 TimeZoneComposer* tz) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 DCHECK(day->IsEmpty());
204 DCHECK(time->IsEmpty());
205 DCHECK(tz->IsEmpty());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000206
207 // Parse mandatory date string: [('-'|'+')yy]yyyy[':'MM[':'DD]]
208 if (scanner->Peek().IsAsciiSign()) {
209 // Keep the sign token, so we can pass it back to the legacy
210 // parser if we don't use it.
211 DateToken sign_token = scanner->Next();
212 if (!scanner->Peek().IsFixedLengthNumber(6)) return sign_token;
213 int sign = sign_token.ascii_sign();
214 int year = scanner->Next().number();
215 if (sign < 0 && year == 0) return sign_token;
216 day->Add(sign * year);
217 } else if (scanner->Peek().IsFixedLengthNumber(4)) {
218 day->Add(scanner->Next().number());
219 } else {
220 return scanner->Next();
221 }
222 if (scanner->SkipSymbol('-')) {
223 if (!scanner->Peek().IsFixedLengthNumber(2) ||
224 !DayComposer::IsMonth(scanner->Peek().number())) return scanner->Next();
225 day->Add(scanner->Next().number());
226 if (scanner->SkipSymbol('-')) {
227 if (!scanner->Peek().IsFixedLengthNumber(2) ||
228 !DayComposer::IsDay(scanner->Peek().number())) return scanner->Next();
229 day->Add(scanner->Next().number());
230 }
231 }
232 // Check for optional time string: 'T'HH':'mm[':'ss['.'sss]]Z
233 if (!scanner->Peek().IsKeywordType(TIME_SEPARATOR)) {
234 if (!scanner->Peek().IsEndOfInput()) return scanner->Next();
235 } else {
236 // ES5 Date Time String time part is present.
237 scanner->Next();
238 if (!scanner->Peek().IsFixedLengthNumber(2) ||
239 !Between(scanner->Peek().number(), 0, 24)) {
240 return DateToken::Invalid();
241 }
242 // Allow 24:00[:00[.000]], but no other time starting with 24.
243 bool hour_is_24 = (scanner->Peek().number() == 24);
244 time->Add(scanner->Next().number());
245 if (!scanner->SkipSymbol(':')) return DateToken::Invalid();
246 if (!scanner->Peek().IsFixedLengthNumber(2) ||
247 !TimeComposer::IsMinute(scanner->Peek().number()) ||
248 (hour_is_24 && scanner->Peek().number() > 0)) {
249 return DateToken::Invalid();
250 }
251 time->Add(scanner->Next().number());
252 if (scanner->SkipSymbol(':')) {
253 if (!scanner->Peek().IsFixedLengthNumber(2) ||
254 !TimeComposer::IsSecond(scanner->Peek().number()) ||
255 (hour_is_24 && scanner->Peek().number() > 0)) {
256 return DateToken::Invalid();
257 }
258 time->Add(scanner->Next().number());
259 if (scanner->SkipSymbol('.')) {
260 if (!scanner->Peek().IsNumber() ||
261 (hour_is_24 && scanner->Peek().number() > 0)) {
262 return DateToken::Invalid();
263 }
264 // Allow more or less than the mandated three digits.
265 time->Add(ReadMilliseconds(scanner->Next()));
266 }
267 }
268 // Check for optional timezone designation: 'Z' | ('+'|'-')hh':'mm
269 if (scanner->Peek().IsKeywordZ()) {
270 scanner->Next();
271 tz->Set(0);
272 } else if (scanner->Peek().IsSymbol('+') ||
273 scanner->Peek().IsSymbol('-')) {
274 tz->SetSign(scanner->Next().symbol() == '+' ? 1 : -1);
275 if (scanner->Peek().IsFixedLengthNumber(4)) {
276 // hhmm extension syntax.
277 int hourmin = scanner->Next().number();
278 int hour = hourmin / 100;
279 int min = hourmin % 100;
280 if (!TimeComposer::IsHour(hour) || !TimeComposer::IsMinute(min)) {
281 return DateToken::Invalid();
282 }
283 tz->SetAbsoluteHour(hour);
284 tz->SetAbsoluteMinute(min);
285 } else {
286 // hh:mm standard syntax.
287 if (!scanner->Peek().IsFixedLengthNumber(2) ||
288 !TimeComposer::IsHour(scanner->Peek().number())) {
289 return DateToken::Invalid();
290 }
291 tz->SetAbsoluteHour(scanner->Next().number());
292 if (!scanner->SkipSymbol(':')) return DateToken::Invalid();
293 if (!scanner->Peek().IsFixedLengthNumber(2) ||
294 !TimeComposer::IsMinute(scanner->Peek().number())) {
295 return DateToken::Invalid();
296 }
297 tz->SetAbsoluteMinute(scanner->Next().number());
298 }
299 }
300 if (!scanner->Peek().IsEndOfInput()) return DateToken::Invalid();
301 }
302 // Successfully parsed ES5 Date Time String. Default to UTC if no TZ given.
303 if (tz->IsEmpty()) tz->Set(0);
304 day->set_iso_date();
305 return DateToken::EndOfInput();
306}
307
308
Steve Blocka7e24c12009-10-30 11:49:00 +0000309} } // namespace v8::internal
310
311#endif // V8_DATEPARSER_INL_H_