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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 static const int SECONDS_PER_MINUTE = 60; | 56 static const int SECONDS_PER_MINUTE = 60; |
57 static const int MINUTES_PER_HOUR = 60; | 57 static const int MINUTES_PER_HOUR = 60; |
58 static const int HOURS_PER_DAY = 24; | 58 static const int HOURS_PER_DAY = 24; |
59 | 59 |
60 static const int MICROSECONDS_PER_SECOND = | 60 static const int MICROSECONDS_PER_SECOND = |
61 MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND; | 61 MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND; |
62 static const int MICROSECONDS_PER_MINUTE = | 62 static const int MICROSECONDS_PER_MINUTE = |
63 MICROSECONDS_PER_SECOND * SECONDS_PER_MINUTE; | 63 MICROSECONDS_PER_SECOND * SECONDS_PER_MINUTE; |
64 static const int MICROSECONDS_PER_HOUR = | 64 static const int MICROSECONDS_PER_HOUR = |
65 MICROSECONDS_PER_MINUTE * MINUTES_PER_HOUR; | 65 MICROSECONDS_PER_MINUTE * MINUTES_PER_HOUR; |
66 static const int MICROSECONDS_PER_DAY = | 66 static const int MICROSECONDS_PER_DAY = MICROSECONDS_PER_HOUR * HOURS_PER_DAY; |
67 MICROSECONDS_PER_HOUR * HOURS_PER_DAY; | |
68 | |
69 | 67 |
70 static const int MILLISECONDS_PER_MINUTE = | 68 static const int MILLISECONDS_PER_MINUTE = |
71 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE; | 69 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE; |
72 static const int MILLISECONDS_PER_HOUR = | 70 static const int MILLISECONDS_PER_HOUR = |
73 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR; | 71 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR; |
74 static const int MILLISECONDS_PER_DAY = | 72 static const int MILLISECONDS_PER_DAY = MILLISECONDS_PER_HOUR * HOURS_PER_DAY; |
75 MILLISECONDS_PER_HOUR * HOURS_PER_DAY; | |
76 | 73 |
77 static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; | 74 static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; |
78 static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; | 75 static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; |
79 | 76 |
80 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY; | 77 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY; |
81 | 78 |
82 static const Duration ZERO = const Duration(seconds: 0); | 79 static const Duration ZERO = const Duration(seconds: 0); |
83 | 80 |
84 /* | 81 /* |
85 * The value of this Duration object in microseconds. | 82 * The value of this Duration object in microseconds. |
86 */ | 83 */ |
87 final int _duration; | 84 final int _duration; |
88 | 85 |
89 /** | 86 /** |
90 * Creates a new Duration object whose value | 87 * Creates a new Duration object whose value |
91 * is the sum of all individual parts. | 88 * is the sum of all individual parts. |
92 * | 89 * |
93 * Individual parts can be larger than the next-bigger unit. | 90 * Individual parts can be larger than the next-bigger unit. |
94 * For example, [hours] can be greater than 23. | 91 * For example, [hours] can be greater than 23. |
95 * | 92 * |
96 * All individual parts are allowed to be negative. | 93 * All individual parts are allowed to be negative. |
97 * All arguments are 0 by default. | 94 * All arguments are 0 by default. |
98 */ | 95 */ |
99 const Duration({int days: 0, | 96 const Duration( |
100 int hours: 0, | 97 {int days: 0, |
101 int minutes: 0, | 98 int hours: 0, |
102 int seconds: 0, | 99 int minutes: 0, |
103 int milliseconds: 0, | 100 int seconds: 0, |
104 int microseconds: 0}) | 101 int milliseconds: 0, |
105 : this._microseconds( | 102 int microseconds: 0}) |
106 MICROSECONDS_PER_DAY * days + | 103 : this._microseconds(MICROSECONDS_PER_DAY * days + |
107 MICROSECONDS_PER_HOUR * hours + | 104 MICROSECONDS_PER_HOUR * hours + |
108 MICROSECONDS_PER_MINUTE * minutes + | 105 MICROSECONDS_PER_MINUTE * minutes + |
109 MICROSECONDS_PER_SECOND * seconds + | 106 MICROSECONDS_PER_SECOND * seconds + |
110 MICROSECONDS_PER_MILLISECOND * milliseconds + | 107 MICROSECONDS_PER_MILLISECOND * milliseconds + |
111 microseconds); | 108 microseconds); |
112 | 109 |
113 // Fast path internal direct constructor to avoids the optional arguments and | 110 // Fast path internal direct constructor to avoids the optional arguments and |
114 // [_microseconds] recomputation. | 111 // [_microseconds] recomputation. |
115 const Duration._microseconds(this._duration); | 112 const Duration._microseconds(this._duration); |
116 | 113 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 210 |
214 /** | 211 /** |
215 * Returns number of whole microseconds spanned by this Duration. | 212 * Returns number of whole microseconds spanned by this Duration. |
216 */ | 213 */ |
217 int get inMicroseconds => _duration; | 214 int get inMicroseconds => _duration; |
218 | 215 |
219 /** | 216 /** |
220 * Returns `true` if this Duration is the same object as [other]. | 217 * Returns `true` if this Duration is the same object as [other]. |
221 */ | 218 */ |
222 bool operator ==(other) { | 219 bool operator ==(other) { |
223 if (other is !Duration) return false; | 220 if (other is! Duration) return false; |
224 return _duration == other._duration; | 221 return _duration == other._duration; |
225 } | 222 } |
226 | 223 |
227 int get hashCode => _duration.hashCode; | 224 int get hashCode => _duration.hashCode; |
228 | 225 |
229 /** | 226 /** |
230 * Compares this Duration to [other], returning zero if the values are equal. | 227 * Compares this Duration to [other], returning zero if the values are equal. |
231 * | 228 * |
232 * Returns a negative integer if this `Duration` is shorter than | 229 * Returns a negative integer if this `Duration` is shorter than |
233 * [other], or a positive integer if it is longer. | 230 * [other], or a positive integer if it is longer. |
(...skipping 16 matching lines...) Expand all Loading... |
250 */ | 247 */ |
251 String toString() { | 248 String toString() { |
252 String sixDigits(int n) { | 249 String sixDigits(int n) { |
253 if (n >= 100000) return "$n"; | 250 if (n >= 100000) return "$n"; |
254 if (n >= 10000) return "0$n"; | 251 if (n >= 10000) return "0$n"; |
255 if (n >= 1000) return "00$n"; | 252 if (n >= 1000) return "00$n"; |
256 if (n >= 100) return "000$n"; | 253 if (n >= 100) return "000$n"; |
257 if (n >= 10) return "0000$n"; | 254 if (n >= 10) return "0000$n"; |
258 return "00000$n"; | 255 return "00000$n"; |
259 } | 256 } |
| 257 |
260 String twoDigits(int n) { | 258 String twoDigits(int n) { |
261 if (n >= 10) return "$n"; | 259 if (n >= 10) return "$n"; |
262 return "0$n"; | 260 return "0$n"; |
263 } | 261 } |
264 | 262 |
265 if (inMicroseconds < 0) { | 263 if (inMicroseconds < 0) { |
266 return "-${-this}"; | 264 return "-${-this}"; |
267 } | 265 } |
268 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); | 266 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); |
269 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); | 267 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); |
(...skipping 21 matching lines...) Expand all Loading... |
291 | 289 |
292 /** | 290 /** |
293 * Returns a new `Duration` representing this `Duration` negated. | 291 * Returns a new `Duration` representing this `Duration` negated. |
294 * | 292 * |
295 * The returned `Duration` has the same length as this one, but will have the | 293 * The returned `Duration` has the same length as this one, but will have the |
296 * opposite sign of this one. | 294 * opposite sign of this one. |
297 */ | 295 */ |
298 // Using subtraction helps dart2js avoid negative zeros. | 296 // Using subtraction helps dart2js avoid negative zeros. |
299 Duration operator -() => new Duration._microseconds(0 - _duration); | 297 Duration operator -() => new Duration._microseconds(0 - _duration); |
300 } | 298 } |
OLD | NEW |