| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 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, week name, field order, etc. | 10 * vary across locales include month name, week name, field order, etc. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 * instance, characters like ':', '.', ' ', '#' and '@' will appear in the | 64 * instance, characters like ':', '.', ' ', '#' and '@' will appear in the |
| 65 * 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 |
| 66 * current pattern usage, we didn't use up all letters. But those unused | 66 * current pattern usage, we didn't use up all letters. But those unused |
| 67 * letters are strongly discouraged to be used as quoted text without quote. | 67 * letters are strongly discouraged to be used as quoted text without quote. |
| 68 * That's because we may use other letter for pattern in future. | 68 * That's because we may use other letter for pattern in future. |
| 69 * | 69 * |
| 70 * Examples Using the US Locale: | 70 * Examples Using the US Locale: |
| 71 * | 71 * |
| 72 * Format Pattern Result | 72 * Format Pattern Result |
| 73 * -------------- ------- | 73 * -------------- ------- |
| 74 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv"->> 1996.07.10 AD at 15:08:56 Pacific Ti
me | 74 * "yyyy.MM.dd G 'at' HH:mm:ss vvvv"->1996.07.10 AD at 15:08:56 Pacific Time |
| 75 * "EEE, MMM d, ''yy" ->> Wed, July 10, '96 | 75 * "EEE, MMM d, ''yy" ->Wed, July 10, '96 |
| 76 * "h:mm a" ->> 12:08 PM | 76 * "h:mm a" ->12:08 PM |
| 77 * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time | 77 * "hh 'o''clock' a, zzzz" ->12 o'clock PM, Pacific Daylight Time |
| 78 * "K:mm a, vvv" ->> 0:00 PM, PT | 78 * "K:mm a, vvv" ->0:00 PM, PT |
| 79 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 01996.July.10 AD 12:08 PM | 79 * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->01996.July.10 AD 12:08 PM |
| 80 * | 80 * |
| 81 * When parsing a date string using the abbreviated year pattern ("yy"), | 81 * When parsing a date string using the abbreviated year pattern ("yy"), |
| 82 * DateTimeParse must interpret the abbreviated year relative to some | 82 * DateTimeParse must interpret the abbreviated year relative to some |
| 83 * century. It does this by adjusting dates to be within 80 years before and 20 | 83 * century. It does this by adjusting dates to be within 80 years before and 20 |
| 84 * years after the time the parse function is called. For example, using a | 84 * years after the time the parse function is called. For example, using a |
| 85 * pattern of "MM/dd/yy" and a DateTimeParse instance created on Jan 1, 1997, | 85 * pattern of "MM/dd/yy" and a DateTimeParse instance created on Jan 1, 1997, |
| 86 * the string "01/11/12" would be interpreted as Jan 11, 2012 while the string | 86 * the string "01/11/12" would be interpreted as Jan 11, 2012 while the string |
| 87 * "05/04/64" would be interpreted as May 4, 1964. During parsing, only | 87 * "05/04/64" would be interpreted as May 4, 1964. During parsing, only |
| 88 * strings consisting of exactly two digits, as defined by {@link | 88 * strings consisting of exactly two digits, as defined by {@link |
| 89 * java.lang.Character#isDigit(char)}, will be parsed into the default | 89 * java.lang.Character#isDigit(char)}, will be parsed into the default |
| 90 * century. Any other numeric string, such as a one digit string, a three or | 90 * century. Any other numeric string, such as a one digit string, a three or |
| 91 * more digit string will be interpreted as its face value. | 91 * more digit string will be interpreted as its face value. |
| 92 * | 92 * |
| 93 * If the year pattern does not have exactly two 'y' characters, the year is | 93 * If the year pattern does not have exactly two 'y' characters, the year is |
| 94 * interpreted literally, regardless of the number of digits. So using the | 94 * interpreted literally, regardless of the number of digits. So using the |
| 95 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D. | 95 * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D. |
| 96 * | 96 * |
| 97 * When numeric fields abut one another directly, with no intervening | 97 * When numeric fields abut one another directly, with no intervening |
| 98 * delimiter characters, they constitute a run of abutting numeric fields. Such | 98 * delimiter characters, they constitute a run of abutting numeric fields. Such |
| 99 * 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 |
| 100 * 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 |
| 101 * 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 |
| 102 * 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 |
| 103 * 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 |
| 104 * 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 |
| 105 * 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 |
| 106 * that point, the parse of the run fails. | 106 * that point, the parse of the run fails. |
| 107 */ | 107 */ |
| 108 | 108 |
| 109 #library('date_format'); | |
| 110 | |
| 111 class DateFormat { | 109 class DateFormat { |
| 112 | 110 |
| 113 /** Definition of how this object formats dates. */ | 111 /** Definition of how this object formats dates. */ |
| 114 String _formatDefinition; | 112 String _formatDefinition; |
| 115 | 113 |
| 116 /** | 114 /** |
| 117 * The locale code with which the message is to be formatted (such as en-CA). | 115 * The locale code with which the message is to be formatted (such as en-CA). |
| 118 */ | 116 */ |
| 119 String _locale; | 117 String _locale; |
| 120 | 118 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 static final String mediumTime = Hms; | 162 static final String mediumTime = Hms; |
| 165 static final String shortTime = Hm; | 163 static final String shortTime = Hm; |
| 166 static final String fullDateTime = '$fullDate$fullTime'; | 164 static final String fullDateTime = '$fullDate$fullTime'; |
| 167 static final String longDateTime = '$longDate$longTime'; | 165 static final String longDateTime = '$longDate$longTime'; |
| 168 static final String mediumDateTime = '$mediumDate$mediumTime'; | 166 static final String mediumDateTime = '$mediumDate$mediumTime'; |
| 169 static final String shortDateTime = '$shortDate$shortTime'; | 167 static final String shortDateTime = '$shortDate$shortTime'; |
| 170 | 168 |
| 171 /** | 169 /** |
| 172 * Constructors for dates/times that use a default format. | 170 * Constructors for dates/times that use a default format. |
| 173 */ | 171 */ |
| 174 DateFormat.Hm([this._locale]) : _formatDefinition = Hm; | 172 DateFormat.Hm([locale='']) : _locale = locale, _formatDefinition = Hm; |
| 175 DateFormat.Hms([this._locale]) : _formatDefinition = Hms; | 173 DateFormat.Hms([locale='']) : _locale = locale, _formatDefinition = Hms; |
| 176 DateFormat.M([this._locale]) : _formatDefinition = M; | 174 DateFormat.M([locale='']) : _locale = locale, _formatDefinition = M; |
| 177 DateFormat.MEd([this._locale]) : _formatDefinition = MEd; | 175 DateFormat.MEd([locale='']) : _locale = locale, _formatDefinition = MEd; |
| 178 DateFormat.MMM([this._locale]) : _formatDefinition = MMM; | 176 DateFormat.MMM([locale='']) : _locale = locale, _formatDefinition = MMM; |
| 179 DateFormat.MMMEd([this._locale]) : _formatDefinition = MMMEd; | 177 DateFormat.MMMEd([locale='']) : _locale = locale, _formatDefinition = MMMEd; |
| 180 DateFormat.MMMMEd([this._locale]) : _formatDefinition = MMMMEd; | 178 DateFormat.MMMMEd([locale='']) : _locale = locale, _formatDefinition = MMMMEd; |
| 181 DateFormat.MMMMd([this._locale]) : _formatDefinition = MMMMd; | 179 DateFormat.MMMMd([locale='']) : _locale = locale, _formatDefinition = MMMMd; |
| 182 DateFormat.MMMd([this._locale]) : _formatDefinition = MMMd; | 180 DateFormat.MMMd([locale='']) : _locale = locale, _formatDefinition = MMMd; |
| 183 DateFormat.Md([this._locale]) : _formatDefinition = Md; | 181 DateFormat.Md([locale='']) : _locale = locale, _formatDefinition = Md; |
| 184 DateFormat.d([this._locale]) : _formatDefinition = d; | 182 DateFormat.d([locale='']) : _locale = locale, _formatDefinition = d; |
| 185 DateFormat.hm([this._locale]) : _formatDefinition = hm; | 183 DateFormat.hm([locale='']) : _locale = locale, _formatDefinition = hm; |
| 186 DateFormat.ms([this._locale]) : _formatDefinition = ms; | 184 DateFormat.ms([locale='']) : _locale = locale, _formatDefinition = ms; |
| 187 DateFormat.y([this._locale]) : _formatDefinition = y; | 185 DateFormat.y([locale='']) : _locale = locale, _formatDefinition = y; |
| 188 DateFormat.yM([this._locale]) : _formatDefinition = yM; | 186 DateFormat.yM([locale='']) : _locale = locale, _formatDefinition = yM; |
| 189 DateFormat.yMEd([this._locale]) : _formatDefinition = yMEd; | 187 DateFormat.yMEd([locale='']) : _locale = locale, _formatDefinition = yMEd; |
| 190 DateFormat.yMMM([this._locale]) : _formatDefinition = yMMM; | 188 DateFormat.yMMM([locale='']) : _locale = locale, _formatDefinition = yMMM; |
| 191 DateFormat.yMMMEd([this._locale]) : _formatDefinition = yMMMEd; | 189 DateFormat.yMMMEd([locale='']) : _locale = locale, _formatDefinition = yMMMEd; |
| 192 DateFormat.yMMMM([this._locale]) : _formatDefinition = yMMMM; | 190 DateFormat.yMMMM([locale='']) : _locale = locale, _formatDefinition = yMMMM; |
| 193 DateFormat.yQ([this._locale]) : _formatDefinition = yQ; | 191 DateFormat.yQ([locale='']) : _locale = locale, _formatDefinition = yQ; |
| 194 DateFormat.yQQQ([this._locale]) : _formatDefinition = yQQQ; | 192 DateFormat.yQQQ([locale='']) : _locale = locale, _formatDefinition = yQQQ; |
| 195 | 193 |
| 196 DateFormat.fullDate([this._locale]) : _formatDefinition = fullDate; | 194 DateFormat.fullDate([locale='']) : _locale = locale, |
| 197 DateFormat.longDate([this._locale]) : _formatDefinition = longDate; | 195 _formatDefinition = fullDate; |
| 198 DateFormat.mediumDate([this._locale]) : _formatDefinition = mediumDate; | 196 DateFormat.longDate([locale='']) : _locale = locale, |
| 199 DateFormat.shortDate([this._locale]) : _formatDefinition = shortDate; | 197 _formatDefinition = longDate; |
| 200 DateFormat.fullTime([this._locale]) : _formatDefinition = fullTime; | 198 DateFormat.mediumDate([locale='']) : _locale = locale, |
| 201 DateFormat.longTime([this._locale]) : _formatDefinition = longTime; | 199 _formatDefinition = mediumDate; |
| 202 DateFormat.mediumTime([this._locale]) : _formatDefinition = mediumTime; | 200 DateFormat.shortDate([locale='']) : _locale = locale, |
| 203 DateFormat.shortTime([this._locale]) : _formatDefinition = shortTime; | 201 _formatDefinition = shortDate; |
| 204 DateFormat.fullDateTime([this._locale]) : _formatDefinition = fullDateTime; | 202 DateFormat.fullTime([locale='']) : _locale = locale, |
| 205 DateFormat.longDateTime([this._locale]) : _formatDefinition = longDateTime; | 203 _formatDefinition = fullTime; |
| 206 DateFormat.mediumDateTime([this._locale]) : _formatDefinition = mediumDateTime
; | 204 DateFormat.longTime([locale='']) : _locale = locale, |
| 207 DateFormat.shortDateTime([this._locale]) : _formatDefinition = shortDateTime; | 205 _formatDefinition = longTime; |
| 206 DateFormat.mediumTime([locale='']) : _locale = locale, |
| 207 _formatDefinition = mediumTime; |
| 208 DateFormat.shortTime([locale='']) : _locale = locale, |
| 209 _formatDefinition = shortTime; |
| 210 DateFormat.fullDateTime([locale='']) : _locale = locale, |
| 211 _formatDefinition = fullDateTime; |
| 212 DateFormat.longDateTime([locale='']) : _locale = locale, |
| 213 _formatDefinition = longDateTime; |
| 214 DateFormat.mediumDateTime([locale='']) : _locale = locale, |
| 215 _formatDefinition = mediumDateTime; |
| 216 DateFormat.shortDateTime([locale='']) : _locale = locale, |
| 217 _formatDefinition = shortDateTime; |
| 208 | 218 |
| 209 /** | 219 /** |
| 210 * Constructor accepts a [formatDefinition], which can be a String, one of the | 220 * 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 | 221 * 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 | 222 * 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 | 223 * 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). | 224 * Dart is running on the client, we can infer from the browser). |
| 215 */ | 225 */ |
| 216 DateFormat([formatDefinition = fullDate, locale]) { | 226 DateFormat([formatDefinition = fullDate, locale = '']) { |
| 217 this._formatDefinition = formatDefinition; | 227 this._formatDefinition = formatDefinition; |
| 218 this._locale = locale; | 228 this._locale = locale; |
| 219 } | 229 } |
| 220 | 230 |
| 221 /** | 231 /** |
| 222 * Given user input, attempt to parse the [inputString] into the anticipated | 232 * Given user input, attempt to parse the [inputString] into the anticipated |
| 223 * format. | 233 * format. |
| 224 */ | 234 */ |
| 225 String parse(String inputString) { | 235 String parse(String inputString) { |
| 226 return inputString; | 236 return inputString; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 246 | 256 |
| 247 /** | 257 /** |
| 248 * Formats a string indicating how long ago (negative [duration]) or how far | 258 * Formats a string indicating how long ago (negative [duration]) or how far |
| 249 * in the future (positive [duration]) some time is with respect to a | 259 * in the future (positive [duration]) some time is with respect to a |
| 250 * reference [date]. | 260 * reference [date]. |
| 251 */ | 261 */ |
| 252 String formatDurationFrom(Duration duration, Date date) { | 262 String formatDurationFrom(Duration duration, Date date) { |
| 253 return ''; | 263 return ''; |
| 254 } | 264 } |
| 255 } | 265 } |
| OLD | NEW |