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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | lib/src/intl/date_format_field.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/intl/date_format.dart
diff --git a/lib/src/intl/date_format.dart b/lib/src/intl/date_format.dart
index 3aede3676463472f2be68972f5ac67bda6cffde2..86402c9de9ff44483ae05433ed26a45c79f26540 100644
--- a/lib/src/intl/date_format.dart
+++ b/lib/src/intl/date_format.dart
@@ -262,6 +262,58 @@ class DateFormat {
_parse(inputString, utc: utc, strict: false);
/**
+ * Given user input, attempt to parse the [inputString] "loosely" into the
+ * anticipated format, accepting some variations from the strict format.
+ *
+ * If [inputString]
+ * is accepted by [parseStrict], just return the result. If not, attempt to
+ * parse it, but accepting either upper or
+ * lower case, allowing delimiters to be missing and replaced or
+ * supplemented with whitespace,
+ * and allowing arbitrary amounts of whitespace wherever whitespace is
+ * permitted. Note that this does not allow trailing characters, the way
+ * [parse] does. It also does not allow leading whitespace on delimiters,
+ * and does not allow alternative names for months or weekdays other than
+ * those the format knows about. The restrictions are quite arbitrary and
+ * it's not known how well they'll work for locales that aren't English-like.
+ *
+ * If [inputString] does not parse, this throws a
+ * [FormatException].
+ *
+ * For example, this will accept
+ *
+ * new DateTimeFormat.yMMMd("en_US").parseLoose("SEp 3 2014");
+ * new DateTimeFormat.yMd("en_US").parseLoose("09 03/2014");
+ *
+ * It will NOT accept
+ *
+ * // "Sept" is not a valid month name.
+ * new DateTimeFormat.yMMMd("en_US").parseLoose("Sept 3, 2014");
+ * // Delimiters can't have leading whitespace.
+ * new DateTimeFormat.yMd("en_US").parseLoose("09 / 03 / 2014");
+ */
+ DateTime parseLoose(String inputString, [utc = false]) {
+ try {
+ return _parse(inputString, utc: utc, strict: true);
+ } on FormatException {
+ return _parseLoose(inputString.toLowerCase(), utc);
+ }
+ }
+
+ _parseLoose(String inputString, bool utc) {
+ var dateFields = new _DateBuilder();
+ if (utc) dateFields.utc = true;
+ var stream = new _Stream(inputString);
+ _formatFields.forEach((f) => f.parseLoose(stream, dateFields));
+ if (!stream.atEnd()) {
+ throw new FormatException(
+ "Characters remaining after date parsing in $inputString");
+ }
+ dateFields.verify(inputString);
+ return dateFields.asDate();
+ }
+
+ /**
* Given user input, attempt to parse the [inputString] into the anticipated
* format, treating it as being in the local timezone. If [inputString] does
* not match our format, throws a [FormatException]. This will reject dates
« 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