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

Side by Side Diff: runtime/bin/vmservice/observatory/lib/src/app/utils.dart

Issue 810623005: Build Observatory with runtime (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of app;
6
7 class Utils {
8 static String formatPercent(num a, num total) {
9 var percent = 100.0 * (a / total);
10 return '${percent.toStringAsFixed(2)}%';
11 }
12
13 static String zeroPad(int value, int pad) {
14 String prefix = "";
15 while (pad > 1) {
16 int pow10 = pow(10, pad - 1);
17 if (value < pow10) {
18 prefix = prefix + "0";
19 }
20 pad--;
21 }
22 return "${prefix}${value}";
23 }
24
25 static String formatCommaSeparated(int v) {
26 const COMMA_EVERY = 1000;
27 if (v < COMMA_EVERY) {
28 return v.toString();
29 }
30 var mod = v % COMMA_EVERY;
31 v ~/= COMMA_EVERY;
32 var r = '${zeroPad(mod, 3)}';
33 while (v > COMMA_EVERY) {
34 mod = v % COMMA_EVERY;
35 r = '${zeroPad(mod, 3)},$r';
36 v ~/= COMMA_EVERY;
37 }
38 if (v != 0) {
39 r = '$v,$r';
40 }
41 return r;
42 }
43
44 static String formatTimePrecise(double time) {
45 if (time == null) {
46 return "-";
47 }
48 const millisPerHour = 60 * 60 * 1000;
49 const millisPerMinute = 60 * 1000;
50 const millisPerSecond = 1000;
51
52 var millis = (time * millisPerSecond).round();
53
54 var hours = millis ~/ millisPerHour;
55 millis = millis % millisPerHour;
56
57 var minutes = millis ~/ millisPerMinute;
58 millis = millis % millisPerMinute;
59
60 var seconds = millis ~/ millisPerSecond;
61 millis = millis % millisPerSecond;
62
63 if (hours > 0) {
64 return ("${zeroPad(hours,2)}"
65 ":${zeroPad(minutes,2)}"
66 ":${zeroPad(seconds,2)}"
67 ".${zeroPad(millis,3)}");
68 } else {
69 return ("${zeroPad(minutes,2)}"
70 ":${zeroPad(seconds,2)}"
71 ".${zeroPad(millis,3)}");
72 }
73 }
74
75 static String formatSize(int bytes) {
76 const int digits = 1;
77 const int bytesPerKB = 1024;
78 const int bytesPerMB = 1024 * bytesPerKB;
79 const int bytesPerGB = 1024 * bytesPerMB;
80 const int bytesPerTB = 1024 * bytesPerGB;
81
82 if (bytes < bytesPerKB) {
83 return "${bytes}B";
84 } else if (bytes < bytesPerMB) {
85 return "${(bytes / bytesPerKB).toStringAsFixed(digits)}KB";
86 } else if (bytes < bytesPerGB) {
87 return "${(bytes / bytesPerMB).toStringAsFixed(digits)}MB";
88 } else if (bytes < bytesPerTB) {
89 return "${(bytes / bytesPerGB).toStringAsFixed(digits)}GB";
90 } else {
91 return "${(bytes / bytesPerTB).toStringAsFixed(digits)}TB";
92 }
93 }
94
95 static String formatTime(double time) {
96 if (time == null) {
97 return "-";
98 }
99 const millisPerHour = 60 * 60 * 1000;
100 const millisPerMinute = 60 * 1000;
101 const millisPerSecond = 1000;
102
103 var millis = (time * millisPerSecond).round();
104
105 var hours = millis ~/ millisPerHour;
106 millis = millis % millisPerHour;
107
108 var minutes = millis ~/ millisPerMinute;
109 millis = millis % millisPerMinute;
110
111 var seconds = millis ~/ millisPerSecond;
112
113 StringBuffer out = new StringBuffer();
114 if (hours != 0) {
115 return '${hours}h ${minutes}m ${seconds}s';
116 }
117 if (minutes != 0) {
118 return '${minutes}m ${seconds}s';
119 }
120 return '${seconds}s';
121 }
122
123 static String formatSeconds(double x) {
124 return x.toStringAsFixed(2);
125 }
126
127 static bool runningInJavaScript() => identical(1.0, 1);
128 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698