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

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

Issue 10948009: Revert 12494. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 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 | « lib/core/date.dart ('k') | lib/core/exceptions.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 // Dart core library. 5 // Dart core library.
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, Hashable { 10 interface Duration extends Comparable default DurationImplementation {
11 static const int MILLISECONDS_PER_SECOND = 1000;
12 static const int SECONDS_PER_MINUTE = 60;
13 static const int MINUTES_PER_HOUR = 60;
14 static const int HOURS_PER_DAY = 24;
15
16 static const int MILLISECONDS_PER_MINUTE =
17 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
18 static const int MILLISECONDS_PER_HOUR =
19 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR;
20 static const int MILLISECONDS_PER_DAY =
21 MILLISECONDS_PER_HOUR * HOURS_PER_DAY;
22
23 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;
25
26 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;
27
28 /**
29 * This [Duration] in milliseconds.
30 */
31 final int inMilliseconds;
32
33 /** 11 /**
34 * The duration is the sum of all individual parts. This means that individual 12 * The duration is the sum of all individual parts. This means that individual
35 * parts don't need to be less than the next-bigger unit. For example [hours] 13 * parts don't need to be less than the next-bigger unit. For example [hours]
36 * is allowed to have a value greater than 23. 14 * is allowed to have a value greater than 23.
37 * 15 *
38 * All individual parts are allowed to be negative. 16 * All individual parts are allowed to be negative.
39 * All arguments are by default 0. 17 * All arguments are by default 0.
40 */ 18 */
41 const Duration({int days: 0, 19 const Duration({int days, int hours, int minutes, int seconds,
42 int hours: 0, 20 int milliseconds});
43 int minutes: 0,
44 int seconds: 0,
45 int milliseconds: 0})
46 : inMilliseconds = days * Duration.MILLISECONDS_PER_DAY +
47 hours * Duration.MILLISECONDS_PER_HOUR +
48 minutes * Duration.MILLISECONDS_PER_MINUTE +
49 seconds * Duration.MILLISECONDS_PER_SECOND +
50 milliseconds;
51 21
52 /** 22 /**
53 * This [Duration] in days. Incomplete days are discarded 23 * Returns this [Duration] in days. Incomplete days are discarded.
54 */ 24 */
55 int get inDays { 25 int get inDays;
56 return inMilliseconds ~/ Duration.MILLISECONDS_PER_DAY;
57 }
58 26
59 /** 27 /**
60 * This [Duration] in hours. Incomplete hours are discarded. 28 * Returns this [Duration] in hours. Incomplete hours are discarded.
61 * The returned value can be greater than 23. 29 * The returned value can be greater than 23.
62 */ 30 */
63 int get inHours { 31 int get inHours;
64 return inMilliseconds ~/ Duration.MILLISECONDS_PER_HOUR;
65 }
66 32
67 /** 33 /**
68 * This [Duration] in minutes. Incomplete minutes are discarded. 34 * Returns this [Duration] in minutes. Incomplete minutes are discarded.
69 * The returned value can be greater than 59. 35 * The returned value can be greater than 59.
70 */ 36 */
71 int get inMinutes { 37 int get inMinutes;
72 return inMilliseconds ~/ Duration.MILLISECONDS_PER_MINUTE;
73 }
74 38
75 /** 39 /**
76 * This [Duration] in seconds. Incomplete seconds are discarded. 40 * Returns this [Duration] in seconds. Incomplete seconds are discarded.
77 * The returned value can be greater than 59. 41 * The returned value can be greater than 59.
78 */ 42 */
79 int get inSeconds { 43 int get inSeconds;
80 return inMilliseconds ~/ Duration.MILLISECONDS_PER_SECOND;
81 }
82 44
83 bool operator ==(other) { 45 /**
84 if (other is !Duration) return false; 46 * Returns this [Duration] in milliseconds.
85 return inMilliseconds == other.inMilliseconds; 47 */
86 } 48 int get inMilliseconds;
87 49
88 int hashCode() { 50 static const int MILLISECONDS_PER_SECOND = 1000;
89 return inMilliseconds.hashCode(); 51 static const int SECONDS_PER_MINUTE = 60;
90 } 52 static const int MINUTES_PER_HOUR = 60;
53 static const int HOURS_PER_DAY = 24;
91 54
92 int compareTo(Duration other) { 55 static const int MILLISECONDS_PER_MINUTE =
93 return inMilliseconds.compareTo(other.inMilliseconds); 56 MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE;
94 } 57 static const int MILLISECONDS_PER_HOUR =
58 MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR;
59 static const int MILLISECONDS_PER_DAY =
60 MILLISECONDS_PER_HOUR * HOURS_PER_DAY;
95 61
96 String toString() { 62 static const int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR;
97 String threeDigits(int n) { 63 static const int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY;
98 if (n >= 100) return "$n";
99 if (n > 10) return "0$n";
100 return "00$n";
101 }
102 String twoDigits(int n) {
103 if (n >= 10) return "$n";
104 return "0$n";
105 }
106 64
107 if (inMilliseconds < 0) { 65 static const int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY;
108 Duration duration =
109 new Duration(milliseconds: -inMilliseconds);
110 return "-$duration";
111 }
112 String twoDigitMinutes =
113 twoDigits(inMinutes.remainder(Duration.MINUTES_PER_HOUR));
114 String twoDigitSeconds =
115 twoDigits(inSeconds.remainder(Duration.SECONDS_PER_MINUTE));
116 String threeDigitMs =
117 threeDigits(inMilliseconds.remainder(Duration.MILLISECONDS_PER_SECOND));
118 return "$inHours:$twoDigitMinutes:$twoDigitSeconds.$threeDigitMs";
119 }
120 } 66 }
OLDNEW
« no previous file with comments | « lib/core/date.dart ('k') | lib/core/exceptions.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698