| OLD | NEW |
| 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 /** | 5 /** |
| 6 * This is a private class internal to DateFormat which is used for formatting | 6 * This is a private class internal to DateFormat which is used for formatting |
| 7 * particular fields in a template. e.g. if the format is hh:mm:ss then the | 7 * particular fields in a template. e.g. if the format is hh:mm:ss then the |
| 8 * fields would be "hh", ":", "mm", ":", and "ss". Each type of field knows | 8 * fields would be "hh", ":", "mm", ":", and "ss". Each type of field knows |
| 9 * how to format that portion of a date. | 9 * how to format that portion of a date. |
| 10 */ | 10 */ |
| 11 class _DateFormatField { | 11 abstract class _DateFormatField { |
| 12 /** The format string that defines us, e.g. "hh" */ | 12 /** The format string that defines us, e.g. "hh" */ |
| 13 String pattern; | 13 String pattern; |
| 14 | 14 |
| 15 /** The DateFormat that we are part of.*/ | 15 /** The DateFormat that we are part of.*/ |
| 16 DateFormat parent; | 16 DateFormat parent; |
| 17 | 17 |
| 18 _DateFormatField(this.pattern, this.parent); | 18 _DateFormatField(this.pattern, this.parent); |
| 19 | 19 |
| 20 /** | 20 /** |
| 21 * Return the width of [pattern]. Different widths represent different | 21 * Return the width of [pattern]. Different widths represent different |
| 22 * formatting options. See the comment for DateFormat for details. | 22 * formatting options. See the comment for DateFormat for details. |
| 23 */ | 23 */ |
| 24 int get width => pattern.length; | 24 int get width => pattern.length; |
| 25 | 25 |
| 26 String fullPattern() => pattern; | 26 String fullPattern() => pattern; |
| 27 | 27 |
| 28 String toString() => pattern; | 28 String toString() => pattern; |
| 29 | 29 |
| 30 /** Format date according to our specification and return the result. */ | 30 /** Format date according to our specification and return the result. */ |
| 31 String format(Date date) { | 31 String format(Date date) { |
| 32 // Default implementation in the superclass, works for both types of | 32 // Default implementation in the superclass, works for both types of |
| 33 // literal patterns, and is overridden by _DateFormatPatternField. | 33 // literal patterns, and is overridden by _DateFormatPatternField. |
| 34 return pattern; | 34 return pattern; |
| 35 } | 35 } |
| 36 | 36 |
| 37 abstract void parse(_Stream input, _DateBuilder dateFields); | 37 /** Abstract method for subclasses to implementing parsing for their format.*/ |
| 38 void parse(_Stream input, _DateBuilder dateFields); |
| 38 | 39 |
| 39 /** Parse a literal field. We just look for the exact input. */ | 40 /** Parse a literal field. We just look for the exact input. */ |
| 40 void parseLiteral(_Stream input) { | 41 void parseLiteral(_Stream input) { |
| 41 var found = input.read(width); | 42 var found = input.read(width); |
| 42 if (found != pattern) { | 43 if (found != pattern) { |
| 43 throwFormatException(input); | 44 throwFormatException(input); |
| 44 } | 45 } |
| 45 } | 46 } |
| 46 | 47 |
| 47 /** Throw a format exception with an error message indicating the position.*/ | 48 /** Throw a format exception with an error message indicating the position.*/ |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 var basicString = toBePrinted.toString(); | 395 var basicString = toBePrinted.toString(); |
| 395 if (basicString.length >= width) return basicString; | 396 if (basicString.length >= width) return basicString; |
| 396 var buffer = new StringBuffer(); | 397 var buffer = new StringBuffer(); |
| 397 for (var i = 0; i < width - basicString.length; i++) { | 398 for (var i = 0; i < width - basicString.length; i++) { |
| 398 buffer.add('0'); | 399 buffer.add('0'); |
| 399 } | 400 } |
| 400 buffer.add(basicString); | 401 buffer.add(basicString); |
| 401 return buffer.toString(); | 402 return buffer.toString(); |
| 402 } | 403 } |
| 403 } | 404 } |
| OLD | NEW |