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 * |
12 * It allows the user to choose from a set of standard date time formats as well | 13 * 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 | 14 * as specify a customized pattern under certain locales. Date elements that |
14 * vary across locales include month name, week name, field order, etc. | 15 * 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 | 16 * 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 | 17 * date-time strings under certain locales. Date elements that vary across |
17 * locales include month name, weekname, field, order, etc. | 18 * locales include month name, weekname, field, order, etc. |
18 * | 19 * |
19 * Formatting dates in the default "en_US" format does not require any | 20 * Formatting dates in the default "en_US" format does not require any |
20 * initialization. e.g. | 21 * initialization. e.g. |
21 * print(new DateFormat.yMMMd().format(new Date.now())); | 22 * print(new DateFormat.yMMMd().format(new Date.now())); |
(...skipping 23 matching lines...) Expand all Loading... |
45 * | 46 * |
46 * The code in example/basic/basic_example.dart shows a full example of | 47 * The code in example/basic/basic_example.dart shows a full example of |
47 * using this mechanism. | 48 * using this mechanism. |
48 * | 49 * |
49 * Once we have the locale data, we need to specify the particular format. | 50 * Once we have the locale data, we need to specify the particular format. |
50 * This library uses the ICU/JDK date/time pattern specification both for | 51 * This library uses the ICU/JDK date/time pattern specification both for |
51 * complete format specifications and also the abbreviated "skeleton" form | 52 * complete format specifications and also the abbreviated "skeleton" form |
52 * which can also adapt to different locales and is preferred where available. | 53 * which can also adapt to different locales and is preferred where available. |
53 * | 54 * |
54 * Skeletons: These can be specified either as the ICU constant name or as the | 55 * Skeletons: These can be specified either as the ICU constant name or as the |
55 * skeleton to which it resolves. The supported set of skeletons is as follows | 56 * skeleton to which it resolves. The supported set of skeletons is as follows. |
| 57 * For each skeleton there is a named constructor that can be used to create it. |
| 58 * It's also possible to pass the skeleton as a string, but the constructor |
| 59 * is preferred. |
| 60 * |
56 * ICU Name Skeleton | 61 * ICU Name Skeleton |
57 * -------- -------- | 62 * -------- -------- |
58 * DAY d | 63 * DAY d |
59 * ABBR_WEEKDAY E | 64 * ABBR_WEEKDAY E |
60 * WEEKDAY EEEE | 65 * WEEKDAY EEEE |
61 * ABBR_STANDALONE_MONTH LLL | 66 * ABBR_STANDALONE_MONTH LLL |
62 * STANDALONE_MONTH LLLL | 67 * STANDALONE_MONTH LLLL |
63 * NUM_MONTH M | 68 * NUM_MONTH M |
64 * NUM_MONTH_DAY Md | 69 * NUM_MONTH_DAY Md |
65 * NUM_MONTH_WEEKDAY_DAY MEd | 70 * NUM_MONTH_WEEKDAY_DAY MEd |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 * MINUTE_SECOND ms | 102 * MINUTE_SECOND ms |
98 * SECOND s | 103 * SECOND s |
99 * | 104 * |
100 * Examples Using the US Locale: | 105 * Examples Using the US Locale: |
101 * | 106 * |
102 * Pattern Result | 107 * Pattern Result |
103 * ---------------- ------- | 108 * ---------------- ------- |
104 * new DateFormat.yMd() -> 7/10/1996 | 109 * new DateFormat.yMd() -> 7/10/1996 |
105 * new DateFormat("yMd") -> 7/10/1996 | 110 * new DateFormat("yMd") -> 7/10/1996 |
106 * new DateFormat.yMMMMd("en_US") -> July 10, 1996 | 111 * new DateFormat.yMMMMd("en_US") -> July 10, 1996 |
107 * new DateFormat("Hm", "en_US") -> 12:08 PM | 112 * new DateFormat.jm() -> 5:08 PM |
108 * new DateFormat.yMd().add_Hm() -> 7/10/1996 12:08 PM | 113 * new DateFormat.yMd().add_jm() -> 7/10/1996 5:08 PM |
| 114 * new DateFormat.Hm() -> 17:08 // force 24 hour time |
109 * | 115 * |
110 * Explicit Pattern Syntax: Formats can also be specified with a pattern string. | 116 * Explicit Pattern Syntax: Formats can also be specified with a pattern string. |
111 * The skeleton forms will resolve to explicit patterns of this form, but will | 117 * This can be used for formats that don't have a skeleton available, but these |
112 * also adapt to different patterns in different locales. | 118 * will not adapt to different locales. For example, in an explicit pattern the |
113 * The following characters are reserved: | 119 * letters "H" and "h" are available for 24 hour and 12 hour time formats |
| 120 * respectively. But there isn't a way in an explicit pattern to get the |
| 121 * behaviour of the "j" skeleton, which prints 24 hour or 12 hour time according |
| 122 * to the conventions of the locale, and also includes am/pm markers where |
| 123 * appropriate. So it is preferable to use the skeletons. |
| 124 * |
| 125 * The following characters are available in explicit patterns: |
114 * | 126 * |
115 * Symbol Meaning Presentation Example | 127 * Symbol Meaning Presentation Example |
116 * ------ ------- ------------ ------- | 128 * ------ ------- ------------ ------- |
117 * G era designator (Text) AD | 129 * G era designator (Text) AD |
118 * y year (Number) 1996 | 130 * y year (Number) 1996 |
119 * M month in year (Text & Number) July & 07 | 131 * M month in year (Text & Number) July & 07 |
120 * L standalone month (Text & Number) July & 07 | 132 * L standalone month (Text & Number) July & 07 |
121 * d day in month (Number) 10 | 133 * d day in month (Number) 10 |
122 * c standalone day (Number) 10 | 134 * c standalone day (Number) 10 |
123 * h hour in am/pm (1~12) (Number) 12 | 135 * h hour in am/pm (1~12) (Number) 12 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 /** | 205 /** |
194 * Creates a new DateFormat, using the format specified by [newPattern]. For | 206 * Creates a new DateFormat, using the format specified by [newPattern]. For |
195 * forms that match one of our predefined skeletons, we look up the | 207 * forms that match one of our predefined skeletons, we look up the |
196 * corresponding pattern in [locale] (or in the default locale if none is | 208 * corresponding pattern in [locale] (or in the default locale if none is |
197 * specified) and use the resulting full format string. This is the | 209 * specified) and use the resulting full format string. This is the |
198 * preferred usage, but if [newPattern] does not match one of the skeletons, | 210 * preferred usage, but if [newPattern] does not match one of the skeletons, |
199 * then it is used as a format directly, but will not be adapted to suit | 211 * then it is used as a format directly, but will not be adapted to suit |
200 * the locale. | 212 * the locale. |
201 * | 213 * |
202 * For example, in an en_US locale, specifying the skeleton | 214 * For example, in an en_US locale, specifying the skeleton |
203 * new DateFormat('yMEd'); | 215 * new DateFormat.yMEd(); |
204 * or the explicit | 216 * or the explicit |
205 * new DateFormat('EEE, M/d/y'); | 217 * new DateFormat('EEE, M/d/y'); |
206 * would produce the same result, a date of the form | 218 * would produce the same result, a date of the form |
207 * Wed, 6/27/2012 | 219 * Wed, 6/27/2012 |
208 * The first version would produce a different format string if used in | 220 * The first version would produce a different format string if used in |
209 * another locale, but the second format would always be the same. | 221 * another locale, but the second format would always be the same. |
210 * | 222 * |
211 * If [locale] does not exist in our set of supported locales then an | 223 * If [locale] does not exist in our set of supported locales then an |
212 * [ArgumentError] is thrown. | 224 * [ArgumentError] is thrown. |
213 */ | 225 */ |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 * [parse] does. It also does not allow leading whitespace on delimiters, | 287 * [parse] does. It also does not allow leading whitespace on delimiters, |
276 * and does not allow alternative names for months or weekdays other than | 288 * and does not allow alternative names for months or weekdays other than |
277 * those the format knows about. The restrictions are quite arbitrary and | 289 * 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. | 290 * it's not known how well they'll work for locales that aren't English-like. |
279 * | 291 * |
280 * If [inputString] does not parse, this throws a | 292 * If [inputString] does not parse, this throws a |
281 * [FormatException]. | 293 * [FormatException]. |
282 * | 294 * |
283 * For example, this will accept | 295 * For example, this will accept |
284 * | 296 * |
285 * new DateTimeFormat.yMMMd("en_US").parseLoose("SEp 3 2014"); | 297 * new DateFormat.yMMMd("en_US").parseLoose("SEp 3 2014"); |
286 * new DateTimeFormat.yMd("en_US").parseLoose("09 03/2014"); | 298 * new DateFormat.yMd("en_US").parseLoose("09 03/2014"); |
287 * | 299 * |
288 * It will NOT accept | 300 * It will NOT accept |
289 * | 301 * |
290 * // "Sept" is not a valid month name. | 302 * // "Sept" is not a valid month name. |
291 * new DateTimeFormat.yMMMd("en_US").parseLoose("Sept 3, 2014"); | 303 * new DateFormat.yMMMd("en_US").parseLoose("Sept 3, 2014"); |
292 * // Delimiters can't have leading whitespace. | 304 * // Delimiters can't have leading whitespace. |
293 * new DateTimeFormat.yMd("en_US").parseLoose("09 / 03 / 2014"); | 305 * new DateFormat.yMd("en_US").parseLoose("09 / 03 / 2014"); |
294 */ | 306 */ |
295 DateTime parseLoose(String inputString, [utc = false]) { | 307 DateTime parseLoose(String inputString, [utc = false]) { |
296 try { | 308 try { |
297 return _parse(inputString, utc: utc, strict: true); | 309 return _parse(inputString, utc: utc, strict: true); |
298 } on FormatException { | 310 } on FormatException { |
299 return _parseLoose(inputString.toLowerCase(), utc); | 311 return _parseLoose(inputString.toLowerCase(), utc); |
300 } | 312 } |
301 } | 313 } |
302 | 314 |
303 _parseLoose(String inputString, bool utc) { | 315 _parseLoose(String inputString, bool utc) { |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 _DateFormatField _match(String pattern) { | 685 _DateFormatField _match(String pattern) { |
674 for (var i = 0; i < _matchers.length; i++) { | 686 for (var i = 0; i < _matchers.length; i++) { |
675 var regex = _matchers[i]; | 687 var regex = _matchers[i]; |
676 var match = regex.firstMatch(pattern); | 688 var match = regex.firstMatch(pattern); |
677 if (match != null) { | 689 if (match != null) { |
678 return _fieldConstructors[i](match.group(0), this); | 690 return _fieldConstructors[i](match.group(0), this); |
679 } | 691 } |
680 } | 692 } |
681 } | 693 } |
682 } | 694 } |
OLD | NEW |