tools.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'dart:convert';
  2. export 'hexUtils.dart';
  3. // DateFormat('dd.MM.yy HH:mm:ss').format(dateCreated); // ??
  4. String formatTimeStamp({int? timeStamp, DateTime? date, format='yyyy-MM-dd HH:mm:ss'}) {
  5. try {
  6. var time = (date != null) ? date
  7. : ((timeStamp != null) ? DateTime.fromMillisecondsSinceEpoch(timeStamp) : DateTime.now());
  8. var dateObj = {
  9. 'M+': time.month, //月份
  10. 'd+': time.day, //日
  11. 'H+': time.hour, //小时
  12. 'm+': time.minute, //分
  13. 's+': time.second, //秒
  14. };
  15. RegExp yearReg = RegExp(r"(y+)");
  16. if (yearReg.hasMatch(format)) {
  17. var matches = yearReg.allMatches(format);
  18. String match = "${matches.elementAt(0).group(1)}";
  19. format = format.replaceAll(match, "${time.year}".substring(4 - match.length));
  20. }
  21. dateObj.forEach((key, value) {
  22. RegExp replaceReg = RegExp(r'(' + key + ')');
  23. if (replaceReg.hasMatch(format)) {
  24. var matches = replaceReg.allMatches(format);
  25. String match = "${matches.elementAt(0).group(1)}";
  26. format = format.replaceAll(match, (match.length == 1) ? "$value" : "00$value".substring("$value".length));
  27. }
  28. });
  29. return format;
  30. } catch (error) {
  31. return '';
  32. }
  33. }
  34. void printJsonStr(String input) {
  35. const JsonDecoder decoder = JsonDecoder();
  36. const JsonEncoder encoder = JsonEncoder.withIndent(' ');
  37. final dynamic object = decoder.convert(input);
  38. final dynamic prettyString = encoder.convert(object);
  39. prettyString.split('\n').forEach((dynamic element) => print(element));
  40. }
  41. void printJson(Map input) {
  42. const JsonEncoder encoder = JsonEncoder.withIndent(' ');
  43. final dynamic prettyString = encoder.convert(input);
  44. prettyString.split('\n').forEach((dynamic element) => print(element));
  45. }
  46. String formatFileSize(int size) {
  47. final fileSizeInKB = size / 1000;
  48. if (fileSizeInKB < 1) {
  49. return '$size bytes';
  50. }
  51. final fileSizeInMB = fileSizeInKB / 1000;
  52. if (fileSizeInMB < 1) {
  53. return '${fileSizeInKB.toStringAsFixed(2)} KB';
  54. }
  55. final fileSizeInGB = fileSizeInMB / 1000;
  56. if (fileSizeInGB < 1) {
  57. return '${fileSizeInMB.toStringAsFixed(2)} MB';
  58. }
  59. final fileSizeInTB = fileSizeInGB / 1000;
  60. if (fileSizeInTB < 1) {
  61. return '${fileSizeInGB.toStringAsFixed(2)} GB';
  62. }
  63. return '${fileSizeInTB.toStringAsFixed(2)} TB';
  64. }
  65. // 均匀分组
  66. List<int> uniformGrouping(int totalSize, int defaultGroupSize) {
  67. if (totalSize < 0) {
  68. return [];
  69. } else if (totalSize == 0) {
  70. return [0];
  71. }
  72. final int theUpperLimit = (defaultGroupSize * 1.5).toInt(); // 折行上限
  73. if (totalSize <= theUpperLimit) {
  74. return [totalSize];
  75. } else if ((totalSize > theUpperLimit) && (totalSize <= (defaultGroupSize * 2))) {
  76. int half = totalSize ~/ 2;
  77. return [half, totalSize - half];
  78. }
  79. final int groupNumber = totalSize ~/ defaultGroupSize;
  80. final int remainder = totalSize % defaultGroupSize;
  81. List<int> group = [];
  82. if (remainder == 0) {
  83. for (int i = 0; i < groupNumber; i++) {
  84. group.add(defaultGroupSize);
  85. }
  86. } else {
  87. final splitRemainder = remainder ~/ groupNumber;
  88. final groupRemainder = remainder % groupNumber;
  89. for (int i = 0; i < groupRemainder; i++) {
  90. group.add(defaultGroupSize + splitRemainder + 1);
  91. }
  92. for (int i = groupRemainder; i < groupNumber; i++) {
  93. group.add((defaultGroupSize + splitRemainder));
  94. }
  95. }
  96. return group;
  97. }