| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 1 /** | 5 /** |
| 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 | |
| 4 * BSD-style license that can be found in the LICENSE file. | |
| 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, week name, 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: |
| (...skipping 16 matching lines...) Expand all Loading... |
| 39 * K hour in am/pm (0~11) (Number) 0 | 39 * K hour in am/pm (0~11) (Number) 0 |
| 40 * z time zone (Text) Pacific Standard Time | 40 * z time zone (Text) Pacific Standard Time |
| 41 * Z time zone (RFC 822) (Number) -0800 | 41 * Z time zone (RFC 822) (Number) -0800 |
| 42 * v time zone (generic) (Text) Pacific Time | 42 * v time zone (generic) (Text) Pacific Time |
| 43 * ' escape for text (Delimiter) 'Date=' | 43 * ' escape for text (Delimiter) 'Date=' |
| 44 * '' single quote (Literal) 'o''clock' | 44 * '' single quote (Literal) 'o''clock' |
| 45 * | 45 * |
| 46 * Items marked with '#' work differently than in Java. | 46 * Items marked with '#' work differently than in Java. |
| 47 * | 47 * |
| 48 * The count of pattern letters determine the format. | 48 * The count of pattern letters determine the format. |
| 49 * (Text): 4 or more pattern letters--use full form, | 49 * **Text**: |
| 50 * less than 4--use short or abbreviated form if one exists. | 50 * * 4 or more pattern letters--use full form, |
| 51 * In parsing, we will always try long format, then short. | 51 * * less than 4--use short or abbreviated form if one exists. |
| 52 * (e.g., "EEEE" produces "Monday", "EEE" produces "Mon") | 52 * In parsing, we will always try long format, then short. |
| 53 * (e.g., "EEEE" produces "Monday", "EEE" produces "Mon") |
| 53 * | 54 * |
| 54 * (Number): the minimum number of digits. Shorter numbers are zero-padded to | 55 * **Number**: the minimum number of digits. Shorter numbers are zero-padded to |
| 55 * this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled | 56 * this amount (e.g. if "m" produces "6", "mm" produces "06"). Year is handled |
| 56 * specially; that is, if the count of 'y' is 2, the Year will be truncated to | 57 * specially; that is, if the count of 'y' is 2, the Year will be truncated to |
| 57 * 2 digits. (e.g., if "yyyy" produces "1997", "yy" produces "97".) Unlike other | 58 * 2 digits. (e.g., if "yyyy" produces "1997", "yy" produces "97".) Unlike other |
| 58 * fields, fractional seconds are padded on the right with zero. | 59 * fields, fractional seconds are padded on the right with zero. |
| 59 * | 60 * |
| 60 * (Text & Number): 3 or over, use text, otherwise use number. | 61 * (Text & Number): 3 or over, use text, otherwise use number. |
| 61 * | 62 * |
| 62 * Any characters that not in the pattern will be treated as quoted text. For | 63 * Any characters that not in the pattern will be treated as quoted text. For |
| 63 * instance, characters like ':', '.', ' ', '#' and '@' will appear in the | 64 * instance, characters like ':', '.', ' ', '#' and '@' will appear in the |
| 64 * resulting time text even they are not embraced within single quotes. In our | 65 * resulting time text even they are not embraced within single quotes. In our |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 * runs are parsed specially. For example, the format "HHmmss" parses the input | 99 * 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 | 100 * 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 | 101 * 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 | 102 * 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 | 103 * 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 | 104 * 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 | 105 * or the leftmost field is one character in length. If the parse still fails at |
| 105 * that point, the parse of the run fails. | 106 * that point, the parse of the run fails. |
| 106 */ | 107 */ |
| 107 | 108 |
| 108 #library('DateFormat'); | 109 #library('date_format'); |
| 109 | 110 |
| 110 class DateFormat { | 111 class DateFormat { |
| 111 | 112 |
| 112 /** Definition of this object formats dates. */ | 113 /** Definition of how this object formats dates. */ |
| 113 var formatDefinition; | 114 String _formatDefinition; |
| 114 | 115 |
| 115 /** | 116 /** |
| 116 * String indicating the locale code with which the message is to be | 117 * The locale code with which the message is to be formatted (such as en-CA). |
| 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. These | 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 | 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 | 125 * be combined and we will attempt to find the best format for each locale |
| 126 * given the pattern. | 126 * given the pattern. |
| 127 * // TODO(efortuna): Hear back from i18n about Time Zones and the "core set" | 127 * // TODO(efortuna): Hear back from i18n about Time Zones and the "core set" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 static final String mediumTime = Hms; | 164 static final String mediumTime = Hms; |
| 165 static final String shortTime = Hm; | 165 static final String shortTime = Hm; |
| 166 static final String fullDateTime = '$fullDate$fullTime'; | 166 static final String fullDateTime = '$fullDate$fullTime'; |
| 167 static final String longDateTime = '$logDate$longTime'; | 167 static final String longDateTime = '$logDate$longTime'; |
| 168 static final String mediumDateTime = '$mediumDate$mediumTime'; | 168 static final String mediumDateTime = '$mediumDate$mediumTime'; |
| 169 static final String shortDateTime = '$shortDate$shortTime'; | 169 static final String shortDateTime = '$shortDate$shortTime'; |
| 170 | 170 |
| 171 /** | 171 /** |
| 172 * Named constructors for the above values. | 172 * Named constructors for the above values. |
| 173 */ | 173 */ |
| 174 DateFormat.Hm([this._locale]) : formatDefinition = Hm; | 174 DateFormat.Hm([this._locale]) : _formatDefinition = Hm; |
| 175 DateFormat.Hms([this._locale]) : formatDefinition = Hms; | 175 DateFormat.Hms([this._locale]) : _formatDefinition = Hms; |
| 176 DateFormat.M([this._locale]) : formatDefinition = M; | 176 DateFormat.M([this._locale]) : _formatDefinition = M; |
| 177 DateFormat.MEd([this._locale]) : formatDefinition = MEd; | 177 DateFormat.MEd([this._locale]) : _formatDefinition = MEd; |
| 178 DateFormat.MMM([this._locale]) : formatDefinition = MMM; | 178 DateFormat.MMM([this._locale]) : _formatDefinition = MMM; |
| 179 DateFormat.MMMEd([this._locale]) : formatDefinition = MMMEd; | 179 DateFormat.MMMEd([this._locale]) : _formatDefinition = MMMEd; |
| 180 DateFormat.MMMMEd([this._locale]) : formatDefinition = MMMMEd; | 180 DateFormat.MMMMEd([this._locale]) : _formatDefinition = MMMMEd; |
| 181 DateFormat.MMMMd([this._locale]) : formatDefinition = MMMMd; | 181 DateFormat.MMMMd([this._locale]) : _formatDefinition = MMMMd; |
| 182 DateFormat.MMMd([this._locale]) : formatDefinition = MMMd; | 182 DateFormat.MMMd([this._locale]) : _formatDefinition = MMMd; |
| 183 DateFormat.Md([this._locale]) : formatDefinition = Md; | 183 DateFormat.Md([this._locale]) : _formatDefinition = Md; |
| 184 DateFormat.d([this._locale]) : formatDefinition = d; | 184 DateFormat.d([this._locale]) : _formatDefinition = d; |
| 185 DateFormat.hm([this._locale]) : formatDefinition = hm; | 185 DateFormat.hm([this._locale]) : _formatDefinition = hm; |
| 186 DateFormat.ms([this._locale]) : formatDefinition = ms; | 186 DateFormat.ms([this._locale]) : _formatDefinition = ms; |
| 187 DateFormat.y([this._locale]) : formatDefinition = y; | 187 DateFormat.y([this._locale]) : _formatDefinition = y; |
| 188 DateFormat.yM([this._locale]) : formatDefinition = yM; | 188 DateFormat.yM([this._locale]) : _formatDefinition = yM; |
| 189 DateFormat.yMEd([this._locale]) : formatDefinition = yMEd; | 189 DateFormat.yMEd([this._locale]) : _formatDefinition = yMEd; |
| 190 DateFormat.yMMM([this._locale]) : formatDefinition = yMMM; | 190 DateFormat.yMMM([this._locale]) : _formatDefinition = yMMM; |
| 191 DateFormat.yMMMEd([this._locale]) : formatDefinition = yMMMEd; | 191 DateFormat.yMMMEd([this._locale]) : _formatDefinition = yMMMEd; |
| 192 DateFormat.yMMMM([this._locale]) : formatDefinition = yMMMM; | 192 DateFormat.yMMMM([this._locale]) : _formatDefinition = yMMMM; |
| 193 DateFormat.yQ([this._locale]) : formatDefinition = yQ; | 193 DateFormat.yQ([this._locale]) : _formatDefinition = yQ; |
| 194 DateFormat.yQQQ([this._locale]) : formatDefinition = yQQQ; | 194 DateFormat.yQQQ([this._locale]) : _formatDefinition = yQQQ; |
| 195 | 195 |
| 196 DateFormat.fullDate([this._locale]) : formatDefinition = fullDate; | 196 DateFormat.fullDate([this._locale]) : _formatDefinition = fullDate; |
| 197 DateFormat.longDate([this._locale]) : formatDefinition = longDate; | 197 DateFormat.longDate([this._locale]) : _formatDefinition = longDate; |
| 198 DateFormat.mediumDate([this._locale]) : formatDefinition = mediumDate; | 198 DateFormat.mediumDate([this._locale]) : _formatDefinition = mediumDate; |
| 199 DateFormat.shortDate([this._locale]) : formatDefinition = shortDate; | 199 DateFormat.shortDate([this._locale]) : _formatDefinition = shortDate; |
| 200 DateFormat.fullTime([this._locale]) : formatDefinition = fullTime; | 200 DateFormat.fullTime([this._locale]) : _formatDefinition = fullTime; |
| 201 DateFormat.longTime([this._locale]) : formatDefinition = longTime; | 201 DateFormat.longTime([this._locale]) : _formatDefinition = longTime; |
| 202 DateFormat.mediumTime([this._locale]) : formatDefinition = mediumTime; | 202 DateFormat.mediumTime([this._locale]) : _formatDefinition = mediumTime; |
| 203 DateFormat.shortTime([this._locale]) : formatDefinition = shortTime; | 203 DateFormat.shortTime([this._locale]) : _formatDefinition = shortTime; |
| 204 DateFormat.fullDateTime([this._locale]) : formatDefinition = fullDateTime; | 204 DateFormat.fullDateTime([this._locale]) : _formatDefinition = fullDateTime; |
| 205 DateFormat.longDateTime([this._locale]) : formatDefinition = longDateTime; | 205 DateFormat.longDateTime([this._locale]) : _formatDefinition = longDateTime; |
| 206 DateFormat.mediumDateTime([this._locale]) : formatDefinition = mediumDateTime; | 206 DateFormat.mediumDateTime([this._locale]) : _formatDefinition = mediumDateTime
; |
| 207 DateFormat.shortDateTime([this._locale]) : formatDefinition = shortDateTime; | 207 DateFormat.shortDateTime([this._locale]) : _formatDefinition = shortDateTime; |
| 208 | 208 |
| 209 /** | 209 /** |
| 210 * 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 |
| 211 * 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 |
| 212 * 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 |
| 213 * 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 |
| 214 * 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). |
| 215 */ | 215 */ |
| 216 DateFormat([this.formatDefinition = fullDate, this._locale]); | 216 DateFormat([formatDefinition = fullDate, locale]) { |
| 217 this._formatDefinition = formatDefinition; |
| 218 this._locale = locale; |
| 219 } |
| 217 | 220 |
| 218 /** | 221 /** |
| 219 * Given user input, attempt to parse the [inputString] into the anticipated | 222 * Given user input, attempt to parse the [inputString] into the anticipated |
| 220 * format. | 223 * format. |
| 221 */ | 224 */ |
| 222 String parse(String inputString) { | 225 String parse(String inputString) { |
| 223 return inputString; | 226 return inputString; |
| 224 } | 227 } |
| 225 | 228 |
| 226 /** | 229 /** |
| 227 * Format the given [date] object according to preset pattern and current | 230 * Format the given [date] object according to preset pattern and current |
| 228 * locale and return a formated string for the given date. | 231 * locale and return a formated string for the given date. |
| 229 */ | 232 */ |
| 230 String format(Date date, [TimeZone timeZone]) { | 233 String format(Date date, [TimeZone timeZone]) { |
| 231 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... | 234 // TODO(efortuna): optional TimeZone argument? TimeZone is deprecated... |
| 232 return date.toString(); | 235 return date.toString(); |
| 233 } | 236 } |
| 234 | 237 |
| 235 /** | 238 /** |
| 236 * Returns a date string indicating how long ago (3 hours, 2 minutes) | 239 * 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 | 240 * something has happened or how long in the future something will happen |
| 238 * given a [reference] Date relative to the current time. | 241 * given a [reference] Date relative to the current time. |
| 239 */ | 242 */ |
| 240 String formatDuration(Date reference) { | 243 String formatDuration(Date reference) { |
| 241 return ''; | 244 return ''; |
| 242 } | 245 } |
| 243 | 246 |
| 244 /** | 247 /** |
| 245 * Formats a String indicating how long ago (negative [duration]) or how far | 248 * 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 | 249 * in the future (positive [duration]) some time is with respect to a |
| 247 * reference [date]. The [duration] is expressed in miliseconds. | 250 * reference [date]. |
| 248 */ | 251 */ |
| 249 String formatDurationFrom(num duration, Date date) { | 252 String formatDurationFrom(Duration duration, Date date) { |
| 250 return ''; | 253 return ''; |
| 251 } | 254 } |
| 252 } | 255 } |
| OLD | NEW |