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

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

Issue 12221087: Add +, - and * to Duration. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added ~/ operator. Created 7 years, 10 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 [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 {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 int minutes: 0, 43 int minutes: 0,
44 int seconds: 0, 44 int seconds: 0,
45 int milliseconds: 0}) 45 int milliseconds: 0})
46 : inMilliseconds = days * Duration.MILLISECONDS_PER_DAY + 46 : inMilliseconds = days * Duration.MILLISECONDS_PER_DAY +
47 hours * Duration.MILLISECONDS_PER_HOUR + 47 hours * Duration.MILLISECONDS_PER_HOUR +
48 minutes * Duration.MILLISECONDS_PER_MINUTE + 48 minutes * Duration.MILLISECONDS_PER_MINUTE +
49 seconds * Duration.MILLISECONDS_PER_SECOND + 49 seconds * Duration.MILLISECONDS_PER_SECOND +
50 milliseconds; 50 milliseconds;
51 51
52 /** 52 /**
53 * Returns the sum of this [Duration] and [other] as a new [Duration].
54 */
55 Duration operator +(Duration other) {
56 return new Duration(milliseconds: inMilliseconds + other.inMilliseconds);
57 }
58
59 /**
60 * Returns the difference of this [Duration] and [other] as a new
61 * [Duration].
62 */
63 Duration operator -(Duration other) {
64 return new Duration(milliseconds: inMilliseconds - other.inMilliseconds);
65 }
66
67 /**
68 * Multiplies this [Duration] by the given [factor] and returns the result
69 * as a new [Duration].
70 */
71 Duration operator *(int factor) {
72 return new Duration(milliseconds: inMilliseconds * factor);
73 }
74
75 /**
76 * Divides this [Duration] by the given [quotient] and returns the truncated
77 * result as a new [Duration].
78 *
79 * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`.
80 */
81 Duration operator ~/(int quotient) {
82 // By doing the check here instead of relying on "~/" below we get the
83 // exception even with dart2js.
84 if (quotient == 0) throw new IntegerDivisionByZeroException();
85 return new Duration(milliseconds: inMilliseconds ~/ quotient);
86 }
87
88 bool operator <(Duration other) => this.inMilliseconds < other.inMilliseconds;
89
90 bool operator >(Duration other) => this.inMilliseconds > other.inMilliseconds;
91
92 bool operator <=(Duration other) =>
93 this.inMilliseconds <= other.inMilliseconds;
94
95 bool operator >=(Duration other) =>
96 this.inMilliseconds >= other.inMilliseconds;
97
98 /**
53 * This [Duration] in days. Incomplete days are discarded 99 * This [Duration] in days. Incomplete days are discarded
54 */ 100 */
55 int get inDays { 101 int get inDays {
56 return inMilliseconds ~/ Duration.MILLISECONDS_PER_DAY; 102 return inMilliseconds ~/ Duration.MILLISECONDS_PER_DAY;
57 } 103 }
58 104
59 /** 105 /**
60 * This [Duration] in hours. Incomplete hours are discarded. 106 * This [Duration] in hours. Incomplete hours are discarded.
61 * The returned value can be greater than 23. 107 * The returned value can be greater than 23.
62 */ 108 */
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } 157 }
112 String twoDigitMinutes = 158 String twoDigitMinutes =
113 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR)); 159 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR));
114 String twoDigitSeconds = 160 String twoDigitSeconds =
115 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE)); 161 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE));
116 String threeDigitMs = 162 String threeDigitMs =
117 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND)); 163 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND));
118 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$threeDigitMs"; 164 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$threeDigitMs";
119 } 165 }
120 } 166 }
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