| 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 r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. | 20 r'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' // The day part. |
| 21 r'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); | 21 r'(?:[ 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 int.parse(matched); | 27 return int.parse(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 double.parse(matched); | 33 return double.parse(matched); |
| 34 } | 34 } |
| 35 | 35 |
| 36 int years = int.parse(match[1]); | 36 int years = int.parse(match[1]); |
| 37 int month = int.parse(match[2]); | 37 int month = int.parse(match[2]); |
| 38 int day = int.parse(match[3]); | 38 int day = int.parse(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. |
| 49 bool isUtc = (match[8] != null) && (match[8] != ""); | 49 bool isUtc = (match[8] !== null) && (match[8] != ""); |
| 50 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( | 50 int millisecondsSinceEpoch = _brokenDownDateToMillisecondsSinceEpoch( |
| 51 years, month, day, hour, minute, second, millisecond, isUtc); | 51 years, month, day, hour, minute, second, millisecond, isUtc); |
| 52 if (millisecondsSinceEpoch == null) { | 52 if (millisecondsSinceEpoch === null) { |
| 53 throw new ArgumentError(formattedString); | 53 throw new ArgumentError(formattedString); |
| 54 } | 54 } |
| 55 if (addOneMillisecond) millisecondsSinceEpoch++; | 55 if (addOneMillisecond) millisecondsSinceEpoch++; |
| 56 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, | 56 return new Date.fromMillisecondsSinceEpoch(millisecondsSinceEpoch, |
| 57 isUtc: isUtc); | 57 isUtc: isUtc); |
| 58 } else { | 58 } else { |
| 59 throw new ArgumentError(formattedString); | 59 throw new ArgumentError(formattedString); |
| 60 } | 60 } |
| 61 } | 61 } |
| 62 | 62 |
| 63 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; | 63 static const int _MAX_MILLISECONDS_SINCE_EPOCH = 8640000000000000; |
| 64 | 64 |
| 65 DateImplementation.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch, | 65 DateImplementation.fromMillisecondsSinceEpoch(this.millisecondsSinceEpoch, |
| 66 this.isUtc) { | 66 this.isUtc) { |
| 67 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { | 67 if (millisecondsSinceEpoch.abs() > _MAX_MILLISECONDS_SINCE_EPOCH) { |
| 68 throw new ArgumentError(millisecondsSinceEpoch); | 68 throw new ArgumentError(millisecondsSinceEpoch); |
| 69 } | 69 } |
| 70 if (isUtc == null) throw new ArgumentError(isUtc); | 70 if (isUtc === null) throw new ArgumentError(isUtc); |
| 71 } | 71 } |
| 72 | 72 |
| 73 bool operator ==(other) { | 73 bool operator ==(other) { |
| 74 if (!(other is Date)) return false; | 74 if (!(other is Date)) return false; |
| 75 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch); | 75 return (millisecondsSinceEpoch == other.millisecondsSinceEpoch); |
| 76 } | 76 } |
| 77 | 77 |
| 78 bool operator <(Date other) | 78 bool operator <(Date other) |
| 79 => millisecondsSinceEpoch < other.millisecondsSinceEpoch; | 79 => millisecondsSinceEpoch < other.millisecondsSinceEpoch; |
| 80 | 80 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 external Duration get timeZoneOffset; | 178 external Duration get timeZoneOffset; |
| 179 external int get year; | 179 external int get year; |
| 180 external int get month; | 180 external int get month; |
| 181 external int get day; | 181 external int get day; |
| 182 external int get hour; | 182 external int get hour; |
| 183 external int get minute; | 183 external int get minute; |
| 184 external int get second; | 184 external int get second; |
| 185 external int get millisecond; | 185 external int get millisecond; |
| 186 external int get weekday; | 186 external int get weekday; |
| 187 } | 187 } |
| OLD | NEW |