| 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 // Dart core library. | 5 // Dart core library. |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A [Duration] represents a time span. A duration can be negative. | 8 * A [Duration] represents a time span. A duration can be negative. |
| 9 */ | 9 */ |
| 10 interface Duration extends Comparable default DurationImplementation { | 10 class Duration implements Comparable, Hashable { |
| 11 static const int MILLISECONDS_PER_SECOND = 1000; |
| 12 static const int SECONDS_PER_MINUTE = 60; |
| 13 static const int MINUTES_PER_HOUR = 60; |
| 14 static const int HOURS_PER_DAY = 24; |
| 15 |
| 16 static const int MILLISECONDS_PER_MINUTE = |
| 17 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE; |
| 18 static const int MILLISECONDS_PER_HOUR = |
| 19 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR; |
| 20 static const int MILLISECONDS_PER_DAY = |
| 21 MILLISECONDS_PER_HOUR * HOURS_PER_DAY; |
| 22 |
| 23 static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; |
| 24 static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; |
| 25 |
| 26 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY; |
| 27 |
| 28 /** |
| 29 * This [Duration] in milliseconds. |
| 30 */ |
| 31 final int inMilliseconds; |
| 32 |
| 11 /** | 33 /** |
| 12 * The duration is the sum of all individual parts. This means that individual | 34 * The duration is the sum of all individual parts. This means that individual |
| 13 * parts don't need to be less than the next-bigger unit. For example [hours] | 35 * parts don't need to be less than the next-bigger unit. For example [hours] |
| 14 * is allowed to have a value greater than 23. | 36 * is allowed to have a value greater than 23. |
| 15 * | 37 * |
| 16 * All individual parts are allowed to be negative. | 38 * All individual parts are allowed to be negative. |
| 17 * All arguments are by default 0. | 39 * All arguments are by default 0. |
| 18 */ | 40 */ |
| 19 const Duration({int days, int hours, int minutes, int seconds, | 41 const Duration({int days: 0, |
| 20 int milliseconds}); | 42 int hours: 0, |
| 43 int minutes: 0, |
| 44 int seconds: 0, |
| 45 int milliseconds: 0}) |
| 46 : inMilliseconds = days * Duration.MILLISECONDS_PER_DAY + |
| 47 hours * Duration.MILLISECONDS_PER_HOUR + |
| 48 minutes * Duration.MILLISECONDS_PER_MINUTE + |
| 49 seconds * Duration.MILLISECONDS_PER_SECOND + |
| 50 milliseconds; |
| 21 | 51 |
| 22 /** | 52 /** |
| 23 * Returns this [Duration] in days. Incomplete days are discarded. | 53 * This [Duration] in days. Incomplete days are discarded |
| 24 */ | 54 */ |
| 25 int get inDays; | 55 int get inDays { |
| 56 return inMilliseconds ~/ Duration.MILLISECONDS_PER_DAY; |
| 57 } |
| 26 | 58 |
| 27 /** | 59 /** |
| 28 * Returns this [Duration] in hours. Incomplete hours are discarded. | 60 * This [Duration] in hours. Incomplete hours are discarded. |
| 29 * The returned value can be greater than 23. | 61 * The returned value can be greater than 23. |
| 30 */ | 62 */ |
| 31 int get inHours; | 63 int get inHours { |
| 64 return inMilliseconds ~/ Duration.MILLISECONDS_PER_HOUR; |
| 65 } |
| 32 | 66 |
| 33 /** | 67 /** |
| 34 * Returns this [Duration] in minutes. Incomplete minutes are discarded. | 68 * This [Duration] in minutes. Incomplete minutes are discarded. |
| 35 * The returned value can be greater than 59. | 69 * The returned value can be greater than 59. |
| 36 */ | 70 */ |
| 37 int get inMinutes; | 71 int get inMinutes { |
| 72 return inMilliseconds ~/ Duration.MILLISECONDS_PER_MINUTE; |
| 73 } |
| 38 | 74 |
| 39 /** | 75 /** |
| 40 * Returns this [Duration] in seconds. Incomplete seconds are discarded. | 76 * This [Duration] in seconds. Incomplete seconds are discarded. |
| 41 * The returned value can be greater than 59. | 77 * The returned value can be greater than 59. |
| 42 */ | 78 */ |
| 43 int get inSeconds; | 79 int get inSeconds { |
| 80 return inMilliseconds ~/ Duration.MILLISECONDS_PER_SECOND; |
| 81 } |
| 44 | 82 |
| 45 /** | 83 bool operator ==(other) { |
| 46 * Returns this [Duration] in milliseconds. | 84 if (other is !Duration) return false; |
| 47 */ | 85 return inMilliseconds == other.inMilliseconds; |
| 48 int get inMilliseconds; | 86 } |
| 49 | 87 |
| 50 static const int MILLISECONDS_PER_SECOND = 1000; | 88 int hashCode() { |
| 51 static const int SECONDS_PER_MINUTE = 60; | 89 return inMilliseconds.hashCode(); |
| 52 static const int MINUTES_PER_HOUR = 60; | 90 } |
| 53 static const int HOURS_PER_DAY = 24; | |
| 54 | 91 |
| 55 static const int MILLISECONDS_PER_MINUTE = | 92 int compareTo(Duration other) { |
| 56 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE; | 93 return inMilliseconds.compareTo(other.inMilliseconds); |
| 57 static const int MILLISECONDS_PER_HOUR = | 94 } |
| 58 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR; | |
| 59 static const int MILLISECONDS_PER_DAY = | |
| 60 MILLISECONDS_PER_HOUR * HOURS_PER_DAY; | |
| 61 | 95 |
| 62 static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; | 96 String toString() { |
| 63 static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; | 97 String threeDigits(int n) { |
| 98 if (n >= 100) return "$n"; |
| 99 if (n > 10) return "0$n"; |
| 100 return "00$n"; |
| 101 } |
| 102 String twoDigits(int n) { |
| 103 if (n >= 10) return "$n"; |
| 104 return "0$n"; |
| 105 } |
| 64 | 106 |
| 65 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY; | 107 if (inMilliseconds < 0) { |
| 108 Duration duration = |
| 109 new Duration(milliseconds: -inMilliseconds); |
| 110 return "-$duration"; |
| 111 } |
| 112 String twoDigitMinutes = |
| 113 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR)); |
| 114 String twoDigitSeconds = |
| 115 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE)); |
| 116 String threeDigitMs = |
| 117 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND)); |
| 118 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$threeDigitMs"; |
| 119 } |
| 66 } | 120 } |
| OLD | NEW |