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 */ |
130 // Example of how this looks in the US | |
131 // locale. | |
125 static final String Hm = 'Hm'; // HH:mm | 132 static final String Hm = 'Hm'; // HH:mm |
126 static final String Hms = 'Hms'; // HH:mm:ss | 133 static final String Hms = 'Hms'; // HH:mm:ss |
127 static final String M = 'M'; // L | 134 static final String M = 'M'; // L |
128 static final String MEd = 'MEd'; // E, M/d | 135 static final String MEd = 'MEd'; // E, M/d |
129 static final String MMM = 'MMM'; // LLL | 136 static final String MMM = 'MMM'; // LLL |
130 static final String MMMEd = 'MMMEd'; // E, MMM d | 137 static final String MMMEd = 'MMMEd'; // E, MMM d |
131 static final String MMMMEd = 'MMMMEd'; // E, MMMM d | 138 static final String MMMMEd = 'MMMMEd'; // E, MMMM d |
132 static final String MMMMd = 'MMMMd'; // MMMM d | 139 static final String MMMMd = 'MMMMd'; // MMMM d |
133 static final String MMMd = 'MMMd'; // MMM d | 140 static final String MMMd = 'MMMd'; // MMM d |
134 static final String Md = 'Md'; // M/d | 141 static final String Md = 'Md'; // M/d |
135 static final String d = 'd'; // d | 142 static final String d = 'd'; // d |
136 static final String hm = 'hm'; // h:mm a | 143 static final String hm = 'hm'; // h:mm a |
137 static final String ms = 'ms'; // mm:ss | 144 static final String ms = 'ms'; // mm:ss |
138 static final String y = 'y'; // yyyy | 145 static final String y = 'y'; // yyyy |
139 static final String yM = 'yM'; // M/yyyy | 146 static final String yM = 'yM'; // M/yyyy |
140 static final String yMEd = 'yMEd'; // EEE, M/d/yyyy | 147 static final String yMEd = 'yMEd'; // EEE, M/d/yyyy |
141 static final String yMMM = 'yMMM'; // MMM yyyy | 148 static final String yMMM = 'yMMM'; // MMM yyyy |
142 static final String yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy | 149 static final String yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy |
143 static final String yMMMM = 'yMMMM'; // MMMM yyyy | 150 static final String yMMMM = 'yMMMM'; // MMMM yyyy |
144 static final String yQ = 'yQ'; // Q yyyy | 151 static final String yQ = 'yQ'; // Q yyyy |
145 static final String yQQQ = 'yQQQ'; // QQQ 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]) : formatDefinition = Hm; |
169 DateTimeFormat.longDate() : this.formatDefinition = _longDate; | 175 DateFormat.Hms([this._locale]) : formatDefinition = Hms; |
170 DateTimeFormat.mediumDate() : this.formatDefinition = _mediumDate; | 176 DateFormat.M([this._locale]) : formatDefinition = M; |
171 DateTimeFormat.shortDate() : this.formatDefinition = _shortDate; | 177 DateFormat.MEd([this._locale]) : formatDefinition = MEd; |
172 DateTimeFormat.fullTime() : this.formatDefinition = _fullTime; | 178 DateFormat.MMM([this._locale]) : formatDefinition = MMM; |
173 DateTimeFormat.longTime() : this.formatDefinition = _longTime; | 179 DateFormat.MMMEd([this._locale]) : formatDefinition = MMMEd; |
174 DateTimeFormat.mediumTime() : this.formatDefinition = _mediumTime; | 180 DateFormat.MMMMEd([this._locale]) : formatDefinition = MMMMEd; |
175 DateTimeFormat.shortTime() : this.formatDefinition = _shortTime; | 181 DateFormat.MMMMd([this._locale]) : formatDefinition = MMMMd; |
176 DateTimeFormat.fullDateTime() : this.formatDefinition = _fullDateTime; | 182 DateFormat.MMMd([this._locale]) : formatDefinition = MMMd; |
177 DateTimeFormat.longDateTime() : this.formatDefinition = _longDateTime; | 183 DateFormat.Md([this._locale]) : formatDefinition = Md; |
178 DateTimeFormat.mediumDateTime() : this.formatDefinition = _mediumDateTime; | 184 DateFormat.d([this._locale]) : formatDefinition = d; |
179 DateTimeFormat.shortDateTime() : this.formatDefinition = _shortDateTime; | 185 DateFormat.hm([this._locale]) : formatDefinition = hm; |
186 DateFormat.ms([this._locale]) : formatDefinition = ms; | |
187 DateFormat.y([this._locale]) : formatDefinition = y; | |
188 DateFormat.yM([this._locale]) : formatDefinition = yM; | |
189 DateFormat.yMEd([this._locale]) : formatDefinition = yMEd; | |
190 DateFormat.yMMM([this._locale]) : formatDefinition = yMMM; | |
191 DateFormat.yMMMEd([this._locale]) : formatDefinition = yMMMEd; | |
192 DateFormat.yMMMM([this._locale]) : formatDefinition = yMMMM; | |
193 DateFormat.yQ([this._locale]) : formatDefinition = yQ; | |
194 DateFormat.yQQQ([this._locale]) : formatDefinition = yQQQ; | |
195 | |
196 DateFormat.fullDate([this._locale]) : formatDefinition = fullDate; | |
197 DateFormat.longDate([this._locale]) : formatDefinition = longDate; | |
198 DateFormat.mediumDate([this._locale]) : formatDefinition = mediumDate; | |
199 DateFormat.shortDate([this._locale]) : formatDefinition = shortDate; | |
200 DateFormat.fullTime([this._locale]) : formatDefinition = fullTime; | |
201 DateFormat.longTime([this._locale]) : formatDefinition = longTime; | |
202 DateFormat.mediumTime([this._locale]) : formatDefinition = mediumTime; | |
203 DateFormat.shortTime([this._locale]) : formatDefinition = shortTime; | |
204 DateFormat.fullDateTime([this._locale]) : formatDefinition = fullDateTime; | |
205 DateFormat.longDateTime([this._locale]) : formatDefinition = longDateTime; | |
206 DateFormat.mediumDateTime([this._locale]) : formatDefinition = mediumDateTime; | |
207 DateFormat.shortDateTime([this._locale]) : formatDefinition = shortDateTime; | |
180 | 208 |
181 /** | 209 /** |
182 * Constructor accepts a [formatDefinition], which can be a String, one of the | 210 * 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 | 211 * 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 | 212 * 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 | 213 * 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). | 214 * Dart is running on the client, we can infer from the browser). |
187 */ | 215 */ |
188 DateTimeFormat(this.formatDefinition, [this._locale]); | 216 DateFormat([this.formatDefinition = fullDate, this._locale]); |
189 | 217 |
190 /** | 218 /** |
191 * Given user input, attempt to parse the [inputString] into the anticipated | 219 * Given user input, attempt to parse the [inputString] into the anticipated |
192 * format. | 220 * format. |
193 */ | 221 */ |
194 String parse(String inputString) { | 222 String parse(String inputString) { |
195 return inputString; | 223 return inputString; |
196 } | 224 } |
197 | 225 |
198 /** | 226 /** |
199 * Format the given [date] object according to preset pattern and current | 227 * Format the given [date] object according to preset pattern and current |
200 * locale and return a formated string for the given date. | 228 * locale and return a formated string for the given date. |
201 */ | 229 */ |
202 String format(Date date, [TimeZone timeZone]) { | 230 String format(Date date, [TimeZone timeZone]) { |
203 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... | 231 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... |
204 return date.toString(); | 232 return date.toString(); |
205 } | 233 } |
234 | |
235 /** | |
236 * Returns a date string indicating how long ago (3 hours, 2 minutes) | |
237 * something has happened or how long in the future something will happen | |
238 * given a [reference] Date relative to the current time. | |
239 */ | |
240 String formatDuration(Date reference) { | |
241 return ''; | |
242 } | |
243 | |
244 /** | |
245 * Formats a String indicating how long ago (negative [duration]) or how far | |
246 * in the future (positive [duration]) some time is with respect to a | |
247 * reference [date]. The [duration] is expressed in miliseconds. | |
Alan Knight
2012/06/05 17:35:52
Really minor nit - milliseconds (two "l"s)
| |
248 */ | |
249 String formatDurationFrom(num duration, Date date) { | |
Alan Knight
2012/06/05 17:35:52
We seem to have a Duration type in core, should we
| |
250 return ''; | |
251 } | |
206 } | 252 } |
OLD | NEW |