| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.core; | |
| 6 | |
| 7 /** | |
| 8 * A span of time, such as 27 days, 4 hours, 12 minutes, and 3 seconds. | |
| 9 * | |
| 10 * A `Duration` represents a difference from one point in time to another. The | |
| 11 * duration may be "negative" if the difference is from a later time to an | |
| 12 * earlier. | |
| 13 * | |
| 14 * To create a new Duration object, use this class's single constructor | |
| 15 * giving the appropriate arguments: | |
| 16 * | |
| 17 * Duration fastestMarathon = new Duration(hours:2, minutes:3, seconds:2); | |
| 18 * | |
| 19 * The Duration is the sum of all individual parts. | |
| 20 * This means that individual parts can be larger than the next-bigger unit. | |
| 21 * For example, [minutes] can be greater than 59. | |
| 22 * | |
| 23 * assert(fastestMarathon.inMinutes == 123); | |
| 24 * | |
| 25 * All individual parts are allowed to be negative. | |
| 26 * | |
| 27 * Use one of the properties, such as [inDays], | |
| 28 * to retrieve the integer value of the Duration in the specified time unit. | |
| 29 * Note that the returned value is rounded down. | |
| 30 * For example, | |
| 31 * | |
| 32 * Duration aLongWeekend = new Duration(hours:88); | |
| 33 * assert(aLongWeekend.inDays == 3); | |
| 34 * | |
| 35 * This class provides a collection of arithmetic | |
| 36 * and comparison operators, | |
| 37 * plus a set of constants useful for converting time units. | |
| 38 * | |
| 39 * See [DateTime] to represent a point in time. | |
| 40 * See [Stopwatch] to measure time-spans. | |
| 41 * | |
| 42 */ | |
| 43 class Duration implements Comparable<Duration> { | |
| 44 static const int MICROSECONDS_PER_MILLISECOND = 1000; | |
| 45 static const int MILLISECONDS_PER_SECOND = 1000; | |
| 46 static const int SECONDS_PER_MINUTE = 60; | |
| 47 static const int MINUTES_PER_HOUR = 60; | |
| 48 static const int HOURS_PER_DAY = 24; | |
| 49 | |
| 50 static const int MICROSECONDS_PER_SECOND = | |
| 51 MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND; | |
| 52 static const int MICROSECONDS_PER_MINUTE = | |
| 53 MICROSECONDS_PER_SECOND * SECONDS_PER_MINUTE; | |
| 54 static const int MICROSECONDS_PER_HOUR = | |
| 55 MICROSECONDS_PER_MINUTE * MINUTES_PER_HOUR; | |
| 56 static const int MICROSECONDS_PER_DAY = | |
| 57 MICROSECONDS_PER_HOUR * HOURS_PER_DAY; | |
| 58 | |
| 59 | |
| 60 static const int MILLISECONDS_PER_MINUTE = | |
| 61 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE; | |
| 62 static const int MILLISECONDS_PER_HOUR = | |
| 63 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR; | |
| 64 static const int MILLISECONDS_PER_DAY = | |
| 65 MILLISECONDS_PER_HOUR * HOURS_PER_DAY; | |
| 66 | |
| 67 static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; | |
| 68 static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; | |
| 69 | |
| 70 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY; | |
| 71 | |
| 72 static const Duration ZERO = const Duration(seconds: 0); | |
| 73 | |
| 74 /* | |
| 75 * The value of this Duration object in microseconds. | |
| 76 */ | |
| 77 final int _duration; | |
| 78 | |
| 79 /** | |
| 80 * Creates a new Duration object whose value | |
| 81 * is the sum of all individual parts. | |
| 82 * | |
| 83 * Individual parts can be larger than the next-bigger unit. | |
| 84 * For example, [hours] can be greater than 23. | |
| 85 * | |
| 86 * All individual parts are allowed to be negative. | |
| 87 * All arguments are 0 by default. | |
| 88 */ | |
| 89 const Duration({int days: 0, | |
| 90 int hours: 0, | |
| 91 int minutes: 0, | |
| 92 int seconds: 0, | |
| 93 int milliseconds: 0, | |
| 94 int microseconds: 0}) | |
| 95 : this._microseconds( | |
| 96 MICROSECONDS_PER_DAY * days + | |
| 97 MICROSECONDS_PER_HOUR * hours + | |
| 98 MICROSECONDS_PER_MINUTE * minutes + | |
| 99 MICROSECONDS_PER_SECOND * seconds + | |
| 100 MICROSECONDS_PER_MILLISECOND * milliseconds + | |
| 101 microseconds); | |
| 102 | |
| 103 // Fast path internal direct constructor to avoids the optional arguments and | |
| 104 // [_microseconds] recomputation. | |
| 105 const Duration._microseconds(this._duration); | |
| 106 | |
| 107 /** | |
| 108 * Adds this Duration and [other] and | |
| 109 * returns the sum as a new Duration object. | |
| 110 */ | |
| 111 Duration operator +(Duration other) { | |
| 112 return new Duration._microseconds(_duration + other._duration); | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Subtracts [other] from this Duration and | |
| 117 * returns the difference as a new Duration object. | |
| 118 */ | |
| 119 Duration operator -(Duration other) { | |
| 120 return new Duration._microseconds(_duration - other._duration); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Multiplies this Duration by the given [factor] and returns the result | |
| 125 * as a new Duration object. | |
| 126 * | |
| 127 * Note that when [factor] is a double, and the duration is greater than | |
| 128 * 53 bits, precision is lost because of double-precision arithmetic. | |
| 129 */ | |
| 130 Duration operator *(num factor) { | |
| 131 return new Duration._microseconds((_duration * factor).round()); | |
| 132 } | |
| 133 | |
| 134 /** | |
| 135 * Divides this Duration by the given [quotient] and returns the truncated | |
| 136 * result as a new Duration object. | |
| 137 * | |
| 138 * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`. | |
| 139 */ | |
| 140 Duration operator ~/(int quotient) { | |
| 141 // By doing the check here instead of relying on "~/" below we get the | |
| 142 // exception even with dart2js. | |
| 143 if (quotient == 0) throw new IntegerDivisionByZeroException(); | |
| 144 return new Duration._microseconds(_duration ~/ quotient); | |
| 145 } | |
| 146 | |
| 147 /** | |
| 148 * Returns `true` if the value of this Duration | |
| 149 * is less than the value of [other]. | |
| 150 */ | |
| 151 bool operator <(Duration other) => this._duration < other._duration; | |
| 152 | |
| 153 /** | |
| 154 * Returns `true` if the value of this Duration | |
| 155 * is greater than the value of [other]. | |
| 156 */ | |
| 157 bool operator >(Duration other) => this._duration > other._duration; | |
| 158 | |
| 159 /** | |
| 160 * Returns `true` if the value of this Duration | |
| 161 * is less than or equal to the value of [other]. | |
| 162 */ | |
| 163 bool operator <=(Duration other) => this._duration <= other._duration; | |
| 164 | |
| 165 /** | |
| 166 * Returns `true` if the value of this Duration | |
| 167 * is greater than or equal to the value of [other]. | |
| 168 */ | |
| 169 bool operator >=(Duration other) => this._duration >= other._duration; | |
| 170 | |
| 171 /** | |
| 172 * Returns the number of whole days spanned by this Duration. | |
| 173 */ | |
| 174 int get inDays => _duration ~/ Duration.MICROSECONDS_PER_DAY; | |
| 175 | |
| 176 /** | |
| 177 * Returns the number of whole hours spanned by this Duration. | |
| 178 * | |
| 179 * The returned value can be greater than 23. | |
| 180 */ | |
| 181 int get inHours => _duration ~/ Duration.MICROSECONDS_PER_HOUR; | |
| 182 | |
| 183 /** | |
| 184 * Returns the number of whole minutes spanned by this Duration. | |
| 185 * | |
| 186 * The returned value can be greater than 59. | |
| 187 */ | |
| 188 int get inMinutes => _duration ~/ Duration.MICROSECONDS_PER_MINUTE; | |
| 189 | |
| 190 /** | |
| 191 * Returns the number of whole seconds spanned by this Duration. | |
| 192 * | |
| 193 * The returned value can be greater than 59. | |
| 194 */ | |
| 195 int get inSeconds => _duration ~/ Duration.MICROSECONDS_PER_SECOND; | |
| 196 | |
| 197 /** | |
| 198 * Returns number of whole milliseconds spanned by this Duration. | |
| 199 * | |
| 200 * The returned value can be greater than 999. | |
| 201 */ | |
| 202 int get inMilliseconds => _duration ~/ Duration.MICROSECONDS_PER_MILLISECOND; | |
| 203 | |
| 204 /** | |
| 205 * Returns number of whole microseconds spanned by this Duration. | |
| 206 */ | |
| 207 int get inMicroseconds => _duration; | |
| 208 | |
| 209 /** | |
| 210 * Returns `true` if this Duration is the same object as [other]. | |
| 211 */ | |
| 212 bool operator ==(other) { | |
| 213 if (other is !Duration) return false; | |
| 214 return _duration == other._duration; | |
| 215 } | |
| 216 | |
| 217 int get hashCode => _duration.hashCode; | |
| 218 | |
| 219 /** | |
| 220 * Compares this Duration to [other], returning zero if the values are equal. | |
| 221 * | |
| 222 * Returns a negative integer if this `Duration` is shorter than | |
| 223 * [other], or a positive integer if it is longer. | |
| 224 * | |
| 225 * A negative `Duration` is always considered shorter than a positive one. | |
| 226 * | |
| 227 * It is always the case that `duration1.compareTo(duration2) < 0` iff | |
| 228 * `(someDate + duration1).compareTo(someDate + duration2) < 0`. | |
| 229 */ | |
| 230 int compareTo(Duration other) => _duration.compareTo(other._duration); | |
| 231 | |
| 232 /** | |
| 233 * Returns a string representation of this `Duration`. | |
| 234 * | |
| 235 * Returns a string with hours, minutes, seconds, and microseconds, in the | |
| 236 * following format: `HH:MM:SS.mmmmmm`. For example, | |
| 237 * | |
| 238 * var d = new Duration(days:1, hours:1, minutes:33, microseconds: 500); | |
| 239 * d.toString(); // "25:33:00.000500" | |
| 240 */ | |
| 241 String toString() { | |
| 242 String sixDigits(int n) { | |
| 243 if (n >= 100000) return "$n"; | |
| 244 if (n >= 10000) return "0$n"; | |
| 245 if (n >= 1000) return "00$n"; | |
| 246 if (n >= 100) return "000$n"; | |
| 247 if (n >= 10) return "0000$n"; | |
| 248 return "00000$n"; | |
| 249 } | |
| 250 String twoDigits(int n) { | |
| 251 if (n >= 10) return "$n"; | |
| 252 return "0$n"; | |
| 253 } | |
| 254 | |
| 255 if (inMicroseconds < 0) { | |
| 256 return "-${-this}"; | |
| 257 } | |
| 258 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); | |
| 259 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); | |
| 260 String sixDigitUs = | |
| 261 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND)); | |
| 262 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs"; | |
| 263 } | |
| 264 | |
| 265 /** | |
| 266 * Returns whether this `Duration` is negative. | |
| 267 * | |
| 268 * A negative `Duration` represents the difference from a later time to an | |
| 269 * earlier time. | |
| 270 */ | |
| 271 bool get isNegative => _duration < 0; | |
| 272 | |
| 273 /** | |
| 274 * Returns a new `Duration` representing the absolute value of this | |
| 275 * `Duration`. | |
| 276 * | |
| 277 * The returned `Duration` has the same length as this one, but is always | |
| 278 * positive. | |
| 279 */ | |
| 280 Duration abs() => new Duration._microseconds(_duration.abs()); | |
| 281 | |
| 282 /** | |
| 283 * Returns a new `Duration` representing this `Duration` negated. | |
| 284 * | |
| 285 * The returned `Duration` has the same length as this one, but will have the | |
| 286 * opposite sign of this one. | |
| 287 */ | |
| 288 Duration operator -() => new Duration._microseconds(-_duration); | |
| 289 } | |
| OLD | NEW |