OLD | NEW |
---|---|
1 /** | 1 /** |
2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 * Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
3 * for details. All rights reserved. Use of this source code is governed by a | 3 * for details. All rights reserved. Use of this source code is governed by a |
4 * BSD-style license that can be found in the LICENSE file. | 4 * BSD-style license that can be found in the LICENSE file. |
5 * | 5 * |
6 * DateTimeFormat is for formatting and parsing dates in a locale-sensitive | 6 * DateFormat is for formatting and parsing dates in a locale-sensitive |
7 * manner. | 7 * manner. |
8 * It allows the user to choose from a set of standard date time formats as well | 8 * It allows the user to choose from a set of standard date time formats as well |
9 * as specify a customized pattern under certain locales. Date elements that | 9 * as specify a customized pattern under certain locales. Date elements that |
10 * vary across locales include month name, weekname, field, order, etc. | 10 * vary across locales include month name, weekname, field, order, etc. |
11 * //TODO(efortuna): Customized pattern system -- suggested by i18n needs | 11 * //TODO(efortuna): Customized pattern system -- suggested by i18n needs |
12 * // feedback on appropriateness. | 12 * // feedback on appropriateness. |
13 * We also allow the user to use any customized pattern to parse or format | 13 * We also allow the user to use any customized pattern to parse or format |
14 * date-time strings under certain locales. Date elements that vary across | 14 * date-time strings under certain locales. Date elements that vary across |
15 * locales include month name, weekname, field, order, etc. | 15 * locales include month name, weekname, field, order, etc. |
16 * | 16 * |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 * runs are parsed specially. For example, the format "HHmmss" parses the input | 98 * runs are parsed specially. For example, the format "HHmmss" parses the input |
99 * text "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and | 99 * text "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and |
100 * fails to parse "1234". In other words, the leftmost field of the run is | 100 * fails to parse "1234". In other words, the leftmost field of the run is |
101 * flexible, while the others keep a fixed width. If the parse fails anywhere in | 101 * flexible, while the others keep a fixed width. If the parse fails anywhere in |
102 * the run, then the leftmost field is shortened by one character, and the | 102 * the run, then the leftmost field is shortened by one character, and the |
103 * entire run is parsed again. This is repeated until either the parse succeeds | 103 * entire run is parsed again. This is repeated until either the parse succeeds |
104 * or the leftmost field is one character in length. If the parse still fails at | 104 * or the leftmost field is one character in length. If the parse still fails at |
105 * that point, the parse of the run fails. | 105 * that point, the parse of the run fails. |
106 */ | 106 */ |
107 | 107 |
108 #library('DateTimeFormat'); | 108 #library('DateFormat'); |
109 | 109 |
110 class DateTimeFormat { | 110 class DateFormat { |
111 | 111 |
112 /** Definition of this object formats dates. */ | 112 /** Definition of this object formats dates. */ |
113 var formatDefinition; | 113 var formatDefinition; |
114 | 114 |
115 /** | 115 /** |
116 * String indicating a language code with which the message is to be | 116 * String indicating a language code with which the message is to be |
117 * formatted (such as en-US). | 117 * formatted (such as en). |
Alan Knight
2012/06/04 23:59:00
I think this is still not right. It's the locale,
| |
118 */ | 118 */ |
119 String _locale; | 119 String _locale; |
120 | 120 |
121 /** | 121 /** |
122 * Date/Time format "skeleton" patterns. Also specifiable by String, but | 122 * Date/Time format "skeleton" patterns. Also specifiable by String, but |
123 * written this way so that they can be discoverable via autocomplete. | 123 * written this way so that they can be discoverable via autocomplete. These |
124 * follow the ICU syntax described at the top of the file. These skeletons can | |
125 * be combined and we will attempt to find the best format for each locale | |
126 * given the pattern. | |
127 * // TODO(efortuna): Hear back from i18n about Time Zones and the "core set" | |
128 * // of skeleton patterns. | |
124 */ | 129 */ |
125 static final String Hm = 'Hm'; // HH:mm | 130 // Example of how this looks in the US |
126 static final String Hms = 'Hms'; // HH:mm:ss | 131 // locale. |
127 static final String M = 'M'; // L | 132 static final String _Hm = 'Hm'; // HH:mm |
128 static final String MEd = 'MEd'; // E, M/d | 133 static final String _Hms = 'Hms'; // HH:mm:ss |
129 static final String MMM = 'MMM'; // LLL | 134 static final String _M = 'M'; // L |
130 static final String MMMEd = 'MMMEd'; // E, MMM d | 135 static final String _MEd = 'MEd'; // E, M/d |
131 static final String MMMMEd = 'MMMMEd'; // E, MMMM d | 136 static final String _MMM = 'MMM'; // LLL |
132 static final String MMMMd = 'MMMMd'; // MMMM d | 137 static final String _MMMEd = 'MMMEd'; // E, MMM d |
133 static final String MMMd = 'MMMd'; // MMM d | 138 static final String _MMMMEd = 'MMMMEd'; // E, MMMM d |
134 static final String Md = 'Md'; // M/d | 139 static final String _MMMMd = 'MMMMd'; // MMMM d |
135 static final String d = 'd'; // d | 140 static final String _MMMd = 'MMMd'; // MMM d |
136 static final String hm = 'hm'; // h:mm a | 141 static final String _Md = 'Md'; // M/d |
137 static final String ms = 'ms'; // mm:ss | 142 static final String _d = 'd'; // d |
138 static final String y = 'y'; // yyyy | 143 static final String _hm = 'hm'; // h:mm a |
139 static final String yM = 'yM'; // M/yyyy | 144 static final String _ms = 'ms'; // mm:ss |
140 static final String yMEd = 'yMEd'; // EEE, M/d/yyyy | 145 static final String _y = 'y'; // yyyy |
141 static final String yMMM = 'yMMM'; // MMM yyyy | 146 static final String _yM = 'yM'; // M/yyyy |
142 static final String yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy | 147 static final String _yMEd = 'yMEd'; // EEE, M/d/yyyy |
143 static final String yMMMM = 'yMMMM'; // MMMM yyyy | 148 static final String _yMMM = 'yMMM'; // MMM yyyy |
144 static final String yQ = 'yQ'; // Q yyyy | 149 static final String _yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy |
145 static final String yQQQ = 'yQQQ'; // QQQ yyyy | 150 static final String _yMMMM = 'yMMMM'; // MMMM yyyy |
151 static final String _yQ = 'yQ'; // Q yyyy | |
152 static final String _yQQQ = 'yQQQ'; // QQQ yyyy | |
146 | 153 |
147 /** Date/Time format patterns. */ | 154 /** Date/Time format patterns. */ |
148 // TODO(alanknight): There's a style question of whether to use fullDate or | 155 // TODO(efortuna): This are just guesses of what a full date, long date is. |
149 // FULL_DATE naming conventions. | 156 // Do the proper homework on ICU to find the proper set "Hms"/"yMMd" |
150 static final int _fullDate = 0; | 157 // applicable to each case. |
151 static final int _longDate = 1; | 158 static final String _fullDate = DateFormat._y + DateFormat._MMMMd; |
Alan Knight
2012/06/04 23:59:00
I thought we don't have String + any more.
Emily Fortuna
2012/06/05 00:21:50
Yup. old habits die hard. Fixed.
| |
152 static final int _mediumDate = 2; | 159 static final String _longDate = DateFormat._yMMMEd; |
153 static final int _shortDate = 3; | 160 static final String _mediumDate = DateFormat._y + DateFormat._Md; |
Alan Knight
2012/06/04 23:59:00
Couldn't we just write these as e.g. _y + _Md, sin
Emily Fortuna
2012/06/05 00:21:50
Duh! fixed!
| |
154 static final int _fullTime = 4; | 161 static final String _shortDate = DateFormat._Md; |
155 static final int _longTime = 5; | 162 static final String _fullTime = DateFormat._Hms + 'a'; |
156 static final int _mediumTime = 6; | 163 static final String _longTime = DateFormat._Hms + 'a zzzz'; |
157 static final int _shortTime = 7; | 164 static final String _mediumTime = DateFormat._Hms; |
158 static final int _fullDateTime = 8; | 165 static final String _shortTime = DateFormat._Hm; |
159 static final int _longDateTime = 9; | 166 static final String _fullDateTime = _fullDate + _fullTime; |
160 static final int _mediumDateTime = 10; | 167 static final String _longDateTime = _logDate + _longTime; |
161 static final int _shortDateTime = 11; | 168 static final String _mediumDateTime = _mediumDate + _mediumTime; |
169 static final String _shortDateTime = _shortDate + _shortTime; | |
162 | 170 |
163 /** | 171 /** |
164 * Named constructors for each of the above values. | 172 * Named constructors for the above values. |
165 * These could probably be made shorter if we just set the format to the | 173 * These could probably be made shorter if we just set the format to the |
166 * constant and the parsing was lazy. | 174 * constant and the parsing was lazy. |
Alan Knight
2012/06/04 23:59:00
I think the last two lines of this comment are obs
Emily Fortuna
2012/06/05 00:21:50
Done.
| |
167 */ | 175 */ |
168 DateTimeFormat.fullDate() : this.formatDefinition = _fullDate; | 176 DateFormat.Hm() : this.formatDefinition = DateFormat._Hm; |
169 DateTimeFormat.longDate() : this.formatDefinition = _longDate; | 177 DateFormat.Hms() : this.formatDefinition = DateFormat._Hms; |
170 DateTimeFormat.mediumDate() : this.formatDefinition = _mediumDate; | 178 DateFormat.M() : this.formatDefinition = DateFormat._M; |
171 DateTimeFormat.shortDate() : this.formatDefinition = _shortDate; | 179 DateFormat.MEd() : this.formatDefinition = DateFormat._MEd; |
172 DateTimeFormat.fullTime() : this.formatDefinition = _fullTime; | 180 DateFormat.MMM() : this.formatDefinition = DateFormat._MMM; |
173 DateTimeFormat.longTime() : this.formatDefinition = _longTime; | 181 DateFormat.MMMEd() : this.formatDefinition = DateFormat._MMMEd; |
174 DateTimeFormat.mediumTime() : this.formatDefinition = _mediumTime; | 182 DateFormat.MMMMEd() : this.formatDefinition = DateFormat._MMMMEd; |
175 DateTimeFormat.shortTime() : this.formatDefinition = _shortTime; | 183 DateFormat.MMMMd() : this.formatDefinition = DateFormat._MMMMd; |
176 DateTimeFormat.fullDateTime() : this.formatDefinition = _fullDateTime; | 184 DateFormat.MMMd() : this.formatDefinition = DateFormat._MMMd; |
177 DateTimeFormat.longDateTime() : this.formatDefinition = _longDateTime; | 185 DateFormat.Md() : this.formatDefinition = DateFormat._Md; |
178 DateTimeFormat.mediumDateTime() : this.formatDefinition = _mediumDateTime; | 186 DateFormat.d() : this.formatDefinition = DateFormat._d; |
179 DateTimeFormat.shortDateTime() : this.formatDefinition = _shortDateTime; | 187 DateFormat.hm() : this.formatDefinition = DateFormat._hm; |
188 DateFormat.ms() : this.formatDefinition = DateFormat._ms; | |
189 DateFormat.y() : this.formatDefinition = DateFormat._y; | |
190 DateFormat.yM() : this.formatDefinition = DateFormat._yM; | |
191 DateFormat.yMEd() : this.formatDefinition = DateFormat._yMEd; | |
192 DateFormat.yMMM() : this.formatDefinition = DateFormat._yMMM; | |
193 DateFormat.yMMMEd() : this.formatDefinition = DateFormat._yMMMEd; | |
194 DateFormat.yMMMM() : this.formatDefinition = DateFormat._yMMMM; | |
195 DateFormat.yQ() : this.formatDefinition = DateFormat._yQ; | |
196 DateFormat.yQQQ() : this.formatDefinition = DateFormat._yQQQ; | |
197 | |
198 DateFormat.fullDate() : this.formatDefinition = _fullDate; | |
199 DateFormat.longDate() : this.formatDefinition = _longDate; | |
200 DateFormat.mediumDate() : this.formatDefinition = _mediumDate; | |
201 DateFormat.shortDate() : this.formatDefinition = _shortDate; | |
202 DateFormat.fullTime() : this.formatDefinition = _fullTime; | |
203 DateFormat.longTime() : this.formatDefinition = _longTime; | |
204 DateFormat.mediumTime() : this.formatDefinition = _mediumTime; | |
205 DateFormat.shortTime() : this.formatDefinition = _shortTime; | |
206 DateFormat.fullDateTime() : this.formatDefinition = _fullDateTime; | |
207 DateFormat.longDateTime() : this.formatDefinition = _longDateTime; | |
208 DateFormat.mediumDateTime() : this.formatDefinition = _mediumDateTime; | |
209 DateFormat.shortDateTime() : this.formatDefinition = _shortDateTime; | |
180 | 210 |
181 /** | 211 /** |
182 * Constructor accepts a [formatDefinition], which can be a String, one of the | 212 * Constructor accepts a [formatDefinition], which can be a String, one of the |
183 * predefined static forms, or a custom date format using the syntax described | 213 * predefined static forms, or a custom date format using the syntax described |
184 * above. An optional [_locale] can be provided for specifics of the language | 214 * above. An optional [_locale] can be provided for specifics of the language |
185 * locale to be used, otherwise, we will attempt to infer it (acceptable if | 215 * locale to be used, otherwise, we will attempt to infer it (acceptable if |
186 * Dart is running on the client, we can infer from the browser). | 216 * Dart is running on the client, we can infer from the browser). |
187 */ | 217 */ |
188 DateTimeFormat(this.formatDefinition, [this._locale]); | 218 DateFormat(this.formatDefinition, [this._locale]); |
189 | 219 |
190 /** | 220 /** |
191 * Given user input, attempt to parse the [inputString] into the anticipated | 221 * Given user input, attempt to parse the [inputString] into the anticipated |
192 * format. | 222 * format. |
193 */ | 223 */ |
194 String parse(String inputString) { | 224 String parse(String inputString) { |
195 return inputString; | 225 return inputString; |
196 } | 226 } |
197 | 227 |
198 /** | 228 /** |
199 * Format the given [date] object according to preset pattern and current | 229 * Format the given [date] object according to preset pattern and current |
200 * locale and return a formated string for the given date. | 230 * locale and return a formated string for the given date. |
201 */ | 231 */ |
202 String format(Date date, [TimeZone timeZone]) { | 232 String format(Date date, [TimeZone timeZone]) { |
203 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... | 233 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... |
204 return date.toString(); | 234 return date.toString(); |
205 } | 235 } |
206 } | 236 } |
OLD | NEW |