| 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 part of dart.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A span of time, such as 27 days, 4 hours, 12 minutes, and 3 seconds. | 8 * A span of time, such as 27 days, 4 hours, 12 minutes, and 3 seconds. |
| 9 * | 9 * |
| 10 * A `Duration` represents a difference from one point in time to another. The | 10 * A `Duration` represents a difference from one point in time to another. The |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 * | 85 * |
| 86 * All individual parts are allowed to be negative. | 86 * All individual parts are allowed to be negative. |
| 87 * All arguments are 0 by default. | 87 * All arguments are 0 by default. |
| 88 */ | 88 */ |
| 89 const Duration({int days: 0, | 89 const Duration({int days: 0, |
| 90 int hours: 0, | 90 int hours: 0, |
| 91 int minutes: 0, | 91 int minutes: 0, |
| 92 int seconds: 0, | 92 int seconds: 0, |
| 93 int milliseconds: 0, | 93 int milliseconds: 0, |
| 94 int microseconds: 0}) | 94 int microseconds: 0}) |
| 95 : _duration = days * MICROSECONDS_PER_DAY + | 95 : this._microseconds( |
| 96 hours * MICROSECONDS_PER_HOUR + | 96 days * MICROSECONDS_PER_DAY + |
| 97 minutes * MICROSECONDS_PER_MINUTE + | 97 hours * MICROSECONDS_PER_HOUR + |
| 98 seconds * MICROSECONDS_PER_SECOND + | 98 minutes * MICROSECONDS_PER_MINUTE + |
| 99 milliseconds * MICROSECONDS_PER_MILLISECOND + | 99 seconds * MICROSECONDS_PER_SECOND + |
| 100 microseconds; | 100 milliseconds * MICROSECONDS_PER_MILLISECOND + |
| 101 microseconds); |
| 102 |
| 103 // Fast path internal direct constructor to avoids the optional arguments and |
| 104 // [_microseconds] recomputation. |
| 105 const Duration._microseconds(this._duration); |
| 101 | 106 |
| 102 /** | 107 /** |
| 103 * Adds this Duration and [other] and | 108 * Adds this Duration and [other] and |
| 104 * returns the sum as a new Duration object. | 109 * returns the sum as a new Duration object. |
| 105 */ | 110 */ |
| 106 Duration operator +(Duration other) { | 111 Duration operator +(Duration other) { |
| 107 return new Duration(microseconds: _duration + other._duration); | 112 return new Duration._microseconds(_duration + other._duration); |
| 108 } | 113 } |
| 109 | 114 |
| 110 /** | 115 /** |
| 111 * Subtracts [other] from this Duration and | 116 * Subtracts [other] from this Duration and |
| 112 * returns the difference as a new Duration object. | 117 * returns the difference as a new Duration object. |
| 113 */ | 118 */ |
| 114 Duration operator -(Duration other) { | 119 Duration operator -(Duration other) { |
| 115 return new Duration(microseconds: _duration - other._duration); | 120 return new Duration._microseconds(_duration - other._duration); |
| 116 } | 121 } |
| 117 | 122 |
| 118 /** | 123 /** |
| 119 * Multiplies this Duration by the given [factor] and returns the result | 124 * Multiplies this Duration by the given [factor] and returns the result |
| 120 * as a new Duration object. | 125 * as a new Duration object. |
| 121 * | 126 * |
| 122 * Note that when [factor] is a double, and the duration is greater than | 127 * Note that when [factor] is a double, and the duration is greater than |
| 123 * 53 bits, precision is lost because of double-precision arithmetic. | 128 * 53 bits, precision is lost because of double-precision arithmetic. |
| 124 */ | 129 */ |
| 125 Duration operator *(num factor) { | 130 Duration operator *(num factor) { |
| 126 return new Duration(microseconds: (_duration * factor).round()); | 131 return new Duration._microseconds((_duration * factor).round()); |
| 127 } | 132 } |
| 128 | 133 |
| 129 /** | 134 /** |
| 130 * Divides this Duration by the given [quotient] and returns the truncated | 135 * Divides this Duration by the given [quotient] and returns the truncated |
| 131 * result as a new Duration object. | 136 * result as a new Duration object. |
| 132 * | 137 * |
| 133 * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`. | 138 * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`. |
| 134 */ | 139 */ |
| 135 Duration operator ~/(int quotient) { | 140 Duration operator ~/(int quotient) { |
| 136 // By doing the check here instead of relying on "~/" below we get the | 141 // By doing the check here instead of relying on "~/" below we get the |
| 137 // exception even with dart2js. | 142 // exception even with dart2js. |
| 138 if (quotient == 0) throw new IntegerDivisionByZeroException(); | 143 if (quotient == 0) throw new IntegerDivisionByZeroException(); |
| 139 return new Duration(microseconds: _duration ~/ quotient); | 144 return new Duration._microseconds(_duration ~/ quotient); |
| 140 } | 145 } |
| 141 | 146 |
| 142 /** | 147 /** |
| 143 * Returns `true` if the value of this Duration | 148 * Returns `true` if the value of this Duration |
| 144 * is less than the value of [other]. | 149 * is less than the value of [other]. |
| 145 */ | 150 */ |
| 146 bool operator <(Duration other) => this._duration < other._duration; | 151 bool operator <(Duration other) => this._duration < other._duration; |
| 147 | 152 |
| 148 /** | 153 /** |
| 149 * Returns `true` if the value of this Duration | 154 * Returns `true` if the value of this Duration |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 if (n >= 100) return "000$n"; | 246 if (n >= 100) return "000$n"; |
| 242 if (n >= 10) return "0000$n"; | 247 if (n >= 10) return "0000$n"; |
| 243 return "00000$n"; | 248 return "00000$n"; |
| 244 } | 249 } |
| 245 String twoDigits(int n) { | 250 String twoDigits(int n) { |
| 246 if (n >= 10) return "$n"; | 251 if (n >= 10) return "$n"; |
| 247 return "0$n"; | 252 return "0$n"; |
| 248 } | 253 } |
| 249 | 254 |
| 250 if (inMicroseconds < 0) { | 255 if (inMicroseconds < 0) { |
| 251 Duration duration = | 256 return "-${-this}"; |
| 252 new Duration(microseconds: -inMicroseconds); | |
| 253 return "-$duration"; | |
| 254 } | 257 } |
| 255 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); | 258 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); |
| 256 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); | 259 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); |
| 257 String sixDigitUs = | 260 String sixDigitUs = |
| 258 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND)); | 261 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND)); |
| 259 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs"; | 262 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs"; |
| 260 } | 263 } |
| 261 | 264 |
| 262 /** | 265 /** |
| 263 * Returns whether this `Duration` is negative. | 266 * Returns whether this `Duration` is negative. |
| 264 * | 267 * |
| 265 * A negative `Duration` represents the difference from a later time to an | 268 * A negative `Duration` represents the difference from a later time to an |
| 266 * earlier time. | 269 * earlier time. |
| 267 */ | 270 */ |
| 268 bool get isNegative => _duration < 0; | 271 bool get isNegative => _duration < 0; |
| 269 | 272 |
| 270 /** | 273 /** |
| 271 * Returns a new `Duration` representing the absolute value of this | 274 * Returns a new `Duration` representing the absolute value of this |
| 272 * `Duration`. | 275 * `Duration`. |
| 273 * | 276 * |
| 274 * The returned `Duration` has the same length as this one, but is always | 277 * The returned `Duration` has the same length as this one, but is always |
| 275 * positive. | 278 * positive. |
| 276 */ | 279 */ |
| 277 Duration abs() => new Duration(microseconds: _duration.abs()); | 280 Duration abs() => new Duration._microseconds(_duration.abs()); |
| 278 | 281 |
| 279 /** | 282 /** |
| 280 * Returns a new `Duration` representing this `Duration` negated. | 283 * Returns a new `Duration` representing this `Duration` negated. |
| 281 * | 284 * |
| 282 * The returned `Duration` has the same length as this one, but will have the | 285 * The returned `Duration` has the same length as this one, but will have the |
| 283 * opposite sign of this one. | 286 * opposite sign of this one. |
| 284 */ | 287 */ |
| 285 Duration operator -() => new Duration(microseconds: -_duration); | 288 Duration operator -() => new Duration._microseconds(-_duration); |
| 286 } | 289 } |
| OLD | NEW |