OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library utils; | 5 library utils; |
6 | 6 |
7 import 'dart:math'; | 7 import 'dart:math'; |
8 | 8 |
9 class Utils { | 9 class Utils { |
| 10 |
| 11 static String formatPercentNormalized(double x) { |
| 12 var percent = 100.0 * x; |
| 13 return '${percent.toStringAsFixed(2)}%'; |
| 14 } |
| 15 |
10 static String formatPercent(num a, num total) { | 16 static String formatPercent(num a, num total) { |
11 var percent = 100.0 * (a / total); | 17 return formatPercentNormalized(a / total); |
12 return '${percent.toStringAsFixed(2)}%'; | |
13 } | 18 } |
14 | 19 |
15 static String zeroPad(int value, int pad) { | 20 static String zeroPad(int value, int pad) { |
16 String prefix = ""; | 21 String prefix = ""; |
17 while (pad > 1) { | 22 while (pad > 1) { |
18 int pow10 = pow(10, pad - 1); | 23 int pow10 = pow(10, pad - 1); |
19 if (value < pow10) { | 24 if (value < pow10) { |
20 prefix = prefix + "0"; | 25 prefix = prefix + "0"; |
21 } | 26 } |
22 pad--; | 27 pad--; |
(...skipping 17 matching lines...) Expand all Loading... |
40 if (v != 0) { | 45 if (v != 0) { |
41 r = '$v,$r'; | 46 r = '$v,$r'; |
42 } | 47 } |
43 return r; | 48 return r; |
44 } | 49 } |
45 | 50 |
46 static String formatTimePrecise(double time) { | 51 static String formatTimePrecise(double time) { |
47 if (time == null) { | 52 if (time == null) { |
48 return "-"; | 53 return "-"; |
49 } | 54 } |
| 55 const millisPerSecond = 1000; |
| 56 |
| 57 var millis = (time * millisPerSecond).round(); |
| 58 return formatTimeMilliseconds(millis); |
| 59 } |
| 60 |
| 61 static String formatTimeMilliseconds(int millis) { |
50 const millisPerHour = 60 * 60 * 1000; | 62 const millisPerHour = 60 * 60 * 1000; |
51 const millisPerMinute = 60 * 1000; | 63 const millisPerMinute = 60 * 1000; |
52 const millisPerSecond = 1000; | 64 const millisPerSecond = 1000; |
53 | 65 |
54 var millis = (time * millisPerSecond).round(); | |
55 | |
56 var hours = millis ~/ millisPerHour; | 66 var hours = millis ~/ millisPerHour; |
57 millis = millis % millisPerHour; | 67 millis = millis % millisPerHour; |
58 | 68 |
59 var minutes = millis ~/ millisPerMinute; | 69 var minutes = millis ~/ millisPerMinute; |
60 millis = millis % millisPerMinute; | 70 millis = millis % millisPerMinute; |
61 | 71 |
62 var seconds = millis ~/ millisPerSecond; | 72 var seconds = millis ~/ millisPerSecond; |
63 millis = millis % millisPerSecond; | 73 millis = millis % millisPerSecond; |
64 | 74 |
65 if (hours > 0) { | 75 if (hours > 0) { |
66 return ("${zeroPad(hours,2)}" | 76 return ("${zeroPad(hours,2)}" |
67 ":${zeroPad(minutes,2)}" | 77 ":${zeroPad(minutes,2)}" |
| 78 ":${zeroPad(seconds,2)}" |
| 79 ".${zeroPad(millis,3)}"); |
| 80 } else if (minutes > 0) { |
| 81 return ("${zeroPad(minutes,2)}" |
68 ":${zeroPad(seconds,2)}" | 82 ":${zeroPad(seconds,2)}" |
69 ".${zeroPad(millis,3)}"); | 83 ".${zeroPad(millis,3)}"); |
70 } else { | 84 } else { |
71 return ("${zeroPad(minutes,2)}" | 85 return ("${zeroPad(seconds,2)}" |
72 ":${zeroPad(seconds,2)}" | |
73 ".${zeroPad(millis,3)}"); | 86 ".${zeroPad(millis,3)}"); |
74 } | 87 } |
75 } | 88 } |
76 | 89 |
77 static String formatSize(int bytes) { | 90 static String formatSize(int bytes) { |
78 const int digits = 1; | 91 const int digits = 1; |
79 const int bytesPerKB = 1024; | 92 const int bytesPerKB = 1024; |
80 const int bytesPerMB = 1024 * bytesPerKB; | 93 const int bytesPerMB = 1024 * bytesPerKB; |
81 const int bytesPerGB = 1024 * bytesPerMB; | 94 const int bytesPerGB = 1024 * bytesPerMB; |
82 const int bytesPerTB = 1024 * bytesPerGB; | 95 const int bytesPerTB = 1024 * bytesPerGB; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 return '${minutes}m ${seconds}s'; | 132 return '${minutes}m ${seconds}s'; |
120 } | 133 } |
121 return '${seconds}s'; | 134 return '${seconds}s'; |
122 } | 135 } |
123 | 136 |
124 static String formatSeconds(double x) { | 137 static String formatSeconds(double x) { |
125 return x.toStringAsFixed(2); | 138 return x.toStringAsFixed(2); |
126 } | 139 } |
127 | 140 |
128 static bool runningInJavaScript() => identical(1.0, 1); | 141 static bool runningInJavaScript() => identical(1.0, 1); |
129 } | 142 } |
OLD | NEW |