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

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

Issue 17591020: Let operator* in Duration accept doubles. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: reupload Created 7 years, 5 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
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<Duration> { 10 class Duration implements Comparable<Duration> {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 * is allowed to have a value greater than 23. 49 * is allowed to have a value greater than 23.
50 * 50 *
51 * All individual parts are allowed to be negative. 51 * All individual parts are allowed to be negative.
52 * All arguments are by default 0. 52 * All arguments are by default 0.
53 */ 53 */
54 const Duration({int days: 0, 54 const Duration({int days: 0,
55 int hours: 0, 55 int hours: 0,
56 int minutes: 0, 56 int minutes: 0,
57 int seconds: 0, 57 int seconds: 0,
58 int milliseconds: 0, 58 int milliseconds: 0,
59 int microseconds: 0}) 59 int microseconds: 0})
floitsch 2013/06/25 15:28:02 Remove trailing space.
zarah 2013/06/25 15:54:05 Done.
60 : _duration = days * MICROSECONDS_PER_DAY + 60 : _duration = days * MICROSECONDS_PER_DAY +
61 hours * MICROSECONDS_PER_HOUR + 61 hours * MICROSECONDS_PER_HOUR +
62 minutes * MICROSECONDS_PER_MINUTE + 62 minutes * MICROSECONDS_PER_MINUTE +
63 seconds * MICROSECONDS_PER_SECOND + 63 seconds * MICROSECONDS_PER_SECOND +
64 milliseconds * MICROSECONDS_PER_MILLISECOND + 64 milliseconds * MICROSECONDS_PER_MILLISECOND +
65 microseconds; 65 microseconds;
66 66
67 /** 67 /**
68 * 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].
69 */ 69 */
70 Duration operator +(Duration other) { 70 Duration operator +(Duration other) {
71 return new Duration(microseconds: _duration + other._duration); 71 return new Duration(microseconds: _duration + other._duration);
72 } 72 }
73 73
74 /** 74 /**
75 * Returns the difference of this [Duration] and [other] as a new 75 * Returns the difference of this [Duration] and [other] as a new
76 * [Duration]. 76 * [Duration].
77 */ 77 */
78 Duration operator -(Duration other) { 78 Duration operator -(Duration other) {
79 return new Duration(microseconds: _duration - other._duration); 79 return new Duration(microseconds: _duration - other._duration);
80 } 80 }
81 81
82 /** 82 /**
83 * Multiplies this [Duration] by the given [factor] and returns the result 83 * Multiplies this [Duration] by the given [factor] and returns the result
84 * as a new [Duration]. 84 * as a new [Duration].
85 *
86 * Note when [factor] is a double, and the duration is greater than
floitsch 2013/06/25 15:28:02 Note that ... or: Note: ...
zarah 2013/06/25 15:54:05 Done.
87 * 53 bits, precision is lost because of double-precision arithmetic.
85 */ 88 */
86 Duration operator *(int factor) { 89 Duration operator *(num factor) {
87 return new Duration(microseconds: _duration * factor); 90 return new Duration(microseconds: (_duration * factor).round());
88 } 91 }
89 92
90 /** 93 /**
91 * Divides this [Duration] by the given [quotient] and returns the truncated 94 * Divides this [Duration] by the given [quotient] and returns the truncated
92 * result as a new [Duration]. 95 * result as a new [Duration].
93 * 96 *
94 * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`. 97 * Throws an [IntegerDivisionByZeroException] if [quotient] is `0`.
95 */ 98 */
96 Duration operator ~/(int quotient) { 99 Duration operator ~/(int quotient) {
97 // By doing the check here instead of relying on "~/" below we get the 100 // By doing the check here instead of relying on "~/" below we get the
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 new Duration(microseconds: -inMicroseconds); 177 new Duration(microseconds: -inMicroseconds);
175 return "-$duration"; 178 return "-$duration";
176 } 179 }
177 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR)); 180 String twoDigitMinutes = twoDigits(inMinutes.remainder(MINUTES_PER_HOUR));
178 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE)); 181 String twoDigitSeconds = twoDigits(inSeconds.remainder(SECONDS_PER_MINUTE));
179 String sixDigitUs = 182 String sixDigitUs =
180 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND)); 183 sixDigits(inMicroseconds.remainder(MICROSECONDS_PER_SECOND));
181 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs"; 184 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$sixDigitUs";
182 } 185 }
183 } 186 }
OLDNEW
« no previous file with comments | « no previous file | tests/corelib/duration_big_num_test.dart » ('j') | tests/corelib/duration_big_num_test.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698