Chromium Code Reviews| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:math'; | 8 import 'dart:math'; |
| 9 | 9 |
| 10 enum DurationComponent { | |
| 11 Days, | |
| 12 Hours, | |
| 13 Minutes, | |
| 14 Seconds, | |
| 15 Milliseconds, | |
| 16 Microseconds | |
| 17 } | |
| 18 | |
| 10 class Utils { | 19 class Utils { |
| 11 static String formatPercentNormalized(double x) { | 20 static String formatPercentNormalized(double x) { |
| 12 var percent = 100.0 * x; | 21 var percent = 100.0 * x; |
| 13 return '${percent.toStringAsFixed(2)}%'; | 22 return '${percent.toStringAsFixed(2)}%'; |
| 14 } | 23 } |
| 15 | 24 |
| 16 static String formatPercent(num a, num total) { | 25 static String formatPercent(num a, num total) { |
| 17 return formatPercentNormalized(a / total); | 26 return formatPercentNormalized(a / total); |
| 18 } | 27 } |
| 19 | 28 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 return '${seconds}s'; | 143 return '${seconds}s'; |
| 135 } | 144 } |
| 136 | 145 |
| 137 static String formatDateTime(DateTime now) { | 146 static String formatDateTime(DateTime now) { |
| 138 return '${now.year}-${now.month}-${now.day} ' | 147 return '${now.year}-${now.month}-${now.day} ' |
| 139 '${now.hour.toString().padLeft(2)}:' | 148 '${now.hour.toString().padLeft(2)}:' |
| 140 '${now.minute.toString().padLeft(2)}:' | 149 '${now.minute.toString().padLeft(2)}:' |
| 141 '${now.second.toString().padLeft(2)}'; | 150 '${now.second.toString().padLeft(2)}'; |
| 142 } | 151 } |
| 143 | 152 |
| 153 static String formatDuration(Duration duration, | |
| 154 {DurationComponent precision = DurationComponent.Microseconds, | |
| 155 String future = '', | |
| 156 String past = 'ago'}) { | |
| 157 var value = duration.isNegative | |
| 158 ? -duration.inMicroseconds | |
| 159 : duration.inMicroseconds; | |
|
siva
2017/08/03 18:14:51
Would it be the same as
var value = duration.inMic
cbernaschina
2017/08/03 22:32:53
Done.
| |
| 160 switch (precision) { | |
| 161 case DurationComponent.Days: | |
| 162 value = (value / Duration.MICROSECONDS_PER_DAY).round(); | |
| 163 break; | |
| 164 case DurationComponent.Hours: | |
| 165 value = (value / Duration.MICROSECONDS_PER_HOUR).round(); | |
| 166 break; | |
| 167 case DurationComponent.Minutes: | |
| 168 value = (value / Duration.MICROSECONDS_PER_MINUTE).round(); | |
| 169 break; | |
| 170 case DurationComponent.Seconds: | |
| 171 value = (value / Duration.MICROSECONDS_PER_SECOND).round(); | |
| 172 break; | |
| 173 case DurationComponent.Milliseconds: | |
| 174 value = (value / Duration.MICROSECONDS_PER_MILLISECOND).round(); | |
| 175 break; | |
| 176 case DurationComponent.Microseconds: | |
| 177 break; | |
| 178 } | |
| 179 final components = <String>[]; | |
| 180 if (duration.isNegative) { | |
| 181 if (!past.isEmpty) { | |
| 182 components.add(past); | |
| 183 } | |
| 184 } else { | |
| 185 if (!future.isEmpty) { | |
| 186 components.add(future); | |
| 187 } | |
| 188 } | |
| 189 switch (precision) { | |
| 190 case DurationComponent.Microseconds: | |
| 191 components.add('${value % Duration.MICROSECONDS_PER_MILLISECOND}μs'); | |
| 192 value = (value / Duration.MICROSECONDS_PER_MILLISECOND).floor(); | |
| 193 if (value != 0) { | |
| 194 continue Milliseconds; | |
| 195 } | |
| 196 break; | |
| 197 Milliseconds: | |
| 198 case DurationComponent.Milliseconds: | |
| 199 components.add('${value % Duration.MILLISECONDS_PER_SECOND}ms'); | |
| 200 value = (value / Duration.MILLISECONDS_PER_SECOND).floor(); | |
| 201 if (value != 0) { | |
| 202 continue Seconds; | |
| 203 } | |
| 204 break; | |
| 205 Seconds: | |
| 206 case DurationComponent.Seconds: | |
| 207 components.add('${value % Duration.SECONDS_PER_MINUTE}s'); | |
| 208 value = (value / Duration.SECONDS_PER_MINUTE).floor(); | |
| 209 ; | |
| 210 if (value != 0) { | |
| 211 continue Minutes; | |
| 212 } | |
| 213 break; | |
| 214 Minutes: | |
| 215 case DurationComponent.Minutes: | |
| 216 components.add('${value % Duration.MINUTES_PER_HOUR}m'); | |
| 217 value = (value / Duration.MINUTES_PER_HOUR).floor(); | |
| 218 if (value != 0) { | |
| 219 continue Hours; | |
| 220 } | |
| 221 break; | |
| 222 Hours: | |
| 223 case DurationComponent.Hours: | |
| 224 components.add('${value % Duration.HOURS_PER_DAY}h'); | |
| 225 value = (value / Duration.HOURS_PER_DAY).floor(); | |
| 226 if (value != 0) { | |
| 227 continue Days; | |
| 228 } | |
| 229 break; | |
| 230 Days: | |
| 231 case DurationComponent.Days: | |
| 232 components.add('${value}d'); | |
| 233 } | |
| 234 return components.reversed.join(' '); | |
| 235 } | |
| 236 | |
| 144 static String formatSeconds(double x) { | 237 static String formatSeconds(double x) { |
| 145 return x.toStringAsFixed(2); | 238 return x.toStringAsFixed(2); |
| 146 } | 239 } |
| 147 | 240 |
| 148 static String formatMillis(double x) { | 241 static String formatMillis(double x) { |
| 149 return x.toStringAsFixed(2); | 242 return x.toStringAsFixed(2); |
| 150 } | 243 } |
| 151 | 244 |
| 152 static String formatDurationInSeconds(Duration x) => | 245 static String formatDurationInSeconds(Duration x) => |
| 153 formatSeconds(x.inMicroseconds / Duration.MICROSECONDS_PER_SECOND); | 246 formatSeconds(x.inMicroseconds / Duration.MICROSECONDS_PER_SECOND); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 if (_timer != null) { | 300 if (_timer != null) { |
| 208 // Already scheduled. | 301 // Already scheduled. |
| 209 return; | 302 return; |
| 210 } | 303 } |
| 211 _timer = new Timer(Duration.ZERO, () { | 304 _timer = new Timer(Duration.ZERO, () { |
| 212 _timer = null; | 305 _timer = null; |
| 213 callback(); | 306 callback(); |
| 214 }); | 307 }); |
| 215 } | 308 } |
| 216 } | 309 } |
| OLD | NEW |