 Chromium Code Reviews
 Chromium Code Reviews Issue 48823005:
  improve DateTime parsing a UTC timezone offset  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
    
  
    Issue 48823005:
  improve DateTime parsing a UTC timezone offset  (Closed) 
  Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart| 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) { | 
| 206 final RegExp re = new RegExp( | 207 final RegExp re = new RegExp( | 
| 207 r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. | 208 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])?)?$'); | 209 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ]|(\+00(:?00)? )?))?$'); | 
| 
floitsch
2013/10/28 23:01:24
80 chars.
The "(\+00" should be a non-capturing ("
 | |
| 209 Match match = re.firstMatch(formattedString); | 210 Match match = re.firstMatch(formattedString); | 
| 210 if (match != null) { | 211 if (match != null) { | 
| 211 int parseIntOrZero(String matched) { | 212 int parseIntOrZero(String matched) { | 
| 212 if (matched == null) return 0; | 213 if (matched == null) return 0; | 
| 213 return int.parse(matched); | 214 return int.parse(matched); | 
| 214 } | 215 } | 
| 215 | 216 | 
| 216 double parseDoubleOrZero(String matched) { | 217 double parseDoubleOrZero(String matched) { | 
| 217 if (matched == null) return 0.0; | 218 if (matched == null) return 0.0; | 
| 218 return double.parse(matched); | 219 return double.parse(matched); | 
| (...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 556 * In accordance with ISO 8601 | 557 * In accordance with ISO 8601 | 
| 557 * a week starts with Monday, which has the value 1. | 558 * a week starts with Monday, which has the value 1. | 
| 558 * | 559 * | 
| 559 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | 560 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); | 
| 560 * assert(moonLanding.weekday == 7); | 561 * assert(moonLanding.weekday == 7); | 
| 561 * assert(moonLanding.weekday == DateTime.SUNDAY); | 562 * assert(moonLanding.weekday == DateTime.SUNDAY); | 
| 562 * | 563 * | 
| 563 */ | 564 */ | 
| 564 external int get weekday; | 565 external int get weekday; | 
| 565 } | 566 } | 
| OLD | NEW |