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

Side by Side Diff: lib/src/intl/date_format.dart

Issue 932093004: Add loose parsing option for dates, accepting mixed case and missing delimiters (Closed) Base URL: https://github.com/dart-lang/intl.git@master
Patch Set: Fix test that wasn't checking for exception Created 5 years, 9 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 | « CHANGELOG.md ('k') | lib/src/intl/date_format_field.dart » ('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 (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of intl; 5 part of intl;
6 6
7 // TODO(efortuna): Customized pattern system -- suggested by i18n needs 7 // TODO(efortuna): Customized pattern system -- suggested by i18n needs
8 // feedback on appropriateness. 8 // feedback on appropriateness.
9 /** 9 /**
10 * DateFormat is for formatting and parsing dates in a locale-sensitive 10 * DateFormat is for formatting and parsing dates in a locale-sensitive
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 * format, treating it as being in the local timezone. If [inputString] does 255 * format, treating it as being in the local timezone. If [inputString] does
256 * not match our format, throws a [FormatException]. This will accept dates 256 * not match our format, throws a [FormatException]. This will accept dates
257 * whose values are not strictly valid, or strings with additional characters 257 * whose values are not strictly valid, or strings with additional characters
258 * (including whitespace) after a valid date. For stricter parsing, use 258 * (including whitespace) after a valid date. For stricter parsing, use
259 * [parseStrict]. 259 * [parseStrict].
260 */ 260 */
261 DateTime parse(String inputString, [utc = false]) => 261 DateTime parse(String inputString, [utc = false]) =>
262 _parse(inputString, utc: utc, strict: false); 262 _parse(inputString, utc: utc, strict: false);
263 263
264 /** 264 /**
265 * Given user input, attempt to parse the [inputString] "loosely" into the
266 * anticipated format, accepting some variations from the strict format.
267 *
268 * If [inputString]
269 * is accepted by [parseStrict], just return the result. If not, attempt to
270 * parse it, but accepting either upper or
271 * lower case, allowing delimiters to be missing and replaced or
272 * supplemented with whitespace,
273 * and allowing arbitrary amounts of whitespace wherever whitespace is
274 * permitted. Note that this does not allow trailing characters, the way
275 * [parse] does. It also does not allow leading whitespace on delimiters,
276 * and does not allow alternative names for months or weekdays other than
277 * those the format knows about. The restrictions are quite arbitrary and
278 * it's not known how well they'll work for locales that aren't English-like.
279 *
280 * If [inputString] does not parse, this throws a
281 * [FormatException].
282 *
283 * For example, this will accept
284 *
285 * new DateTimeFormat.yMMMd("en_US").parseLoose("SEp 3 2014");
286 * new DateTimeFormat.yMd("en_US").parseLoose("09 03/2014");
287 *
288 * It will NOT accept
289 *
290 * // "Sept" is not a valid month name.
291 * new DateTimeFormat.yMMMd("en_US").parseLoose("Sept 3, 2014");
292 * // Delimiters can't have leading whitespace.
293 * new DateTimeFormat.yMd("en_US").parseLoose("09 / 03 / 2014");
294 */
295 DateTime parseLoose(String inputString, [utc = false]) {
296 try {
297 return _parse(inputString, utc: utc, strict: true);
298 } on FormatException {
299 return _parseLoose(inputString.toLowerCase(), utc);
300 }
301 }
302
303 _parseLoose(String inputString, bool utc) {
304 var dateFields = new _DateBuilder();
305 if (utc) dateFields.utc = true;
306 var stream = new _Stream(inputString);
307 _formatFields.forEach((f) => f.parseLoose(stream, dateFields));
308 if (!stream.atEnd()) {
309 throw new FormatException(
310 "Characters remaining after date parsing in $inputString");
311 }
312 dateFields.verify(inputString);
313 return dateFields.asDate();
314 }
315
316 /**
265 * Given user input, attempt to parse the [inputString] into the anticipated 317 * Given user input, attempt to parse the [inputString] into the anticipated
266 * format, treating it as being in the local timezone. If [inputString] does 318 * format, treating it as being in the local timezone. If [inputString] does
267 * not match our format, throws a [FormatException]. This will reject dates 319 * not match our format, throws a [FormatException]. This will reject dates
268 * whose values are not strictly valid, even if the 320 * whose values are not strictly valid, even if the
269 * DateTime constructor will accept them. It will also rejct strings with 321 * DateTime constructor will accept them. It will also rejct strings with
270 * additional characters (including whitespace) after a valid date. For 322 * additional characters (including whitespace) after a valid date. For
271 * looser parsing, use [parse]. 323 * looser parsing, use [parse].
272 */ 324 */
273 DateTime parseStrict(String inputString, [utc = false]) => 325 DateTime parseStrict(String inputString, [utc = false]) =>
274 _parse(inputString, utc: utc, strict: true); 326 _parse(inputString, utc: utc, strict: true);
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
621 _DateFormatField _match(String pattern) { 673 _DateFormatField _match(String pattern) {
622 for (var i = 0; i < _matchers.length; i++) { 674 for (var i = 0; i < _matchers.length; i++) {
623 var regex = _matchers[i]; 675 var regex = _matchers[i];
624 var match = regex.firstMatch(pattern); 676 var match = regex.firstMatch(pattern);
625 if (match != null) { 677 if (match != null) {
626 return _fieldConstructors[i](match.group(0), this); 678 return _fieldConstructors[i](match.group(0), this);
627 } 679 }
628 } 680 }
629 } 681 }
630 } 682 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | lib/src/intl/date_format_field.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698