| 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 // Dart core library. | 4 // Dart core library. |
| 5 | 5 |
| 6 class TimeZoneImplementation implements TimeZone { | 6 class TimeZoneImplementation implements TimeZone { |
| 7 const TimeZoneImplementation.utc() : isUtc = true; | 7 const TimeZoneImplementation.utc() : isUtc = true; |
| 8 TimeZoneImplementation.local() : isUtc = false {} | 8 TimeZoneImplementation.local() : isUtc = false {} |
| 9 | 9 |
| 10 bool operator ==(Object other) { | 10 bool operator ==(Object other) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 // Examples: | 55 // Examples: |
| 56 // - "2012-02-27 13:27:00" | 56 // - "2012-02-27 13:27:00" |
| 57 // - "2012-02-27 13:27:00.423z" | 57 // - "2012-02-27 13:27:00.423z" |
| 58 // - "20120227 13:27:00" | 58 // - "20120227 13:27:00" |
| 59 // - "20120227T132700" | 59 // - "20120227T132700" |
| 60 // - "20120227" | 60 // - "20120227" |
| 61 // - "2012-02-27T14Z" | 61 // - "2012-02-27T14Z" |
| 62 // - "-123450101 00:00:00 Z" // In the year -12345. | 62 // - "-123450101 00:00:00 Z" // In the year -12345. |
| 63 final RegExp re = const RegExp( | 63 final RegExp re = const RegExp( |
| 64 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' + // The day part. | 64 @'^([+-]?\d?\d\d\d\d)-?(\d\d)-?(\d\d)' + // The day part. |
| 65 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(?:.(\d{1,5}))?)?)? ?([zZ])?)?$'); | 65 @'(?:[ T](\d\d)(?::?(\d\d)(?::?(\d\d)(.\d{1,6})?)?)? ?([zZ])?)?$'); |
| 66 Match match = re.firstMatch(formattedString); | 66 Match match = re.firstMatch(formattedString); |
| 67 if (match !== null) { | 67 if (match !== null) { |
| 68 int parseIntOrZero(String matched) { | 68 int parseIntOrZero(String matched) { |
| 69 // TODO(floitsch): we should not need to test against the empty string. | 69 // TODO(floitsch): we should not need to test against the empty string. |
| 70 if (matched === null || matched == "") return 0; | 70 if (matched === null || matched == "") return 0; |
| 71 return Math.parseInt(matched); | 71 return Math.parseInt(matched); |
| 72 } | 72 } |
| 73 | 73 |
| 74 double parseDoubleOrZero(String matched) { |
| 75 // TODO(floitsch): we should not need to test against the empty string. |
| 76 if (matched === null || matched == "") return 0.0; |
| 77 return Math.parseDouble(matched); |
| 78 } |
| 79 |
| 74 int years = Math.parseInt(match[1]); | 80 int years = Math.parseInt(match[1]); |
| 75 int month = Math.parseInt(match[2]); | 81 int month = Math.parseInt(match[2]); |
| 76 int day = Math.parseInt(match[3]); | 82 int day = Math.parseInt(match[3]); |
| 77 int hours = parseIntOrZero(match[4]); | 83 int hours = parseIntOrZero(match[4]); |
| 78 int minutes = parseIntOrZero(match[5]); | 84 int minutes = parseIntOrZero(match[5]); |
| 79 int seconds = parseIntOrZero(match[6]); | 85 int seconds = parseIntOrZero(match[6]); |
| 80 bool addOneMillisecond = false; | 86 bool addOneMillisecond = false; |
| 81 int milliseconds = parseIntOrZero(match[7]); | 87 int milliseconds = (parseDoubleOrZero(match[7]) * 1000).round(); |
| 82 if (milliseconds != 0) { | 88 if (milliseconds == 1000) { |
| 83 if (match[7].length == 1) { | 89 addOneMillisecond = true; |
| 84 milliseconds *= 100; | 90 milliseconds = 999; |
| 85 } else if (match[7].length == 2) { | |
| 86 milliseconds *= 10; | |
| 87 } else if (match[7].length == 3) { | |
| 88 // Do nothing. | |
| 89 } else if (match[7].length == 4) { | |
| 90 addOneMillisecond = ((milliseconds % 10) >= 5); | |
| 91 milliseconds ~/= 10; | |
| 92 } else { | |
| 93 assert(match[7].length == 5); | |
| 94 addOneMillisecond = ((milliseconds %100) >= 50); | |
| 95 milliseconds ~/= 100; | |
| 96 } | |
| 97 if (addOneMillisecond && milliseconds < 999) { | |
| 98 addOneMillisecond = false; | |
| 99 milliseconds++; | |
| 100 } | |
| 101 } | 91 } |
| 102 // TODO(floitsch): we should not need to test against the empty string. | 92 // TODO(floitsch): we should not need to test against the empty string. |
| 103 bool isUtc = (match[8] !== null) && (match[8] != ""); | 93 bool isUtc = (match[8] !== null) && (match[8] != ""); |
| 104 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); | 94 TimeZone timezone = isUtc ? const TimeZone.utc() : new TimeZone.local(); |
| 105 int epochValue = _brokenDownDateToMillisecondsSinceEpoch( | 95 int epochValue = _brokenDownDateToMillisecondsSinceEpoch( |
| 106 years, month, day, hours, minutes, seconds, milliseconds, isUtc); | 96 years, month, day, hours, minutes, seconds, milliseconds, isUtc); |
| 107 if (epochValue === null) { | 97 if (epochValue === null) { |
| 108 throw new IllegalArgumentException(formattedString); | 98 throw new IllegalArgumentException(formattedString); |
| 109 } | 99 } |
| 110 if (addOneMillisecond) epochValue++; | 100 if (addOneMillisecond) epochValue++; |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 | 407 |
| 418 static int getHours_(int secondsSinceEpoch, bool isUtc) | 408 static int getHours_(int secondsSinceEpoch, bool isUtc) |
| 419 native "DateNatives_getHours"; | 409 native "DateNatives_getHours"; |
| 420 | 410 |
| 421 static int getMinutes_(int secondsSinceEpoch, bool isUtc) | 411 static int getMinutes_(int secondsSinceEpoch, bool isUtc) |
| 422 native "DateNatives_getMinutes"; | 412 native "DateNatives_getMinutes"; |
| 423 | 413 |
| 424 static int getSeconds_(int secondsSinceEpoch, bool isUtc) | 414 static int getSeconds_(int secondsSinceEpoch, bool isUtc) |
| 425 native "DateNatives_getSeconds"; | 415 native "DateNatives_getSeconds"; |
| 426 } | 416 } |
| OLD | NEW |