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

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: Add comparison operators to Duration. 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') | tests/corelib/duration_test.dart » ('J')
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 {
Lasse Reichstein Nielsen 2013/02/09 01:13:30 We should add a type variable to Comparable, so it
floitsch 2013/02/09 02:08:04 Definitely. Filed http://dartbug.com/8445
11 static const int MILLISECONDS_PER_SECOND = 1000; 11 static const int MILLISECONDS_PER_SECOND = 1000;
12 static const int SECONDS_PER_MINUTE = 60; 12 static const int SECONDS_PER_MINUTE = 60;
13 static const int MINUTES_PER_HOUR = 60; 13 static const int MINUTES_PER_HOUR = 60;
14 static const int HOURS_PER_DAY = 24; 14 static const int HOURS_PER_DAY = 24;
15 15
16 static const int MILLISECONDS_PER_MINUTE = 16 static const int MILLISECONDS_PER_MINUTE =
17 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE; 17 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
18 static const int MILLISECONDS_PER_HOUR = 18 static const int MILLISECONDS_PER_HOUR =
19 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR; 19 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR;
20 static const int MILLISECONDS_PER_DAY = 20 static const int MILLISECONDS_PER_DAY =
(...skipping 22 matching lines...) Expand all
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 }
Lasse Reichstein Nielsen 2013/02/09 01:13:30 Consider having ~/ too.
floitsch 2013/02/09 02:08:04 Done.
74
75 bool operator <(Duration other) => this.inMilliseconds < other.inMilliseconds;
76
77 bool operator >(Duration other) => this.inMilliseconds > other.inMilliseconds;
78
79 bool operator <=(Duration other) =>
80 this.inMilliseconds <= other.inMilliseconds;
81
82 bool operator >=(Duration other) =>
83 this.inMilliseconds >= other.inMilliseconds;
84
85 /**
53 * This [Duration] in days. Incomplete days are discarded 86 * This [Duration] in days. Incomplete days are discarded
54 */ 87 */
55 int get inDays { 88 int get inDays {
56 return inMilliseconds ~/ Duration.MILLISECONDS_PER_DAY; 89 return inMilliseconds ~/ Duration.MILLISECONDS_PER_DAY;
57 } 90 }
58 91
59 /** 92 /**
60 * This [Duration] in hours. Incomplete hours are discarded. 93 * This [Duration] in hours. Incomplete hours are discarded.
61 * The returned value can be greater than 23. 94 * The returned value can be greater than 23.
62 */ 95 */
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } 144 }
112 String twoDigitMinutes = 145 String twoDigitMinutes =
113 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR)); 146 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR));
114 String twoDigitSeconds = 147 String twoDigitSeconds =
115 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE)); 148 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE));
116 String threeDigitMs = 149 String threeDigitMs =
117 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND)); 150 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND));
118 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$threeDigitMs"; 151 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$threeDigitMs";
119 } 152 }
120 } 153 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/duration_test.dart » ('j') | tests/corelib/duration_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698