Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: sdk/lib/core/date_time.dart

Issue 48823005: improve DateTime parsing a UTC timezone offset (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: improved regex to parse Datetime Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | tests/corelib/date_time_parse_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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})?)?)?' // The time part
210 r' ?([zZ]|(?:\+00(?::?00)?)?))?$'); // The timezone part
Lasse Reichstein Nielsen 2013/10/29 08:48:45 This seemns to accept a single terminating space (
floitsch 2013/10/30 23:17:34 Done.
211
209 Match match = re.firstMatch(formattedString); 212 Match match = re.firstMatch(formattedString);
210 if (match != null) { 213 if (match != null) {
211 int parseIntOrZero(String matched) { 214 int parseIntOrZero(String matched) {
212 if (matched == null) return 0; 215 if (matched == null) return 0;
213 return int.parse(matched); 216 return int.parse(matched);
214 } 217 }
215 218
216 double parseDoubleOrZero(String matched) { 219 double parseDoubleOrZero(String matched) {
217 if (matched == null) return 0.0; 220 if (matched == null) return 0.0;
218 return double.parse(matched); 221 return double.parse(matched);
(...skipping 337 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 * In accordance with ISO 8601 559 * In accordance with ISO 8601
557 * a week starts with Monday, which has the value 1. 560 * a week starts with Monday, which has the value 1.
558 * 561 *
559 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00"); 562 * DateTime moonLanding = DateTime.parse("1969-07-20 20:18:00");
560 * assert(moonLanding.weekday == 7); 563 * assert(moonLanding.weekday == 7);
561 * assert(moonLanding.weekday == DateTime.SUNDAY); 564 * assert(moonLanding.weekday == DateTime.SUNDAY);
562 * 565 *
563 */ 566 */
564 external int get weekday; 567 external int get weekday;
565 } 568 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/date_time_parse_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698