| 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 * To create a new Duration object, use this class's single constructor | 10 * To create a new Duration object, use this class's single constructor |
| 11 * giving the appropriate arguments: | 11 * giving the appropriate arguments: |
| 12 * | 12 * |
| 13 * Duration fastestMarathon = new Duration(hours:2, minutes:3, seconds:2); | 13 * Duration fastestMarathon = new Duration(hours:2, minutes:3, seconds:2); |
| 14 * | 14 * |
| 15 * The Duration is the sum of all individual parts. | 15 * The Duration is the sum of all individual parts. |
| 16 * This means that individual parts can be larger than the next-bigger unit. | 16 * This means that individual parts can be larger than the next-bigger unit. |
| 17 * For example, [minutes] can be greater than 59. | 17 * For example, [minutes] can be greater than 59. |
| 18 * | 18 * |
| 19 * assert(fastestMarathon.inMinutes == 123); | 19 * assert(fastestMarathon.inMinutes == 123); |
| 20 * | 20 * |
| 21 * All individual parts are allowed to be negative. | 21 * All individual parts are allowed to be negative. |
| 22 * | 22 * |
| 23 * Use one of the properties, such as [inDays], | 23 * Use one of the properties, such as [inDays], |
| 24 * to retrieve the integer value of the Duration in the specified time unit. | 24 * to retrieve the integer value of the Duration in the specified time unit. |
| 25 * Note that the returned value is rounded down. | 25 * Note that the returned value is rounded down. |
| 26 * For example, | 26 * For example, |
| 27 * | 27 * |
| 28 * Duration aLongWeekend = new Duration(hours:88); | 28 * Duration aLongWeekend = new Duration(hours:88); |
| 29 * assert(aLongWeekend.inDays == 3); | 29 * assert(aLongWeekend.inDays == 3); |
| 30 * | 30 * |
| 31 * This class provides a collection of arithmetic | 31 * This class provides a collection of arithmetic |
| 32 * and comparison operators, | 32 * and comparison operators, |
| 33 * plus a set of constants useful for converting time units. | 33 * plus a set of constants useful for converting time units. |
| 34 * | 34 * |
| 35 * See [DateTime] to represent a point in time. | 35 * See [DateTime] to represent a point in time. |
| 36 * See [Stopwatch] to measure time-spans. | 36 * See [Stopwatch] to measure time-spans. |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 * or a positive integer if it is greater. | 216 * or a positive integer if it is greater. |
| 217 */ | 217 */ |
| 218 int compareTo(Duration other) => _duration.compareTo(other._duration); | 218 int compareTo(Duration other) => _duration.compareTo(other._duration); |
| 219 | 219 |
| 220 String toString() { | 220 String toString() { |
| 221 String sixDigits(int n) { | 221 String sixDigits(int n) { |
| 222 if (n >= 100000) return "$n"; | 222 if (n >= 100000) return "$n"; |
| 223 if (n >= 10000) return "0$n"; | 223 if (n >= 10000) return "0$n"; |
| 224 if (n >= 1000) return "00$n"; | 224 if (n >= 1000) return "00$n"; |
| 225 if (n >= 100) return "000$n"; | 225 if (n >= 100) return "000$n"; |
| 226 if (n > 10) return "0000$n"; | 226 if (n >= 10) return "0000$n"; |
| 227 return "00000$n"; | 227 return "00000$n"; |
| 228 } | 228 } |
| 229 String twoDigits(int n) { | 229 String twoDigits(int n) { |
| 230 if (n >= 10) return "$n"; | 230 if (n >= 10) return "$n"; |
| 231 return "0$n"; | 231 return "0$n"; |
| 232 } | 232 } |
| 233 | 233 |
| 234 if (inMicroseconds < 0) { | 234 if (inMicroseconds < 0) { |
| 235 Duration duration = | 235 Duration duration = |
| 236 new Duration(microseconds: -inMicroseconds); | 236 new Duration(microseconds: -inMicroseconds); |
| 237 return "-$duration"; | 237 return "-$duration"; |
| 238 } | 238 } |
| 239 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); | 239 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); |
| 240 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); | 240 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); |
| 241 String sixDigitUs = | 241 String sixDigitUs = |
| 242 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND)); | 242 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND)); |
| 243 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs"; | 243 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs"; |
| 244 } | 244 } |
| 245 } | 245 } |
| OLD | NEW |