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). |
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. | |
124 */ | 127 */ |
128 // Example of how this looks in the US | |
129 // locale. | |
125 static final String Hm = 'Hm'; // HH:mm | 130 static final String Hm = 'Hm'; // HH:mm |
126 static final String Hms = 'Hms'; // HH:mm:ss | 131 static final String Hms = 'Hms'; // HH:mm:ss |
127 static final String M = 'M'; // L | 132 static final String M = 'M'; // L |
128 static final String MEd = 'MEd'; // E, M/d | 133 static final String MEd = 'MEd'; // E, M/d |
129 static final String MMM = 'MMM'; // LLL | 134 static final String MMM = 'MMM'; // LLL |
130 static final String MMMEd = 'MMMEd'; // E, MMM d | 135 static final String MMMEd = 'MMMEd'; // E, MMM d |
131 static final String MMMMEd = 'MMMMEd'; // E, MMMM d | 136 static final String MMMMEd = 'MMMMEd'; // E, MMMM d |
132 static final String MMMMd = 'MMMMd'; // MMMM d | 137 static final String MMMMd = 'MMMMd'; // MMMM d |
133 static final String MMMd = 'MMMd'; // MMM d | 138 static final String MMMd = 'MMMd'; // MMM d |
134 static final String Md = 'Md'; // M/d | 139 static final String Md = 'Md'; // M/d |
135 static final String d = 'd'; // d | 140 static final String d = 'd'; // d |
136 static final String hm = 'hm'; // h:mm a | 141 static final String hm = 'hm'; // h:mm a |
137 static final String ms = 'ms'; // mm:ss | 142 static final String ms = 'ms'; // mm:ss |
138 static final String y = 'y'; // yyyy | 143 static final String y = 'y'; // yyyy |
139 static final String yM = 'yM'; // M/yyyy | 144 static final String yM = 'yM'; // M/yyyy |
140 static final String yMEd = 'yMEd'; // EEE, M/d/yyyy | 145 static final String yMEd = 'yMEd'; // EEE, M/d/yyyy |
141 static final String yMMM = 'yMMM'; // MMM yyyy | 146 static final String yMMM = 'yMMM'; // MMM yyyy |
142 static final String yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy | 147 static final String yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy |
143 static final String yMMMM = 'yMMMM'; // MMMM yyyy | 148 static final String yMMMM = 'yMMMM'; // MMMM yyyy |
144 static final String yQ = 'yQ'; // Q yyyy | 149 static final String yQ = 'yQ'; // Q yyyy |
145 static final String yQQQ = 'yQQQ'; // QQQ yyyy | 150 static final String yQQQ = 'yQQQ'; // QQQ yyyy |
146 | 151 |
147 /** Date/Time format patterns. */ | 152 /** Date/Time format patterns. */ |
148 // TODO(alanknight): There's a style question of whether to use fullDate or | 153 static final String _fullDate = DateFormat.y + DateFormat.MMMMd; |
149 // FULL_DATE naming conventions. | 154 static final String _longDate = DateFormat.yMMMEd; |
150 static final int _fullDate = 0; | 155 static final String _mediumDate = DateFormat.y + DateFormat.Md; |
151 static final int _longDate = 1; | 156 static final String _shortDate = DateFormat.Md; |
152 static final int _mediumDate = 2; | 157 static final String _fullTime = DateFormat.Hms + 'a'; |
153 static final int _shortDate = 3; | 158 static final String _longTime = DateFormat.Hms + 'a zzzz'; |
Emily Fortuna
2012/06/04 22:48:48
This will probably be fixed up after we have our m
| |
154 static final int _fullTime = 4; | 159 static final String _mediumTime = DateFormat.Hms; |
155 static final int _longTime = 5; | 160 static final String _shortTime = DateFormat.Hm; |
156 static final int _mediumTime = 6; | 161 static final String _fullDateTime = _fullDate + _fullTime; |
157 static final int _shortTime = 7; | 162 static final String _longDateTime = _logDate + _longTime; |
158 static final int _fullDateTime = 8; | 163 static final String _mediumDateTime = _mediumDate + _mediumTime; |
159 static final int _longDateTime = 9; | 164 static final String _shortDateTime = _shortDate + _shortTime; |
160 static final int _mediumDateTime = 10; | |
161 static final int _shortDateTime = 11; | |
162 | 165 |
163 /** | 166 /** |
164 * Named constructors for each of the above values. | 167 * Named constructors for the above values. |
165 * These could probably be made shorter if we just set the format to the | 168 * These could probably be made shorter if we just set the format to the |
166 * constant and the parsing was lazy. | 169 * constant and the parsing was lazy. |
167 */ | 170 */ |
168 DateTimeFormat.fullDate() : this.formatDefinition = _fullDate; | 171 DateFormat.Hm() : this.formatDefinition = DateFormat.Hm; |
169 DateTimeFormat.longDate() : this.formatDefinition = _longDate; | 172 DateFormat.Hms() : this.formatDefinition = DateFormat.Hms; |
170 DateTimeFormat.mediumDate() : this.formatDefinition = _mediumDate; | 173 DateFormat.M() : this.formatDefinition = DateFormat.M; |
171 DateTimeFormat.shortDate() : this.formatDefinition = _shortDate; | 174 DateFormat.MEd() : this.formatDefinition = DateFormat.MEd; |
172 DateTimeFormat.fullTime() : this.formatDefinition = _fullTime; | 175 DateFormat.MMM() : this.formatDefinition = DateFormat.MMM; |
173 DateTimeFormat.longTime() : this.formatDefinition = _longTime; | 176 DateFormat.MMMEd() : this.formatDefinition = DateFormat.MMMEd; |
174 DateTimeFormat.mediumTime() : this.formatDefinition = _mediumTime; | 177 DateFormat.MMMMEd() : this.formatDefinition = DateFormat.MMMMEd; |
175 DateTimeFormat.shortTime() : this.formatDefinition = _shortTime; | 178 DateFormat.MMMMd() : this.formatDefinition = DateFormat.MMMMd; |
176 DateTimeFormat.fullDateTime() : this.formatDefinition = _fullDateTime; | 179 DateFormat.MMMd() : this.formatDefinition = DateFormat.MMMd; |
177 DateTimeFormat.longDateTime() : this.formatDefinition = _longDateTime; | 180 DateFormat.Md() : this.formatDefinition = DateFormat.Md; |
178 DateTimeFormat.mediumDateTime() : this.formatDefinition = _mediumDateTime; | 181 DateFormat.d() : this.formatDefinition = DateFormat.d; |
179 DateTimeFormat.shortDateTime() : this.formatDefinition = _shortDateTime; | 182 DateFormat.hm() : this.formatDefinition = DateFormat.hm; |
183 DateFormat.ms() : this.formatDefinition = DateFormat.ms; | |
184 DateFormat.y() : this.formatDefinition = DateFormat.y; | |
185 DateFormat.yM() : this.formatDefinition = DateFormat.yM; | |
186 DateFormat.yMEd() : this.formatDefinition = DateFormat.yMEd; | |
187 DateFormat.yMMM() : this.formatDefinition = DateFormat.yMMM; | |
188 DateFormat.yMMMEd() : this.formatDefinition = DateFormat.yMMMEd; | |
189 DateFormat.yMMMM() : this.formatDefinition = DateFormat.yMMMM; | |
190 DateFormat.yQ() : this.formatDefinition = DateFormat.yQ; | |
191 DateFormat.yQQQ() : this.formatDefinition = DateFormat.yQQQ; | |
192 | |
193 DateFormat.fullDate() : this.formatDefinition = _fullDate; | |
194 DateFormat.longDate() : this.formatDefinition = _longDate; | |
195 DateFormat.mediumDate() : this.formatDefinition = _mediumDate; | |
196 DateFormat.shortDate() : this.formatDefinition = _shortDate; | |
197 DateFormat.fullTime() : this.formatDefinition = _fullTime; | |
198 DateFormat.longTime() : this.formatDefinition = _longTime; | |
199 DateFormat.mediumTime() : this.formatDefinition = _mediumTime; | |
200 DateFormat.shortTime() : this.formatDefinition = _shortTime; | |
201 DateFormat.fullDateTime() : this.formatDefinition = _fullDateTime; | |
202 DateFormat.longDateTime() : this.formatDefinition = _longDateTime; | |
203 DateFormat.mediumDateTime() : this.formatDefinition = _mediumDateTime; | |
204 DateFormat.shortDateTime() : this.formatDefinition = _shortDateTime; | |
180 | 205 |
181 /** | 206 /** |
182 * Constructor accepts a [formatDefinition], which can be a String, one of the | 207 * 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 | 208 * 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 | 209 * 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 | 210 * 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). | 211 * Dart is running on the client, we can infer from the browser). |
187 */ | 212 */ |
188 DateTimeFormat(this.formatDefinition, [this._locale]); | 213 DateFormat(this.formatDefinition, [this._locale]); |
189 | 214 |
190 /** | 215 /** |
191 * Given user input, attempt to parse the [inputString] into the anticipated | 216 * Given user input, attempt to parse the [inputString] into the anticipated |
192 * format. | 217 * format. |
193 */ | 218 */ |
194 String parse(String inputString) { | 219 String parse(String inputString) { |
195 return inputString; | 220 return inputString; |
196 } | 221 } |
197 | 222 |
198 /** | 223 /** |
199 * Format the given [date] object according to preset pattern and current | 224 * Format the given [date] object according to preset pattern and current |
200 * locale and return a formated string for the given date. | 225 * locale and return a formated string for the given date. |
201 */ | 226 */ |
202 String format(Date date, [TimeZone timeZone]) { | 227 String format(Date date, [TimeZone timeZone]) { |
203 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... | 228 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... |
204 return date.toString(); | 229 return date.toString(); |
205 } | 230 } |
206 } | 231 } |
OLD | NEW |