| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 part of dart.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An instant in time, such as July 20, 1969, 8:18pm PST. | 8 * An instant in time, such as July 20, 1969, 8:18pm PST. |
| 9 * | 9 * |
| 10 * Create a DateTime object by using one of the constructors | 10 * Create a DateTime object by using one of the constructors |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 * | 41 * |
| 42 * Day and month values begin at 1, and the week starts on Monday. | 42 * Day and month values begin at 1, and the week starts on Monday. |
| 43 * That is, the constants [JANUARY] and [MONDAY] are both 1. | 43 * That is, the constants [JANUARY] and [MONDAY] are both 1. |
| 44 * | 44 * |
| 45 * ## Working with UTC and local time | 45 * ## Working with UTC and local time |
| 46 * | 46 * |
| 47 * A DateTime object is in the local time zone | 47 * A DateTime object is in the local time zone |
| 48 * unless explicitly created in the UTC time zone. | 48 * unless explicitly created in the UTC time zone. |
| 49 * | 49 * |
| 50 * DateTime dDay = new DateTime.utc(1944, 6, 6); | 50 * DateTime dDay = new DateTime.utc(1944, 6, 6); |
| 51 * | 51 * |
| 52 * Use [isUtc] to determine whether a DateTime object is based in UTC. | 52 * Use [isUtc] to determine whether a DateTime object is based in UTC. |
| 53 * Use the methods [toLocal] and [toUtc] | 53 * Use the methods [toLocal] and [toUtc] |
| 54 * to get the equivalent date/time value specified in the other time zone. | 54 * to get the equivalent date/time value specified in the other time zone. |
| 55 * Use [timeZoneName] to get an abbreviated name of the time zone | 55 * Use [timeZoneName] to get an abbreviated name of the time zone |
| 56 * for the DateTime object. | 56 * for the DateTime object. |
| 57 * To find the difference | 57 * To find the difference |
| 58 * between UTC and the time zone of a DateTime object | 58 * between UTC and the time zone of a DateTime object |
| 59 * call [timeZoneOffset]. | 59 * call [timeZoneOffset]. |
| 60 * | 60 * |
| 61 * ## Comparing DateTime objects | 61 * ## Comparing DateTime objects |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 * | 192 * |
| 193 * The function parses a subset of ISO 8601. Examples of accepted strings: | 193 * The function parses a subset of ISO 8601. Examples of accepted strings: |
| 194 * | 194 * |
| 195 * * `"2012-02-27 13:27:00"` | 195 * * `"2012-02-27 13:27:00"` |
| 196 * * `"2012-02-27 13:27:00.123456z"` | 196 * * `"2012-02-27 13:27:00.123456z"` |
| 197 * * `"20120227 13:27:00"` | 197 * * `"20120227 13:27:00"` |
| 198 * * `"20120227T132700"` | 198 * * `"20120227T132700"` |
| 199 * * `"20120227"` | 199 * * `"20120227"` |
| 200 * * `"+20120227"` | 200 * * `"+20120227"` |
| 201 * * `"2012-02-27T14Z"` | 201 * * `"2012-02-27T14Z"` |
| 202 * * `"2012-02-27T14+00:00"` |
| 202 * * `"-123450101 00:00:00 Z"`: in the year -12345. | 203 * * `"-123450101 00:00:00 Z"`: in the year -12345. |
| 203 */ | 204 */ |
| 204 // TODO(floitsch): specify grammar. | 205 // TODO(floitsch): specify grammar. |
| 205 static DateTime parse(String formattedString) { | 206 static DateTime parse(String formattedString) { |
| 207 /* |
| 208 * date ::= yeardate time_opt timezone_opt |
| 209 * yeardate ::= year colon_opt month colon_opt day |
| 210 * year ::= sign_opt digit{4,5} |
| 211 * colon_opt :: <empty> | ':' |
| 212 * sign_opt ::= <empty> | '+' | '-' |
| 213 * month ::= digit{2} |
| 214 * day ::= digit{2} |
| 215 * time_opt ::= <empty> | (' ' | 'T') hour minutes_opt |
| 216 * minutes_opt ::= <empty> | ':' digit{2} seconds_opt |
| 217 * seconds_opt ::= <empty> | ':' digit{2} millis_opt |
| 218 * millis_opt ::= <empty> | '.' digit{1,6} |
| 219 * timezone_opt ::= <empty> | space_opt timezone |
| 220 * space_opt :: ' ' | <empty> |
| 221 * timezone ::= 'z' | 'Z' | '+' '0' '0' timezonemins_opt |
| 222 * timezonemins_opt ::= <empty> | colon_opt '0' '0' |
| 223 */ |
| 206 final RegExp re = new RegExp( | 224 final RegExp re = new RegExp( |
| 207 r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. | 225 r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. |
| 208 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); | 226 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)?' // The time part |
| 227 r'( ?[zZ]| ?\+00(?::?00)?)?)?$'); // The timezone part |
| 228 |
| 209 Match match = re.firstMatch(formattedString); | 229 Match match = re.firstMatch(formattedString); |
| 210 if (match != null) { | 230 if (match != null) { |
| 211 int parseIntOrZero(String matched) { | 231 int parseIntOrZero(String matched) { |
| 212 if (matched == null) return 0; | 232 if (matched == null) return 0; |
| 213 return int.parse(matched); | 233 return int.parse(matched); |
| 214 } | 234 } |
| 215 | 235 |
| 216 double parseDoubleOrZero(String matched) { | 236 double parseDoubleOrZero(String matched) { |
| 217 if (matched == null) return 0.0; | 237 if (matched == null) return 0.0; |
| 218 return double.parse(matched); | 238 return double.parse(matched); |
| 219 } | 239 } |
| 220 | 240 |
| 221 int years = int.parse(match[1]); | 241 int years = int.parse(match[1]); |
| 222 int month = int.parse(match[2]); | 242 int month = int.parse(match[2]); |
| 223 int day = int.parse(match[3]); | 243 int day = int.parse(match[3]); |
| 224 int hour = parseIntOrZero(match[4]); | 244 int hour = parseIntOrZero(match[4]); |
| 225 int minute = parseIntOrZero(match[5]); | 245 int minute = parseIntOrZero(match[5]); |
| 226 int second = parseIntOrZero(match[6]); | 246 int second = parseIntOrZero(match[6]); |
| 227 bool addOneMillisecond = false; | 247 bool addOneMillisecond = false; |
| 228 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round(); | 248 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round(); |
| 229 if (millisecond == 1000) { | 249 if (millisecond == 1000) { |
| 230 addOneMillisecond = true; | 250 addOneMillisecond = true; |
| 231 millisecond = 999; | 251 millisecond = 999; |
| 232 } | 252 } |
| 233 // TODO(floitsch): we should not need to test against the empty string. | 253 bool isUtc = (match[8] != null); |
| 234 bool isUtc = (match[8] != null) && (match[8] != ""); | |
| 235 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | 254 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( |
| 236 years, month, day, hour, minute, second, millisecond, isUtc); | 255 years, month, day, hour, minute, second, millisecond, isUtc); |
| 237 if (millisecondsSinceEpoch == null) { | 256 if (millisecondsSinceEpoch == null) { |
| 238 throw new ArgumentError(formattedString); | 257 throw new ArgumentError(formattedString); |
| 239 } | 258 } |
| 240 if (addOneMillisecond) millisecondsSinceEpoch++; | 259 if (addOneMillisecond) millisecondsSinceEpoch++; |
| 241 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, | 260 return new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, |
| 242 isUtc: isUtc); | 261 isUtc: isUtc); |
| 243 } else { | 262 } else { |
| 244 throw new ArgumentError(formattedString); | 263 throw new ArgumentError(formattedString); |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 * In accordance with ISO 8601 | 575 * In accordance with ISO 8601 |
| 557 * a week starts with Monday, which has the value 1. | 576 * a week starts with Monday, which has the value 1. |
| 558 * | 577 * |
| 559 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | 578 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); |
| 560 * assert(moonLanding.weekday == 7); | 579 * assert(moonLanding.weekday == 7); |
| 561 * assert(moonLanding.weekday == DateTime.SUNDAY); | 580 * assert(moonLanding.weekday == DateTime.SUNDAY); |
| 562 * | 581 * |
| 563 */ | 582 */ |
| 564 external int get weekday; | 583 external int get weekday; |
| 565 } | 584 } |
| OLD | NEW |