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