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

Unified Diff: lib/i18n/_date_format_helpers.dart

Issue 10807096: Add date formatting and parsing code. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 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 side-by-side diff with in-line comments
Download patch
Index: lib/i18n/_date_format_helpers.dart
===================================================================
--- lib/i18n/_date_format_helpers.dart (revision 0)
+++ lib/i18n/_date_format_helpers.dart (revision 0)
@@ -0,0 +1,142 @@
+/**
Emily Fortuna 2012/08/06 22:43:00 copyright notice at top
Alan Knight 2012/08/08 00:47:44 Oops. Done.
+ * A class for holding onto the data for a date so that it can be built
+ * up incrementally.
+ */
+class _DateBuilder {
+ int year = 0,
+ month = 0,
+ day = 0,
+ hour = 0,
+ minute = 0,
+ second = 0,
+ fractionalSecond = 0;
+ bool pm = false;
+ bool utc = false;
+
+ // Functions that exist just be closurized so we can pass them to a general
Emily Fortuna 2012/08/06 22:43:00 missing word in here "exist just be closurized"
Alan Knight 2012/08/08 00:47:44 Done.
+ // method.
+ void setYear(x) { year = x; }
+ void setMonth(x) { month = x; }
+ void setDay(x) { day = x; }
+ void setHour(x) { hour = x; }
+ void setMinute(x) { minute = x; }
+ void setSecond(x) { second = x; }
+ void setFractionalSecond(x) { fractionalSecond = x; }
+
+ /**
+ * Return a date built using our values. If no date portion is set,
+ * use today's date, as otherwise the constructor will fail.
+ */
+ Date asDate() {
+ if (year == 0 || month == 0 || day == 0) {
+ var today = new Date.now();
+ if (year == 0) year = today.year;
+ if (month == 0) month = today.month;
+ if (day == 0) day = today.day;
+ }
+
+ // TODO(alanknight): Validate the date, especially for things which
+ // can crash the VM, e.g. large month values.
+ return new Date(
+ year,
Emily Fortuna 2012/08/06 22:43:00 indent 4 spaces here
Alan Knight 2012/08/08 00:47:44 Done.
+ month,
+ day,
+ pm ? hour + 12 : hour,
+ minute,
+ second,
+ fractionalSecond,
+ utc);
+ }
+}
+
+/**
+ * A simple and not particularly general stream class to make parsing
+ * dates from strings simpler. It is general enough to operate on either
+ * lists or strings.
+ */
+class _Stream {
+ var contents;
+ int index = 0;
+
+ _Stream(this.contents);
+
+ bool atEnd() => index >= contents.length;
+
+ Dynamic next() => contents[index++];
+
+ /**
+ * Return the next [howMany] items, or as many as there are remaining.
+ * Advance the stream by that many positions.
+ */
+ read([howMany = 1]) {
+ var result = peek(howMany);
+ index += howMany;
+ return result;
+ }
+
+ /**
+ * Return the next [howMany] items, or as many as there are remaining.
+ * Does not modify the stream position.
+ */
+ peek([howMany = 1]) {
+ var result;
+ if (contents is String) {
+ result = contents.substring(
+ index,
Emily Fortuna 2012/08/06 22:43:00 4 spaces indent if it's after an opening (
Alan Knight 2012/08/08 00:47:44 Done.
+ Math.min(index + howMany, contents.length));
+ } else {
+ // Assume List
+ result = contents.getRange(index,howMany);
+ }
+ return result;
+ }
+
+ /** Return the remaining contents of the stream */
+ rest() => peek(contents.length - index);
+
+ /** Find the index of the first element for which [f] returns true.*/
Emily Fortuna 2012/08/06 22:43:00 mention this has the side effect of advancing the
Alan Knight 2012/08/08 00:47:44 Done.
+ int findIndex(Function f) {
+ while (!atEnd()) {
+ if (f(next())) return index - 1;
+ }
+ return null;
+ }
+
+ /**
+ * Find the indexes of all the elements for which [f] returns true.
+ * Also leaves the stream positioned at the end.
+ */
+ List findIndexes(Function f) {
+ var results = [];
+ while (!atEnd()) {
+ if (f(next())) results.add(index - 1);
+ }
+ return results;
+ }
+
+ /**
+ * Assuming that the contents are characters, read as many digits as we
+ * can see and then return the corresponding integer. Advance the stream.
+ */
+ int nextInteger() {
+ var validDigits = '0123456789';
+ var digits = [];
+ while (!atEnd() && (validDigits.contains(peek()))) {
+ digits.add(next().charCodeAt(0));
+ }
+ return Math.parseInt(new String.fromCharCodes(digits));
+ }
+}
+
+/**
+ * Exception indicating that we could not parse a particular string into a
+ * Date according to the specified pattern.
+ */
+class FormatException implements Exception {
Emily Fortuna 2012/08/06 22:43:00 We now have a FormatException as a subclass of the
Alan Knight 2012/08/08 00:47:44 Done.
+ final _Stream input;
+ final _DateFormatField field;
+
+ const FormatException(this.input, this.field);
+
+ toString() => "FormatException: $field matching ${input.rest}";
+}

Powered by Google App Engine
This is Rietveld 408576698