Chromium Code Reviews| 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 * DateFormat 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 * |
| 17 * This library uses the ICU/JDK date/time pattern specification as described | 17 * This library uses the ICU/JDK date/time pattern specification as described |
| 18 * below. | 18 * below. |
| 19 * | 19 * |
| 20 * Time Format Syntax: To specify the time format use a time pattern string. | 20 * Time Format Syntax: To specify the time format use a time pattern string. |
| 21 * In this pattern, following letters are reserved as pattern letters, which | 21 * In this pattern, following letters are reserved as pattern letters, which |
| 22 * are defined in the following manner: | 22 * are defined in the following manner: |
| 23 * | 23 * |
| 24 * Symbol Meaning Presentation Example | 24 * Symbol Meaning Presentation Example |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 * instance, characters like ':', '.', ' ', '#' and '@' will appear in the | 63 * instance, characters like ':', '.', ' ', '#' and '@' will appear in the |
| 64 * resulting time text even they are not embraced within single quotes. In our | 64 * resulting time text even they are not embraced within single quotes. In our |
| 65 * current pattern usage, we didn't use up all letters. But those unused | 65 * current pattern usage, we didn't use up all letters. But those unused |
| 66 * letters are strongly discouraged to be used as quoted text without quote. | 66 * letters are strongly discouraged to be used as quoted text without quote. |
| 67 * That's because we may use other letter for pattern in future. | 67 * That's because we may use other letter for pattern in future. |
| 68 * | 68 * |
| 69 * Examples Using the US Locale: | 69 * Examples Using the US Locale: |
| 70 * | 70 * |
| 71 * Format Pattern Result | 71 * Format Pattern Result |
| 72 * -------------- ------- | 72 * -------------- ------- |
| 73 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv"->1996.07.10 AD at 15:08:56 Pacific Time | 73 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv"->> 1996.07.10 AD at 15:08:56 Pacific Ti me |
| 74 * "EEE, MMM d, ''yy" ->Wed, July 10, '96 | 74 * "EEE, MMM d, ''yy" ->> Wed, July 10, '96 |
| 75 * "h:mm a" ->12:08 PM | 75 * "h:mm a" ->> 12:08 PM |
|
Emily Fortuna
2012/06/07 21:33:36
Please put the formatting back for this. Markdown
Alan Knight
2012/06/07 21:58:53
Done.
| |
| 76 * "hh 'o''clock' a, zzzz" ->12 o'clock PM, Pacific Daylight Time | 76 * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time |
| 77 * "K:mm a, vvv" ->0:00 PM, PT | 77 * "K:mm a, vvv" ->> 0:00 PM, PT |
| 78 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->01996.July.10 AD 12:08 PM | 78 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 01996.July.10 AD 12:08 PM |
| 79 * | 79 * |
| 80 * When parsing a date string using the abbreviated year pattern ("yy"), | 80 * When parsing a date string using the abbreviated year pattern ("yy"), |
| 81 * DateTimeParse must interpret the abbreviated year relative to some | 81 * DateTimeParse must interpret the abbreviated year relative to some |
| 82 * century. It does this by adjusting dates to be within 80 years before and 20 | 82 * century. It does this by adjusting dates to be within 80 years before and 20 |
| 83 * years after the time the parse function is called. For example, using a | 83 * years after the time the parse function is called. For example, using a |
| 84 * pattern of "MM/dd/yy" and a DateTimeParse instance created on Jan 1, 1997, | 84 * pattern of "MM/dd/yy" and a DateTimeParse instance created on Jan 1, 1997, |
| 85 * the string "01/11/12" would be interpreted as Jan 11, 2012 while the string | 85 * the string "01/11/12" would be interpreted as Jan 11, 2012 while the string |
| 86 * "05/04/64" would be interpreted as May 4, 1964. During parsing, only | 86 * "05/04/64" would be interpreted as May 4, 1964. During parsing, only |
| 87 * strings consisting of exactly two digits, as defined by {@link | 87 * strings consisting of exactly two digits, as defined by {@link |
| 88 * java.lang.Character#isDigit(char)}, will be parsed into the default | 88 * java.lang.Character#isDigit(char)}, will be parsed into the default |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 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('DateFormat'); | 108 #library('DateFormat'); |
| 109 | 109 |
| 110 class DateFormat { | 110 class DateFormat { |
| 111 | 111 |
| 112 /** Definition of this object formats dates. */ | 112 /** |
| 113 * Definition of how this object formats dates. | |
| 114 * (TODO) alanknight This might just be a String, but it's not clear yet. | |
| 115 */ | |
| 113 var formatDefinition; | 116 var formatDefinition; |
| 114 | 117 |
| 115 /** | 118 /** |
| 116 * String indicating the locale code with which the message is to be | 119 * String indicating the locale code with which the message is to be |
| 117 * formatted (such as en-CA). | 120 * formatted (such as en-CA). |
| 118 */ | 121 */ |
| 119 String _locale; | 122 String _locale; |
| 120 | 123 |
| 121 /** | 124 /** |
| 122 * Date/Time format "skeleton" patterns. Also specifiable by String, but | 125 * Date/Time format "skeleton" patterns. Also specifiable by String, but |
| 123 * written this way so that they can be discoverable via autocomplete. These | 126 * 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 | 127 * 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 | 128 * be combined and we will attempt to find the best format for each locale |
| 126 * given the pattern. | 129 * given the pattern. |
| 127 * // TODO(efortuna): Hear back from i18n about Time Zones and the "core set" | 130 * // TODO(efortuna): Hear back from i18n about Time Zones and the "core set" |
| 128 * // of skeleton patterns. | 131 * // of skeleton patterns. |
| 129 */ | 132 */ |
| 130 // Example of how this looks in the US | 133 // Example of how this looks in the US |
| 131 // locale. | 134 // locale. |
| 132 static final String Hm = 'Hm'; // HH:mm | 135 static final String Hm = 'Hm'; // HH:mm |
| 133 static final String Hms = 'Hms'; // HH:mm:ss | 136 static final String Hms = 'Hms'; // HH:mm:ss |
| 134 static final String M = 'M'; // L | 137 static final String M = 'M'; // L |
| 135 static final String MEd = 'MEd'; // E, M/d | 138 static final String MEd = 'MEd'; // E, M/d |
| 136 static final String MMM = 'MMM'; // LLL | 139 static final String MMM = 'MMM'; // LLL |
| 137 static final String MMMEd = 'MMMEd'; // E, MMM d | 140 static final String MMMEd = 'MMMEd'; // E, MMM d |
| 138 static final String MMMMEd = 'MMMMEd'; // E, MMMM d | 141 static final String MMMMEd = 'MMMMEd'; // E, MMMM d |
| 139 static final String MMMMd = 'MMMMd'; // MMMM d | 142 static final String MMMMd = 'MMMMd'; // MMMM d |
| 140 static final String MMMd = 'MMMd'; // MMM d | 143 static final String MMMd = 'MMMd'; // MMM d |
| 141 static final String Md = 'Md'; // M/d | 144 static final String Md = 'Md'; // M/d |
| 142 static final String d = 'd'; // d | 145 static final String d = 'd'; // d |
| 143 static final String hm = 'hm'; // h:mm a | 146 static final String hm = 'hm'; // h:mm a |
| 144 static final String ms = 'ms'; // mm:ss | 147 static final String ms = 'ms'; // mm:ss |
| 145 static final String y = 'y'; // yyyy | 148 static final String y = 'y'; // yyyy |
| 146 static final String yM = 'yM'; // M/yyyy | 149 static final String yM = 'yM'; // M/yyyy |
| 147 static final String yMEd = 'yMEd'; // EEE, M/d/yyyy | 150 static final String yMEd = 'yMEd'; // EEE, M/d/yyyy |
| 148 static final String yMMM = 'yMMM'; // MMM yyyy | 151 static final String yMMM = 'yMMM'; // MMM yyyy |
| 149 static final String yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy | 152 static final String yMMMEd = 'yMMMEd'; // EEE, MM d, yyyy |
| 150 static final String yMMMM = 'yMMMM'; // MMMM yyyy | 153 static final String yMMMM = 'yMMMM'; // MMMM yyyy |
| 151 static final String yQ = 'yQ'; // Q yyyy | 154 static final String yQ = 'yQ'; // Q yyyy |
| 152 static final String yQQQ = 'yQQQ'; // QQQ yyyy | 155 static final String yQQQ = 'yQQQ'; // QQQ yyyy |
| 153 | 156 |
| 154 /** Date/Time format patterns. */ | 157 /** Date/Time format patterns. */ |
| 155 // TODO(efortuna): This are just guesses of what a full date, long date is. | 158 // TODO(efortuna): This are just guesses of what a full date, long date is. |
| 156 // Do the proper homework on ICU to find the proper set "Hms"/"yMMd" | 159 // Do the proper homework on ICU to find the proper set "Hms"/"yMMd" |
| 157 // applicable to each case. | 160 // applicable to each case. |
| 158 static final String fullDate = '$y$MMMMd'; | 161 static final String fullDate = '$y$MMMMd'; |
| 159 static final String longDate = yMMMEd; | 162 static final String longDate = yMMMEd; |
| 160 static final String mediumDate = '$y$Md'; | 163 static final String mediumDate = '$y$Md'; |
| 161 static final String shortDate = Md; | 164 static final String shortDate = Md; |
| 162 static final String fullTime = '$Hms a'; | 165 static final String fullTime = '$Hms a'; |
| 163 static final String longTime = '$Hms a zzzz'; | 166 static final String longTime = '$Hms a zzzz'; |
| 164 static final String mediumTime = Hms; | 167 static final String mediumTime = Hms; |
| 165 static final String shortTime = Hm; | 168 static final String shortTime = Hm; |
| 166 static final String fullDateTime = '$fullDate$fullTime'; | 169 static final String fullDateTime = '$fullDate$fullTime'; |
| 167 static final String longDateTime = '$logDate$longTime'; | 170 static final String longDateTime = '$longDate$longTime'; |
| 168 static final String mediumDateTime = '$mediumDate$mediumTime'; | 171 static final String mediumDateTime = '$mediumDate$mediumTime'; |
| 169 static final String shortDateTime = '$shortDate$shortTime'; | 172 static final String shortDateTime = '$shortDate$shortTime'; |
| 170 | 173 |
| 171 /** | 174 /** |
| 172 * Named constructors for the above values. | 175 * Constructors for dates/times that use a default format. |
| 173 */ | 176 **/ |
| 177 | |
| 174 DateFormat.Hm([this._locale]) : formatDefinition = Hm; | 178 DateFormat.Hm([this._locale]) : formatDefinition = Hm; |
| 175 DateFormat.Hms([this._locale]) : formatDefinition = Hms; | 179 DateFormat.Hms([this._locale]) : formatDefinition = Hms; |
| 176 DateFormat.M([this._locale]) : formatDefinition = M; | 180 DateFormat.M([this._locale]) : formatDefinition = M; |
| 177 DateFormat.MEd([this._locale]) : formatDefinition = MEd; | 181 DateFormat.MEd([this._locale]) : formatDefinition = MEd; |
| 178 DateFormat.MMM([this._locale]) : formatDefinition = MMM; | 182 DateFormat.MMM([this._locale]) : formatDefinition = MMM; |
| 179 DateFormat.MMMEd([this._locale]) : formatDefinition = MMMEd; | 183 DateFormat.MMMEd([this._locale]) : formatDefinition = MMMEd; |
| 180 DateFormat.MMMMEd([this._locale]) : formatDefinition = MMMMEd; | 184 DateFormat.MMMMEd([this._locale]) : formatDefinition = MMMMEd; |
| 181 DateFormat.MMMMd([this._locale]) : formatDefinition = MMMMd; | 185 DateFormat.MMMMd([this._locale]) : formatDefinition = MMMMd; |
| 182 DateFormat.MMMd([this._locale]) : formatDefinition = MMMd; | 186 DateFormat.MMMd([this._locale]) : formatDefinition = MMMd; |
| 183 DateFormat.Md([this._locale]) : formatDefinition = Md; | 187 DateFormat.Md([this._locale]) : formatDefinition = Md; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 198 DateFormat.mediumDate([this._locale]) : formatDefinition = mediumDate; | 202 DateFormat.mediumDate([this._locale]) : formatDefinition = mediumDate; |
| 199 DateFormat.shortDate([this._locale]) : formatDefinition = shortDate; | 203 DateFormat.shortDate([this._locale]) : formatDefinition = shortDate; |
| 200 DateFormat.fullTime([this._locale]) : formatDefinition = fullTime; | 204 DateFormat.fullTime([this._locale]) : formatDefinition = fullTime; |
| 201 DateFormat.longTime([this._locale]) : formatDefinition = longTime; | 205 DateFormat.longTime([this._locale]) : formatDefinition = longTime; |
| 202 DateFormat.mediumTime([this._locale]) : formatDefinition = mediumTime; | 206 DateFormat.mediumTime([this._locale]) : formatDefinition = mediumTime; |
| 203 DateFormat.shortTime([this._locale]) : formatDefinition = shortTime; | 207 DateFormat.shortTime([this._locale]) : formatDefinition = shortTime; |
| 204 DateFormat.fullDateTime([this._locale]) : formatDefinition = fullDateTime; | 208 DateFormat.fullDateTime([this._locale]) : formatDefinition = fullDateTime; |
| 205 DateFormat.longDateTime([this._locale]) : formatDefinition = longDateTime; | 209 DateFormat.longDateTime([this._locale]) : formatDefinition = longDateTime; |
| 206 DateFormat.mediumDateTime([this._locale]) : formatDefinition = mediumDateTime; | 210 DateFormat.mediumDateTime([this._locale]) : formatDefinition = mediumDateTime; |
| 207 DateFormat.shortDateTime([this._locale]) : formatDefinition = shortDateTime; | 211 DateFormat.shortDateTime([this._locale]) : formatDefinition = shortDateTime; |
| 208 | 212 |
| 209 /** | 213 /** |
| 210 * 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 |
| 211 * 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 |
| 212 * 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 |
| 213 * 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 |
| 214 * 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). |
| 215 */ | 219 */ |
| 216 DateFormat([this.formatDefinition = fullDate, this._locale]); | 220 DateFormat([this.formatDefinition = fullDate, this._locale]); |
| 217 | 221 |
| 218 /** | 222 /** |
| 219 * Given user input, attempt to parse the [inputString] into the anticipated | 223 * Given user input, attempt to parse the [inputString] into the anticipated |
| 220 * format. | 224 * format. |
| 221 */ | 225 */ |
| 222 String parse(String inputString) { | 226 String parse(String inputString) { |
| 223 return inputString; | 227 return inputString; |
| 224 } | 228 } |
| 225 | 229 |
| 226 /** | 230 /** |
| 227 * Format the given [date] object according to preset pattern and current | 231 * Format the given [date] object according to preset pattern and current |
| 228 * locale and return a formated string for the given date. | 232 * locale and return a formated string for the given date. |
| 229 */ | 233 */ |
| 230 String format(Date date, [TimeZone timeZone]) { | 234 String format(Date date, [TimeZone timeZone]) { |
| 231 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... | 235 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... |
| 232 return date.toString(); | 236 return date.toString(); |
| 233 } | 237 } |
| 234 | 238 |
| 235 /** | 239 /** |
| 236 * Returns a date string indicating how long ago (3 hours, 2 minutes) | 240 * 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 | 241 * something has happened or how long in the future something will happen |
| 238 * given a [reference] Date relative to the current time. | 242 * given a [reference] Date relative to the current time. |
| 239 */ | 243 */ |
| 240 String formatDuration(Date reference) { | 244 String formatDuration(Date reference) { |
| 241 return ''; | 245 return ''; |
| 242 } | 246 } |
| 243 | 247 |
| 244 /** | 248 /** |
| 245 * Formats a String indicating how long ago (negative [duration]) or how far | 249 * 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 | 250 * in the future (positive [duration]) some time is with respect to a |
| 247 * reference [date]. The [duration] is expressed in miliseconds. | 251 * reference [date]. The [duration] is expressed in miliseconds. |
| 248 */ | 252 */ |
| 249 String formatDurationFrom(num duration, Date date) { | 253 String formatDurationFrom(num duration, Date date) { |
| 250 return ''; | 254 return ''; |
| 251 } | 255 } |
| 252 } | 256 } |
| OLD | NEW |