| 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 // TODO(jmesserly): the native class should be the real JS Date. | 5 // TODO(jmesserly): the native class should be the real JS Date. |
| 6 // TODO(jimhug): Making the date value non-lazy might be a good path there. | 6 // TODO(jimhug): Making the date value non-lazy might be a good path there. |
| 7 class DateImplementation implements Date { | 7 class DateImplementation implements Date { |
| 8 final int value; | 8 final int value; |
| 9 final TimeZone timeZone; | 9 final TimeZone timeZone; |
| 10 | 10 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 // Examples: | 48 // Examples: |
| 49 // - "2012-02-27 13:27:00" | 49 // - "2012-02-27 13:27:00" |
| 50 // - "2012-02-27 13:27:00.423z" | 50 // - "2012-02-27 13:27:00.423z" |
| 51 // - "20120227 13:27:00" | 51 // - "20120227 13:27:00" |
| 52 // - "20120227T132700" | 52 // - "20120227T132700" |
| 53 // - "20120227" | 53 // - "20120227" |
| 54 // - "2012-02-27T14Z" | 54 // - "2012-02-27T14Z" |
| 55 // - "-123450101 00:00:00 Z" // In the year -12345. | 55 // - "-123450101 00:00:00 Z" // In the year -12345. |
| 56 final RegExp re = const RegExp( | 56 final RegExp re = const RegExp( |
| 57 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' + // The day part. | 57 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' + // The day part. |
| 58 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:.(\d{1,5}))?)?)? ?([zZ])?)?$'); | 58 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); |
| 59 Match match = re.firstMatch(formattedString); | 59 Match match = re.firstMatch(formattedString); |
| 60 if (match !== null) { | 60 if (match !== null) { |
| 61 int parseIntOrZero(String matched) { | 61 int parseIntOrZero(String matched) { |
| 62 // TODO(floitsch): we should not need to test against the empty string. | 62 // TODO(floitsch): we should not need to test against the empty string. |
| 63 if (matched === null || matched == "") return 0; | 63 if (matched === null || matched == "") return 0; |
| 64 return Math.parseInt(matched); | 64 return Math.parseInt(matched); |
| 65 } | 65 } |
| 66 | 66 |
| 67 double parseDoubleOrZero(String matched) { |
| 68 // TODO(floitsch): we should not need to test against the empty string. |
| 69 if (matched === null || matched == "") return 0.0; |
| 70 return Math.parseDouble(matched); |
| 71 } |
| 72 |
| 67 int years = Math.parseInt(match[1]); | 73 int years = Math.parseInt(match[1]); |
| 68 int month = Math.parseInt(match[2]); | 74 int month = Math.parseInt(match[2]); |
| 69 int day = Math.parseInt(match[3]); | 75 int day = Math.parseInt(match[3]); |
| 70 int hours = parseIntOrZero(match[4]); | 76 int hours = parseIntOrZero(match[4]); |
| 71 int minutes = parseIntOrZero(match[5]); | 77 int minutes = parseIntOrZero(match[5]); |
| 72 int seconds = parseIntOrZero(match[6]); | 78 int seconds = parseIntOrZero(match[6]); |
| 73 bool addOneMillisecond = false; | 79 bool addOneMillisecond = false; |
| 74 int milliseconds = parseIntOrZero(match[7]); | 80 int milliseconds = (parseDoubleOrZero(match[7]) * 1000).round(); |
| 75 if (milliseconds != 0) { | 81 if (milliseconds == 1000) { |
| 76 if (match[7].length == 1) { | 82 addOneMillisecond = true; |
| 77 milliseconds *= 100; | 83 milliseconds = 999; |
| 78 } else if (match[7].length == 2) { | |
| 79 milliseconds *= 10; | |
| 80 } else if (match[7].length == 3) { | |
| 81 // Do nothing. | |
| 82 } else if (match[7].length == 4) { | |
| 83 addOneMillisecond = ((milliseconds % 10) >= 5); | |
| 84 milliseconds ~/= 10; | |
| 85 } else { | |
| 86 assert(match[7].length == 5); | |
| 87 addOneMillisecond = ((milliseconds %100) >= 50); | |
| 88 milliseconds ~/= 100; | |
| 89 } | |
| 90 if (addOneMillisecond && milliseconds < 999) { | |
| 91 addOneMillisecond = false; | |
| 92 milliseconds++; | |
| 93 } | |
| 94 } | 84 } |
| 95 // TODO(floitsch): we should not need to test against the empty string. | 85 // TODO(floitsch): we should not need to test against the empty string. |
| 96 bool isUtc = (match[8] !== null) && (match[8] != ""); | 86 bool isUtc = (match[8] !== null) && (match[8] != ""); |
| 97 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); | 87 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); |
| 98 int epochValue = _valueFromDecomposed( | 88 int epochValue = _valueFromDecomposed( |
| 99 years, month, day, hours, minutes, seconds, milliseconds, isUtc); | 89 years, month, day, hours, minutes, seconds, milliseconds, isUtc); |
| 100 if (epochValue === null) { | 90 if (epochValue === null) { |
| 101 throw new IllegalArgumentException(formattedString); | 91 throw new IllegalArgumentException(formattedString); |
| 102 } | 92 } |
| 103 if (addOneMillisecond) epochValue++; | 93 if (addOneMillisecond) epochValue++; |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 return isUtc == other.isUtc; | 271 return isUtc == other.isUtc; |
| 282 } | 272 } |
| 283 | 273 |
| 284 String toString() { | 274 String toString() { |
| 285 if (isUtc) return "TimeZone (UTC)"; | 275 if (isUtc) return "TimeZone (UTC)"; |
| 286 return "TimeZone (Local)"; | 276 return "TimeZone (Local)"; |
| 287 } | 277 } |
| 288 | 278 |
| 289 final bool isUtc; | 279 final bool isUtc; |
| 290 } | 280 } |
| OLD | NEW |