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 the locale code with which the message is to be |
117 * formatted (such as en-US). | 117 * formatted (such as en-CA). |
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 = '$_y$_MMMMd'; |
152 static final int _mediumDate = 2; | 159 static final String _longDate = _yMMMEd; |
153 static final int _shortDate = 3; | 160 static final String _mediumDate = '$_y$_Md'; |
154 static final int _fullTime = 4; | 161 static final String _shortDate = _Md; |
155 static final int _longTime = 5; | 162 static final String _fullTime = '$_Hms a'; |
156 static final int _mediumTime = 6; | 163 static final String _longTime = '$_Hms a zzzz'; |
157 static final int _shortTime = 7; | 164 static final String _mediumTime = _Hms; |
158 static final int _fullDateTime = 8; | 165 static final String _shortTime = _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 | |
166 * constant and the parsing was lazy. | |
167 */ | 173 */ |
168 DateTimeFormat.fullDate() : this.formatDefinition = _fullDate; | 174 DateFormat.Hm([this._locale]) : this.formatDefinition = _Hm; |
Jennifer Messerly
2012/06/05 01:28:55
minor nit: you shouldn't need "this." initializer
| |
169 DateTimeFormat.longDate() : this.formatDefinition = _longDate; | 175 DateFormat.Hms([this._locale]) : this.formatDefinition = _Hms; |
170 DateTimeFormat.mediumDate() : this.formatDefinition = _mediumDate; | 176 DateFormat.M([this._locale]) : this.formatDefinition = _M; |
171 DateTimeFormat.shortDate() : this.formatDefinition = _shortDate; | 177 DateFormat.MEd([this._locale]) : this.formatDefinition = _MEd; |
172 DateTimeFormat.fullTime() : this.formatDefinition = _fullTime; | 178 DateFormat.MMM([this._locale]) : this.formatDefinition = _MMM; |
173 DateTimeFormat.longTime() : this.formatDefinition = _longTime; | 179 DateFormat.MMMEd([this._locale]) : this.formatDefinition = _MMMEd; |
174 DateTimeFormat.mediumTime() : this.formatDefinition = _mediumTime; | 180 DateFormat.MMMMEd([this._locale]) : this.formatDefinition = _MMMMEd; |
175 DateTimeFormat.shortTime() : this.formatDefinition = _shortTime; | 181 DateFormat.MMMMd([this._locale]) : this.formatDefinition = _MMMMd; |
176 DateTimeFormat.fullDateTime() : this.formatDefinition = _fullDateTime; | 182 DateFormat.MMMd([this._locale]) : this.formatDefinition = _MMMd; |
177 DateTimeFormat.longDateTime() : this.formatDefinition = _longDateTime; | 183 DateFormat.Md([this._locale]) : this.formatDefinition = _Md; |
178 DateTimeFormat.mediumDateTime() : this.formatDefinition = _mediumDateTime; | 184 DateFormat.d([this._locale]) : this.formatDefinition = _d; |
179 DateTimeFormat.shortDateTime() : this.formatDefinition = _shortDateTime; | 185 DateFormat.hm([this._locale]) : this.formatDefinition = _hm; |
186 DateFormat.ms([this._locale]) : this.formatDefinition = _ms; | |
187 DateFormat.y([this._locale]) : this.formatDefinition = _y; | |
188 DateFormat.yM([this._locale]) : this.formatDefinition = _yM; | |
189 DateFormat.yMEd([this._locale]) : this.formatDefinition = _yMEd; | |
190 DateFormat.yMMM([this._locale]) : this.formatDefinition = _yMMM; | |
191 DateFormat.yMMMEd([this._locale]) : this.formatDefinition = _yMMMEd; | |
192 DateFormat.yMMMM([this._locale]) : this.formatDefinition = _yMMMM; | |
193 DateFormat.yQ([this._locale]) : this.formatDefinition = _yQ; | |
194 DateFormat.yQQQ([this._locale]) : this.formatDefinition = _yQQQ; | |
195 | |
196 DateFormat.fullDate([this._locale]) : this.formatDefinition = _fullDate; | |
197 DateFormat.longDate([this._locale]) : this.formatDefinition = _longDate; | |
198 DateFormat.mediumDate([this._locale]) : this.formatDefinition = _mediumDate; | |
199 DateFormat.shortDate([this._locale]) : this.formatDefinition = _shortDate; | |
200 DateFormat.fullTime([this._locale]) : this.formatDefinition = _fullTime; | |
201 DateFormat.longTime([this._locale]) : this.formatDefinition = _longTime; | |
202 DateFormat.mediumTime([this._locale]) : this.formatDefinition = _mediumTime; | |
203 DateFormat.shortTime([this._locale]) : this.formatDefinition = _shortTime; | |
204 DateFormat.fullDateTime([this._locale]) : | |
205 this.formatDefinition = _fullDateTime; | |
206 DateFormat.longDateTime([this._locale]) : | |
207 this.formatDefinition = _longDateTime; | |
208 DateFormat.mediumDateTime([this._locale]) : | |
209 this.formatDefinition = _mediumDateTime; | |
210 DateFormat.shortDateTime([this._locale]) : | |
211 this.formatDefinition = _shortDateTime; | |
180 | 212 |
181 /** | 213 /** |
182 * Constructor accepts a [formatDefinition], which can be a String, one of the | 214 * 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 | 215 * 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 | 216 * 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 | 217 * 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). | 218 * Dart is running on the client, we can infer from the browser). |
187 */ | 219 */ |
188 DateTimeFormat(this.formatDefinition, [this._locale]); | 220 DateFormat(this.formatDefinition, [this._locale]); |
189 | 221 |
190 /** | 222 /** |
191 * Given user input, attempt to parse the [inputString] into the anticipated | 223 * Given user input, attempt to parse the [inputString] into the anticipated |
192 * format. | 224 * format. |
193 */ | 225 */ |
194 String parse(String inputString) { | 226 String parse(String inputString) { |
195 return inputString; | 227 return inputString; |
196 } | 228 } |
197 | 229 |
198 /** | 230 /** |
199 * Format the given [date] object according to preset pattern and current | 231 * Format the given [date] object according to preset pattern and current |
200 * locale and return a formated string for the given date. | 232 * locale and return a formated string for the given date. |
201 */ | 233 */ |
202 String format(Date date, [TimeZone timeZone]) { | 234 String format(Date date, [TimeZone timeZone]) { |
203 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... | 235 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... |
204 return date.toString(); | 236 return date.toString(); |
205 } | 237 } |
238 | |
239 /** | |
240 * Returns a date string indicating how long ago (3 hours, 2 minutes) | |
241 * something has happened or how long in the future something will happen | |
242 * given a [reference] Date relative to the current time. | |
243 */ | |
244 String formatDuration(Date reference) { | |
245 return ''; | |
246 } | |
247 | |
248 /** | |
249 * Formats a String indicating how long ago (negative [duration]) or how far | |
250 * in the future (positive [duration]) some time is with respect to a | |
251 * reference [date]. The [duration] is expressed in miliseconds. | |
252 */ | |
253 String formatDurationFrom(num duration, Date date) { | |
254 return ''; | |
255 } | |
206 } | 256 } |
OLD | NEW |