| 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 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 |
| 11 * manner. | 11 * manner. |
| 12 * It allows the user to choose from a set of standard date time formats as well | 12 * It allows the user to choose from a set of standard date time formats as well |
| 13 * as specify a customized pattern under certain locales. Date elements that | 13 * as specify a customized pattern under certain locales. Date elements that |
| 14 * vary across locales include month name, week name, field order, etc. | 14 * vary across locales include month name, week name, field order, etc. |
| 15 * We also allow the user to use any customized pattern to parse or format | 15 * We also allow the user to use any customized pattern to parse or format |
| 16 * date-time strings under certain locales. Date elements that vary across | 16 * date-time strings under certain locales. Date elements that vary across |
| 17 * locales include month name, weekname, field, order, etc. | 17 * locales include month name, weekname, field, order, etc. |
| 18 * | 18 * |
| 19 * The actual date for the locales must be obtained. This can currently be done | 19 * Formatting dates in the default "en_US" format does not require any |
| 20 * in one of three ways, determined by which library you import. If you only | 20 * initialization. e.g. |
| 21 * want to use en_US formatting you can use it directly, as a copy of that | 21 * print(new DateFormat.yMMMd().format(new Date.now())); |
| 22 * locale is hard-coded into the formatter. In all other cases, | 22 * |
| 23 * the [initializeDateFormatting] method must be called and will return a future | 23 * But for other locales, the formatting data for the locale must be |
| 24 * obtained. This can currently be done |
| 25 * in one of three ways, determined by which library you import. In all cases, |
| 26 * the "initializeDateFormatting" method must be called and will return a future |
| 24 * that is complete once the locale data is available. The result of the future | 27 * that is complete once the locale data is available. The result of the future |
| 25 * isn't important, but the data for that locale is available to the date | 28 * isn't important, but the data for that locale is available to the date |
| 26 * formatting and parsing once it completes. | 29 * formatting and parsing once it completes. |
| 27 * | 30 * |
| 28 * The easiest option is that the data may be available locally, imported in a | 31 * The easiest option is that the data may be available locally, imported in a |
| 29 * library that contains data for all the locales. | 32 * library that contains data for all the locales. |
| 30 * import 'package:intl/date_symbol_data_local.dart'; | 33 * import 'package:intl/date_symbol_data_local.dart'; |
| 31 * initializeDateFormatting("en_US", null).then((_) => runMyCode()); | 34 * initializeDateFormatting("fr_FR", null).then((_) => runMyCode()); |
| 32 * | 35 * |
| 33 * If we are running outside of a browser, we may want to read the data | 36 * If we are running outside of a browser, we may want to read the data |
| 34 * from files in the file system. | 37 * from files in the file system. |
| 35 * import 'package:intl/date_symbol_data_file.dart'; | 38 * import 'package:intl/date_symbol_data_file.dart'; |
| 36 * initializeDateFormatting("de_DE", null).then((_) => runMyCode()); | 39 * initializeDateFormatting("de_DE", null).then((_) => runMyCode()); |
| 37 * | 40 * |
| 38 * If we are running in a browser, we may want to read the data from the | 41 * If we are running in a browser, we may want to read the data from the |
| 39 * server using the XmlHttpRequest mechanism. | 42 * server using the XmlHttpRequest mechanism. |
| 40 * import 'package:intl/date_symbol_data_http_request.dart'; | 43 * import 'package:intl/date_symbol_data_http_request.dart'; |
| 41 * initializeDateFormatting("pt_BR", null).then((_) => runMyCode()); | 44 * initializeDateFormatting("pt_BR", null).then((_) => runMyCode()); |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 */ | 469 */ |
| 467 _useDefaultPattern() { | 470 _useDefaultPattern() { |
| 468 add_yMMMMd(); | 471 add_yMMMMd(); |
| 469 add_jms(); | 472 add_jms(); |
| 470 } | 473 } |
| 471 | 474 |
| 472 /** | 475 /** |
| 473 * A series of regular expressions used to parse a format string into its | 476 * A series of regular expressions used to parse a format string into its |
| 474 * component fields. | 477 * component fields. |
| 475 */ | 478 */ |
| 476 static List<Pattern> _matchers = [ | 479 static List<RegExp> _matchers = [ |
| 477 // Quoted String - anything between single quotes, with escaping | 480 // Quoted String - anything between single quotes, with escaping |
| 478 // of single quotes by doubling them. | 481 // of single quotes by doubling them. |
| 479 // e.g. in the pattern "hh 'o''clock'" will match 'o''clock' | 482 // e.g. in the pattern "hh 'o''clock'" will match 'o''clock' |
| 480 new RegExp("^\'(?:[^\']|\'\')*\'"), | 483 new RegExp("^\'(?:[^\']|\'\')*\'"), |
| 481 // Fields - any sequence of 1 or more of the same field characters. | 484 // Fields - any sequence of 1 or more of the same field characters. |
| 482 // e.g. in "hh:mm:ss" will match hh, mm, and ss. But in "hms" would | 485 // e.g. in "hh:mm:ss" will match hh, mm, and ss. But in "hms" would |
| 483 // match each letter individually. | 486 // match each letter individually. |
| 484 new RegExp( | 487 new RegExp( |
| 485 "^(?:G+|y+|M+|k+|S+|E+|a+|h+|K+|H+|c+|L+|Q+|d+|m+|s+|v+|z+|Z+)"), | 488 "^(?:G+|y+|M+|k+|S+|E+|a+|h+|K+|H+|c+|L+|Q+|d+|m+|s+|v+|z+|Z+)"), |
| 486 // Everything else - A sequence that is not quotes or field characters. | 489 // Everything else - A sequence that is not quotes or field characters. |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 578 _DateFormatField _match(String pattern) { | 581 _DateFormatField _match(String pattern) { |
| 579 for (var i = 0; i < _matchers.length; i++) { | 582 for (var i = 0; i < _matchers.length; i++) { |
| 580 var regex = _matchers[i]; | 583 var regex = _matchers[i]; |
| 581 var match = regex.firstMatch(pattern); | 584 var match = regex.firstMatch(pattern); |
| 582 if (match != null) { | 585 if (match != null) { |
| 583 return _fieldConstructors[i](match.group(0), this); | 586 return _fieldConstructors[i](match.group(0), this); |
| 584 } | 587 } |
| 585 } | 588 } |
| 586 } | 589 } |
| 587 } | 590 } |
| OLD | NEW |