Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: sdk/lib/core/duration.dart

Issue 12389056: Add Duration.inMicroseconds and add Stopwatch.elapsed. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/core/stopwatch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 [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 class Duration implements Comparable { 10 class Duration implements Comparable<Duration> {
11 static const int MICROSECONDS_PER_MILLISECOND = 1000;
11 static const int MILLISECONDS_PER_SECOND = 1000; 12 static const int MILLISECONDS_PER_SECOND = 1000;
12 static const int SECONDS_PER_MINUTE = 60; 13 static const int SECONDS_PER_MINUTE = 60;
13 static const int MINUTES_PER_HOUR = 60; 14 static const int MINUTES_PER_HOUR = 60;
14 static const int HOURS_PER_DAY = 24; 15 static const int HOURS_PER_DAY = 24;
15 16
17 static const int MICROSECONDS_PER_SECOND =
18 MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND;
19 static const int MICROSECONDS_PER_MINUTE =
20 MICROSECONDS_PER_SECOND * SECONDS_PER_MINUTE;
21 static const int MICROSECONDS_PER_HOUR =
22 MICROSECONDS_PER_MINUTE * MINUTES_PER_HOUR;
23 static const int MICROSECONDS_PER_DAY =
24 MICROSECONDS_PER_HOUR * HOURS_PER_DAY;
25
26
16 static const int MILLISECONDS_PER_MINUTE = 27 static const int MILLISECONDS_PER_MINUTE =
17 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE; 28 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
18 static const int MILLISECONDS_PER_HOUR = 29 static const int MILLISECONDS_PER_HOUR =
19 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR; 30 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR;
20 static const int MILLISECONDS_PER_DAY = 31 static const int MILLISECONDS_PER_DAY =
21 MILLISECONDS_PER_HOUR * HOURS_PER_DAY; 32 MILLISECONDS_PER_HOUR * HOURS_PER_DAY;
22 33
23 static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; 34 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; 35 static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
25 36
26 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY; 37 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;
27 38
28 static const Duration ZERO = const Duration(seconds: 0); 39 static const Duration ZERO = const Duration(seconds: 0);
29 40
30 /** 41 /**
31 * This [Duration] in milliseconds. 42 * This [Duration] in microseconds.
32 */ 43 */
33 final int inMilliseconds; 44 final int _duration;
Lasse Reichstein Nielsen 2013/03/04 09:11:05 Why not name it inMicroseconds instead, and not ha
floitsch 2013/03/04 18:02:28 I prefer to be explicit when a class has an interf
34 45
35 /** 46 /**
36 * The duration is the sum of all individual parts. This means that individual 47 * The duration is the sum of all individual parts. This means that individual
37 * parts don't need to be less than the next-bigger unit. For example [hours] 48 * parts don't need to be less than the next-bigger unit. For example [hours]
38 * is allowed to have a value greater than 23. 49 * is allowed to have a value greater than 23.
39 * 50 *
40 * All individual parts are allowed to be negative. 51 * All individual parts are allowed to be negative.
41 * All arguments are by default 0. 52 * All arguments are by default 0.
42 */ 53 */
43 const Duration({int days: 0, 54 const Duration({int days: 0,
44 int hours: 0, 55 int hours: 0,
45 int minutes: 0, 56 int minutes: 0,
46 int seconds: 0, 57 int seconds: 0,
47 int milliseconds: 0}) 58 int milliseconds: 0,
48 : inMilliseconds = days * Duration.MILLISECONDS_PER_DAY + 59 int microseconds: 0})
49 hours * Duration.MILLISECONDS_PER_HOUR + 60 : _duration = days * MICROSECONDS_PER_DAY +
50 minutes * Duration.MILLISECONDS_PER_MINUTE + 61 hours * MICROSECONDS_PER_HOUR +
51 seconds * Duration.MILLISECONDS_PER_SECOND + 62 minutes * MICROSECONDS_PER_MINUTE +
52 milliseconds; 63 seconds * MICROSECONDS_PER_SECOND +
64 milliseconds * MICROSECONDS_PER_MILLISECOND +
65 microseconds;
53 66
54 /** 67 /**
55 * Returns the sum of this [Duration] and [other] as a new [Duration]. 68 * Returns the sum of this [Duration] and [other] as a new [Duration].
56 */ 69 */
57 Duration operator +(Duration other) { 70 Duration operator +(Duration other) {
58 return new Duration(milliseconds: inMilliseconds + other.inMilliseconds); 71 return new Duration(microseconds: _duration + other._duration);
59 } 72 }
60 73
61 /** 74 /**
62 * Returns the difference of this [Duration] and [other] as a new 75 * Returns the difference of this [Duration] and [other] as a new
63 * [Duration]. 76 * [Duration].
64 */ 77 */
65 Duration operator -(Duration other) { 78 Duration operator -(Duration other) {
66 return new Duration(milliseconds: inMilliseconds - other.inMilliseconds); 79 return new Duration(microseconds: _duration - other._duration);
67 } 80 }
68 81
69 /** 82 /**
70 * Multiplies this [Duration] by the given [factor] and returns the result 83 * Multiplies this [Duration] by the given [factor] and returns the result
71 * as a new [Duration]. 84 * as a new [Duration].
72 */ 85 */
73 Duration operator *(int factor) { 86 Duration operator *(int factor) {
74 return new Duration(milliseconds: inMilliseconds * factor); 87 return new Duration(microseconds: _duration * factor);
75 } 88 }
76 89
77 /** 90 /**
78 * Divides this [Duration] by the given [quotient] and returns the truncated 91 * Divides this [Duration] by the given [quotient] and returns the truncated
79 * result as a new [Duration]. 92 * result as a new [Duration].
80 * 93 *
81 * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`. 94 * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`.
82 */ 95 */
83 Duration operator ~/(int quotient) { 96 Duration operator ~/(int quotient) {
84 // By doing the check here instead of relying on "~/" below we get the 97 // By doing the check here instead of relying on "~/" below we get the
85 // exception even with dart2js. 98 // exception even with dart2js.
86 if (quotient == 0) throw new IntegerDivisionByZeroException(); 99 if (quotient == 0) throw new IntegerDivisionByZeroException();
87 return new Duration(milliseconds: inMilliseconds ~/ quotient); 100 return new Duration(microseconds: _duration ~/ quotient);
88 } 101 }
89 102
90 bool operator <(Duration other) => this.inMilliseconds < other.inMilliseconds; 103 bool operator <(Duration other) => this._duration < other._duration;
91 104
92 bool operator >(Duration other) => this.inMilliseconds > other.inMilliseconds; 105 bool operator >(Duration other) => this._duration > other._duration;
93 106
94 bool operator <=(Duration other) => 107 bool operator <=(Duration other) => this._duration <= other._duration;
95 this.inMilliseconds <= other.inMilliseconds;
96 108
97 bool operator >=(Duration other) => 109 bool operator >=(Duration other) => this._duration >= other._duration;
98 this.inMilliseconds >= other.inMilliseconds;
99 110
100 /** 111 /**
101 * This [Duration] in days. Incomplete days are discarded 112 * This [Duration] in days. Incomplete days are discarded
102 */ 113 */
103 int get inDays { 114 int get inDays => _duration ~/ Duration.MICROSECONDS_PER_DAY;
104 return inMilliseconds ~/ Duration.MILLISECONDS_PER_DAY;
105 }
106 115
107 /** 116 /**
108 * This [Duration] in hours. Incomplete hours are discarded. 117 * This [Duration] in hours. Incomplete hours are discarded.
118 *
109 * The returned value can be greater than 23. 119 * The returned value can be greater than 23.
110 */ 120 */
111 int get inHours { 121 int get inHours => _duration ~/ Duration.MICROSECONDS_PER_HOUR;
112 return inMilliseconds ~/ Duration.MILLISECONDS_PER_HOUR;
113 }
114 122
115 /** 123 /**
116 * This [Duration] in minutes. Incomplete minutes are discarded. 124 * This [Duration] in minutes. Incomplete minutes are discarded.
125 *
117 * The returned value can be greater than 59. 126 * The returned value can be greater than 59.
118 */ 127 */
119 int get inMinutes { 128 int get inMinutes => _duration ~/ Duration.MICROSECONDS_PER_MINUTE;
120 return inMilliseconds ~/ Duration.MILLISECONDS_PER_MINUTE;
121 }
122 129
123 /** 130 /**
124 * This [Duration] in seconds. Incomplete seconds are discarded. 131 * This [Duration] in seconds. Incomplete seconds are discarded.
132 *
125 * The returned value can be greater than 59. 133 * The returned value can be greater than 59.
126 */ 134 */
127 int get inSeconds { 135 int get inSeconds => _duration ~/ Duration.MICROSECONDS_PER_SECOND;
128 return inMilliseconds ~/ Duration.MILLISECONDS_PER_SECOND; 136
129 } 137 /**
138 * This [Duration] in milliseconds. Incomplete milliseconds are discarded.
139 *
140 * The returned value can be greater than 999.
141 */
142 int get inMilliseconds => _duration ~/ Duration.MICROSECONDS_PER_MILLISECOND;
143
144 /**
145 * This [Duration] in microseconds.
146 */
147 int get inMicroseconds => _duration;
130 148
131 bool operator ==(other) { 149 bool operator ==(other) {
132 if (other is !Duration) return false; 150 if (other is !Duration) return false;
133 return inMilliseconds == other.inMilliseconds; 151 return _duration == other._duration;
134 } 152 }
135 153
136 int get hashCode { 154 int get hashCode => _duration.hashCode;
137 return inMilliseconds.hashCode;
138 }
139 155
140 int compareTo(Duration other) { 156 int compareTo(Duration other) => _duration.compareTo(other._duration);
141 return inMilliseconds.compareTo(other.inMilliseconds);
142 }
143 157
144 String toString() { 158 String toString() {
145 String threeDigits(int n) { 159 String sixDigits(int n) {
146 if (n >= 100) return "$n"; 160 if (n >= 100000) return "$n";
147 if (n > 10) return "0$n"; 161 if (n >= 10000) return "0$n";
148 return "00$n"; 162 if (n >= 1000) return "00$n";
163 if (n >= 100) return "000$n";
164 if (n > 10) return "0000$n";
165 return "00000$n";
149 } 166 }
150 String twoDigits(int n) { 167 String twoDigits(int n) {
151 if (n >= 10) return "$n"; 168 if (n >= 10) return "$n";
152 return "0$n"; 169 return "0$n";
153 } 170 }
154 171
155 if (inMilliseconds < 0) { 172 if (inMicroseconds < 0) {
156 Duration duration = 173 Duration duration =
157 new Duration(milliseconds: -inMilliseconds); 174 new Duration(microseconds: -inMicroseconds);
158 return "-$duration"; 175 return "-$duration";
159 } 176 }
160 String twoDigitMinutes = 177 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR));
161 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR)); 178 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE));
162 String twoDigitSeconds = 179 String sixDigitUs =
163 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE)); 180 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND));
164 String threeDigitMs = 181 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs";
165 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND));
166 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$threeDigitMs";
167 } 182 }
168 } 183 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/core/stopwatch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698