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 class DateImplementation implements Date { | 5 class DateImplementation implements Date { |
6 final int millisecondsSinceEpoch; | 6 final int millisecondsSinceEpoch; |
7 final bool isUtc; | 7 final bool isUtc; |
8 | 8 |
9 factory DateImplementation.fromString(String formattedString) { | 9 factory DateImplementation.fromString(String formattedString) { |
10 // Read in (a subset of) ISO 8601. | 10 // Read in (a subset of) ISO 8601. |
11 // Examples: | 11 // Examples: |
12 // - "2012-02-27 13:27:00" | 12 // - "2012-02-27 13:27:00" |
13 // - "2012-02-27 13:27:00.423z" | 13 // - "2012-02-27 13:27:00.423z" |
14 // - "20120227 13:27:00" | 14 // - "20120227 13:27:00" |
15 // - "20120227T132700" | 15 // - "20120227T132700" |
16 // - "20120227" | 16 // - "20120227" |
17 // - "2012-02-27T14Z" | 17 // - "2012-02-27T14Z" |
18 // - "-123450101 00:00:00 Z" // In the year -12345. | 18 // - "-123450101 00:00:00 Z" // In the year -12345. |
19 final RegExp re = const RegExp( | 19 final RegExp re = const RegExp( |
20 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. | 20 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. |
21 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); | 21 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); |
22 Match match = re.firstMatch(formattedString); | 22 Match match = re.firstMatch(formattedString); |
23 if (match !== null) { | 23 if (match !== null) { |
24 int parseIntOrZero(String matched) { | 24 int parseIntOrZero(String matched) { |
25 // TODO(floitsch): we should not need to test against the empty string. | 25 // TODO(floitsch): we should not need to test against the empty string. |
26 if (matched === null || matched == "") return 0; | 26 if (matched === null || matched == "") return 0; |
27 return Math.parseInt(matched); | 27 return parseInt(matched); |
28 } | 28 } |
29 | 29 |
30 double parseDoubleOrZero(String matched) { | 30 double parseDoubleOrZero(String matched) { |
31 // TODO(floitsch): we should not need to test against the empty string. | 31 // TODO(floitsch): we should not need to test against the empty string. |
32 if (matched === null || matched == "") return 0.0; | 32 if (matched === null || matched == "") return 0.0; |
33 return Math.parseDouble(matched); | 33 return parseDouble(matched); |
34 } | 34 } |
35 | 35 |
36 int years = Math.parseInt(match[1]); | 36 int years = parseInt(match[1]); |
37 int month = Math.parseInt(match[2]); | 37 int month = parseInt(match[2]); |
38 int day = Math.parseInt(match[3]); | 38 int day = parseInt(match[3]); |
39 int hour = parseIntOrZero(match[4]); | 39 int hour = parseIntOrZero(match[4]); |
40 int minute = parseIntOrZero(match[5]); | 40 int minute = parseIntOrZero(match[5]); |
41 int second = parseIntOrZero(match[6]); | 41 int second = parseIntOrZero(match[6]); |
42 bool addOneMillisecond = false; | 42 bool addOneMillisecond = false; |
43 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); | 43 int millisecond = (parseDoubleOrZero(match[7]) * 1000).round().toInt(); |
44 if (millisecond == 1000) { | 44 if (millisecond == 1000) { |
45 addOneMillisecond = true; | 45 addOneMillisecond = true; |
46 millisecond = 999; | 46 millisecond = 999; |
47 } | 47 } |
48 // TODO(floitsch): we should not need to test against the empty string. | 48 // TODO(floitsch): we should not need to test against the empty string. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 external Duration get timeZoneOffset(); | 175 external Duration get timeZoneOffset(); |
176 external int get year(); | 176 external int get year(); |
177 external int get month(); | 177 external int get month(); |
178 external int get day(); | 178 external int get day(); |
179 external int get hour(); | 179 external int get hour(); |
180 external int get minute(); | 180 external int get minute(); |
181 external int get second(); | 181 external int get second(); |
182 external int get millisecond(); | 182 external int get millisecond(); |
183 external int get weekday(); | 183 external int get weekday(); |
184 } | 184 } |
OLD | NEW |