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

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

Issue 431263004: Adding Duration documentation, abs(), and isNegative() (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Adding unary-, and better text as per lrn@ Created 6 years, 4 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 | tests/corelib/duration_test.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 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
11 * duration may be "negative" if the difference is from a later time to an
12 * earlier.
13 *
10 * To create a new Duration object, use this class's single constructor 14 * To create a new Duration object, use this class's single constructor
11 * giving the appropriate arguments: 15 * giving the appropriate arguments:
12 * 16 *
13 * Duration fastestMarathon = new Duration(hours:2, minutes:3, seconds:2); 17 * Duration fastestMarathon = new Duration(hours:2, minutes:3, seconds:2);
14 * 18 *
15 * The Duration is the sum of all individual parts. 19 * The Duration is the sum of all individual parts.
16 * This means that individual parts can be larger than the next-bigger unit. 20 * This means that individual parts can be larger than the next-bigger unit.
17 * For example, [minutes] can be greater than 59. 21 * For example, [minutes] can be greater than 59.
18 * 22 *
19 * assert(fastestMarathon.inMinutes == 123); 23 * assert(fastestMarathon.inMinutes == 123);
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 * Returns `true` if this Duration is the same object as [other]. 205 * Returns `true` if this Duration is the same object as [other].
202 */ 206 */
203 bool operator ==(other) { 207 bool operator ==(other) {
204 if (other is !Duration) return false; 208 if (other is !Duration) return false;
205 return _duration == other._duration; 209 return _duration == other._duration;
206 } 210 }
207 211
208 int get hashCode => _duration.hashCode; 212 int get hashCode => _duration.hashCode;
209 213
210 /** 214 /**
211 * Compares this Duration to [other], 215 * Compares this Duration to [other], returning zero if the values are equal.
212 * returning zero if the values are equal.
213 * 216 *
214 * This function returns a negative integer 217 * Returns a negative integer if this `Duration` is shorter than
215 * if this Duration is smaller than [other], 218 * [other], or a positive integer if it is longer.
216 * or a positive integer if it is greater. 219 *
220 * A negative `Duration` is always considered shorter than a positive one.
221 *
222 * It is always the case that `duration1.compareTo(duration2) < 0` iff
223 * `(someDate + duration1).compareTo(someDate + duration2) < 0`.
217 */ 224 */
218 int compareTo(Duration other) => _duration.compareTo(other._duration); 225 int compareTo(Duration other) => _duration.compareTo(other._duration);
219 226
227 /**
228 * Returns a string representation of this `Duration`.
229 *
230 * Returns a string with hours, minutes, seconds, and microseconds, in the
231 * following format: `HH:MM:SS.mmmmmm`. For example,
232 *
233 * var d = new Duration(days:1, hours:1, minutes:33, microseconds: 500);
234 * d.toString(); // "25:33:00.000500"
235 */
220 String toString() { 236 String toString() {
221 String sixDigits(int n) { 237 String sixDigits(int n) {
222 if (n >= 100000) return "$n"; 238 if (n >= 100000) return "$n";
223 if (n >= 10000) return "0$n"; 239 if (n >= 10000) return "0$n";
224 if (n >= 1000) return "00$n"; 240 if (n >= 1000) return "00$n";
225 if (n >= 100) return "000$n"; 241 if (n >= 100) return "000$n";
226 if (n >= 10) return "0000$n"; 242 if (n >= 10) return "0000$n";
227 return "00000$n"; 243 return "00000$n";
228 } 244 }
229 String twoDigits(int n) { 245 String twoDigits(int n) {
230 if (n >= 10) return "$n"; 246 if (n >= 10) return "$n";
231 return "0$n"; 247 return "0$n";
232 } 248 }
233 249
234 if (inMicroseconds < 0) { 250 if (inMicroseconds < 0) {
235 Duration duration = 251 Duration duration =
236 new Duration(microseconds: -inMicroseconds); 252 new Duration(microseconds: -inMicroseconds);
237 return "-$duration"; 253 return "-$duration";
238 } 254 }
239 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); 255 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR));
240 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); 256 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE));
241 String sixDigitUs = 257 String sixDigitUs =
242 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND)); 258 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND));
243 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs"; 259 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs";
244 } 260 }
261
262 /**
263 * Returns whether this `Duration` is negative.
264 *
265 * A negative `Duration` represents the difference from a later time to an
266 * earlier time.
267 */
268 bool get isNegative => _duration < 0;
269
270 /**
271 * Returns a new `Duration` representing the absolute value of this
272 * `Duration`.
273 *
274 * The returned `Duration` has the same length as this one, but is always
275 * positive.
276 */
277 Duration abs() => new Duration(microseconds: _duration.abs());
278
279 /**
280 * Returns a new `Duration` representing this `Duration` negated.
281 *
282 * The returned `Duration` has the same length as this one, but will have the
283 * opposite sign of this one.
284 */
285 Duration operator -() => new Duration(microseconds: -_duration);
245 } 286 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/duration_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698