Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: src/dateparser-inl.h

Issue 1229903004: Make dates default to the local timezone if none specified (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Actually fix test Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/dateparser.h ('k') | test/mjsunit/date.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef V8_DATEPARSER_INL_H_ 5 #ifndef V8_DATEPARSER_INL_H_
6 #define V8_DATEPARSER_INL_H_ 6 #define V8_DATEPARSER_INL_H_
7 7
8 #include "src/dateparser.h" 8 #include "src/dateparser.h"
9 9
10 namespace v8 { 10 namespace v8 {
11 namespace internal { 11 namespace internal {
12 12
13 template <typename Char> 13 template <typename Char>
14 bool DateParser::Parse(Vector<Char> str, 14 bool DateParser::Parse(Vector<Char> str,
15 FixedArray* out, 15 FixedArray* out,
16 UnicodeCache* unicode_cache) { 16 UnicodeCache* unicode_cache) {
17 DCHECK(out->length() >= OUTPUT_SIZE); 17 DCHECK(out->length() >= OUTPUT_SIZE);
18 InputReader<Char> in(unicode_cache, str); 18 InputReader<Char> in(unicode_cache, str);
19 DateStringTokenizer<Char> scanner(&in); 19 DateStringTokenizer<Char> scanner(&in);
20 TimeZoneComposer tz; 20 TimeZoneComposer tz;
21 TimeComposer time; 21 TimeComposer time;
22 DayComposer day; 22 DayComposer day;
23 23
24 // Specification: 24 // Specification:
25 // Accept ES5 ISO 8601 date-time-strings or legacy dates compatible 25 // Accept ES6 ISO 8601 date-time-strings or legacy dates compatible
26 // with Safari. 26 // with Safari.
27 // ES5 ISO 8601 dates: 27 // ES6 ISO 8601 dates:
28 // [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]] 28 // [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]]
29 // where yyyy is in the range 0000..9999 and 29 // where yyyy is in the range 0000..9999 and
30 // +/-yyyyyy is in the range -999999..+999999 - 30 // +/-yyyyyy is in the range -999999..+999999 -
31 // but -000000 is invalid (year zero must be positive), 31 // but -000000 is invalid (year zero must be positive),
32 // MM is in the range 01..12, 32 // MM is in the range 01..12,
33 // DD is in the range 01..31, 33 // DD is in the range 01..31,
34 // MM and DD defaults to 01 if missing,, 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 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 36 // and sss are zero (or missing), representing midnight at the
37 // end of a day, 37 // end of a day,
38 // mm and ss are in the range 00..59, 38 // mm and ss are in the range 00..59,
39 // sss is in the range 000..999, 39 // sss is in the range 000..999,
40 // hh is in the range 00..23, 40 // hh is in the range 00..23,
41 // mm, ss, and sss default to 00 if missing, and 41 // mm, ss, and sss default to 00 if missing, and
42 // timezone defaults to Z if missing 42 // timezone defaults to local time if missing.
43 // (following Safari, ISO actually demands local time).
44 // Extensions: 43 // Extensions:
45 // We also allow sss to have more or less than three digits (but at 44 // We also allow sss to have more or less than three digits (but at
46 // least one). 45 // least one).
47 // We allow hh:mm to be specified as hhmm. 46 // We allow hh:mm to be specified as hhmm.
48 // Legacy dates: 47 // Legacy dates:
49 // Any unrecognized word before the first number is ignored. 48 // Any unrecognized word before the first number is ignored.
50 // Parenthesized text is ignored. 49 // Parenthesized text is ignored.
51 // An unsigned number followed by ':' is a time value, and is 50 // An unsigned number followed by ':' is a time value, and is
52 // added to the TimeComposer. A number followed by '::' adds a second 51 // 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 52 // zero as well. A number followed by '.' is also a time and must be
54 // followed by milliseconds. 53 // followed by milliseconds.
55 // Any other number is a date component and is added to DayComposer. 54 // 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 55 // 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. 56 // 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 57 // A word recognizable as a time-zone is recorded as such, as is
59 // '(+|-)(hhmm|hh:)'. 58 // '(+|-)(hhmm|hh:)'.
60 // Legacy dates don't allow extra signs ('+' or '-') or umatched ')' 59 // Legacy dates don't allow extra signs ('+' or '-') or umatched ')'
61 // after a number has been read (before the first number, any garbage 60 // after a number has been read (before the first number, any garbage
62 // is allowed). 61 // is allowed).
63 // Intersection of the two: 62 // Intersection of the two:
64 // A string that matches both formats (e.g. 1970-01-01) will be 63 // 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 64 // parsed as an ES6 date-time string.
66 // to UTC time-zone. That's unavoidable if following the ES5 65 // After a valid "T" has been read while scanning an ES6 datetime string,
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 66 // the input can no longer be a valid legacy date, since the "T" is a
70 // garbage string after a number has been read. 67 // garbage string after a number has been read.
71 68
72 // First try getting as far as possible with as ES5 Date Time String. 69 // First try getting as far as possible with as ES6 Date Time String.
73 DateToken next_unhandled_token = ParseES5DateTime(&scanner, &day, &time, &tz); 70 DateToken next_unhandled_token = ParseES6DateTime(&scanner, &day, &time, &tz);
74 if (next_unhandled_token.IsInvalid()) return false; 71 if (next_unhandled_token.IsInvalid()) return false;
75 bool has_read_number = !day.IsEmpty(); 72 bool has_read_number = !day.IsEmpty();
76 // If there's anything left, continue with the legacy parser. 73 // If there's anything left, continue with the legacy parser.
77 for (DateToken token = next_unhandled_token; 74 for (DateToken token = next_unhandled_token;
78 !token.IsEndOfInput(); 75 !token.IsEndOfInput();
79 token = scanner.Next()) { 76 token = scanner.Next()) {
80 if (token.IsNumber()) { 77 if (token.IsNumber()) {
81 has_read_number = true; 78 has_read_number = true;
82 int n = token.number(); 79 int n = token.number();
83 if (scanner.SkipSymbol(':')) { 80 if (scanner.SkipSymbol(':')) {
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 } 185 }
189 if (in_->SkipParentheses()) { 186 if (in_->SkipParentheses()) {
190 return DateToken::Unknown(); 187 return DateToken::Unknown();
191 } 188 }
192 in_->Next(); 189 in_->Next();
193 return DateToken::Unknown(); 190 return DateToken::Unknown();
194 } 191 }
195 192
196 193
197 template <typename Char> 194 template <typename Char>
198 DateParser::DateToken DateParser::ParseES5DateTime( 195 DateParser::DateToken DateParser::ParseES6DateTime(
199 DateStringTokenizer<Char>* scanner, 196 DateStringTokenizer<Char>* scanner,
200 DayComposer* day, 197 DayComposer* day,
201 TimeComposer* time, 198 TimeComposer* time,
202 TimeZoneComposer* tz) { 199 TimeZoneComposer* tz) {
203 DCHECK(day->IsEmpty()); 200 DCHECK(day->IsEmpty());
204 DCHECK(time->IsEmpty()); 201 DCHECK(time->IsEmpty());
205 DCHECK(tz->IsEmpty()); 202 DCHECK(tz->IsEmpty());
206 203
207 // Parse mandatory date string: [('-'|'+')yy]yyyy[':'MM[':'DD]] 204 // Parse mandatory date string: [('-'|'+')yy]yyyy[':'MM[':'DD]]
208 if (scanner->Peek().IsAsciiSign()) { 205 if (scanner->Peek().IsAsciiSign()) {
(...skipping 17 matching lines...) Expand all
226 if (scanner->SkipSymbol('-')) { 223 if (scanner->SkipSymbol('-')) {
227 if (!scanner->Peek().IsFixedLengthNumber(2) || 224 if (!scanner->Peek().IsFixedLengthNumber(2) ||
228 !DayComposer::IsDay(scanner->Peek().number())) return scanner->Next(); 225 !DayComposer::IsDay(scanner->Peek().number())) return scanner->Next();
229 day->Add(scanner->Next().number()); 226 day->Add(scanner->Next().number());
230 } 227 }
231 } 228 }
232 // Check for optional time string: 'T'HH':'mm[':'ss['.'sss]]Z 229 // Check for optional time string: 'T'HH':'mm[':'ss['.'sss]]Z
233 if (!scanner->Peek().IsKeywordType(TIME_SEPARATOR)) { 230 if (!scanner->Peek().IsKeywordType(TIME_SEPARATOR)) {
234 if (!scanner->Peek().IsEndOfInput()) return scanner->Next(); 231 if (!scanner->Peek().IsEndOfInput()) return scanner->Next();
235 } else { 232 } else {
236 // ES5 Date Time String time part is present. 233 // ES6 Date Time String time part is present.
237 scanner->Next(); 234 scanner->Next();
238 if (!scanner->Peek().IsFixedLengthNumber(2) || 235 if (!scanner->Peek().IsFixedLengthNumber(2) ||
239 !Between(scanner->Peek().number(), 0, 24)) { 236 !Between(scanner->Peek().number(), 0, 24)) {
240 return DateToken::Invalid(); 237 return DateToken::Invalid();
241 } 238 }
242 // Allow 24:00[:00[.000]], but no other time starting with 24. 239 // Allow 24:00[:00[.000]], but no other time starting with 24.
243 bool hour_is_24 = (scanner->Peek().number() == 24); 240 bool hour_is_24 = (scanner->Peek().number() == 24);
244 time->Add(scanner->Next().number()); 241 time->Add(scanner->Next().number());
245 if (!scanner->SkipSymbol(':')) return DateToken::Invalid(); 242 if (!scanner->SkipSymbol(':')) return DateToken::Invalid();
246 if (!scanner->Peek().IsFixedLengthNumber(2) || 243 if (!scanner->Peek().IsFixedLengthNumber(2) ||
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 if (!scanner->SkipSymbol(':')) return DateToken::Invalid(); 289 if (!scanner->SkipSymbol(':')) return DateToken::Invalid();
293 if (!scanner->Peek().IsFixedLengthNumber(2) || 290 if (!scanner->Peek().IsFixedLengthNumber(2) ||
294 !TimeComposer::IsMinute(scanner->Peek().number())) { 291 !TimeComposer::IsMinute(scanner->Peek().number())) {
295 return DateToken::Invalid(); 292 return DateToken::Invalid();
296 } 293 }
297 tz->SetAbsoluteMinute(scanner->Next().number()); 294 tz->SetAbsoluteMinute(scanner->Next().number());
298 } 295 }
299 } 296 }
300 if (!scanner->Peek().IsEndOfInput()) return DateToken::Invalid(); 297 if (!scanner->Peek().IsEndOfInput()) return DateToken::Invalid();
301 } 298 }
302 // Successfully parsed ES5 Date Time String. Default to UTC if no TZ given. 299 // Successfully parsed ES6 Date Time String.
303 if (tz->IsEmpty()) tz->Set(0);
304 day->set_iso_date(); 300 day->set_iso_date();
305 return DateToken::EndOfInput(); 301 return DateToken::EndOfInput();
306 } 302 }
307 303
308 304
309 } } // namespace v8::internal 305 } } // namespace v8::internal
310 306
311 #endif // V8_DATEPARSER_INL_H_ 307 #endif // V8_DATEPARSER_INL_H_
OLDNEW
« no previous file with comments | « src/dateparser.h ('k') | test/mjsunit/date.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698