tools.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import 'dart:convert';
  2. // DateFormat('dd.MM.yy HH:mm:ss').format(dateCreated); // ??
  3. String formatTimeStamp({int? timeStamp, DateTime? date, format='yyyy-MM-dd HH:mm:ss'}) {
  4. try {
  5. var time = (date != null) ? date
  6. : ((timeStamp != null) ? DateTime.fromMillisecondsSinceEpoch(timeStamp) : DateTime.now());
  7. var dateObj = {
  8. 'M+': time.month, //月份
  9. 'd+': time.day, //日
  10. 'H+': time.hour, //小时
  11. 'm+': time.minute, //分
  12. 's+': time.second, //秒
  13. };
  14. RegExp yearReg = RegExp(r"(y+)");
  15. if (yearReg.hasMatch(format)) {
  16. var matches = yearReg.allMatches(format);
  17. String match = "${matches.elementAt(0).group(1)}";
  18. format = format.replaceAll(match, "${time.year}".substring(4 - match.length));
  19. }
  20. dateObj.forEach((key, value) {
  21. RegExp replaceReg = RegExp(r'(' + key + ')');
  22. if (replaceReg.hasMatch(format)) {
  23. var matches = replaceReg.allMatches(format);
  24. String match = "${matches.elementAt(0).group(1)}";
  25. format = format.replaceAll(match, (match.length == 1) ? "$value" : "00$value".substring("$value".length));
  26. }
  27. });
  28. return format;
  29. } catch (error) {
  30. return '';
  31. }
  32. }
  33. void printJsonStr(String input) {
  34. const JsonDecoder decoder = JsonDecoder();
  35. const JsonEncoder encoder = JsonEncoder.withIndent(' ');
  36. final dynamic object = decoder.convert(input);
  37. final dynamic prettyString = encoder.convert(object);
  38. prettyString.split('\n').forEach((dynamic element) => print(element));
  39. }
  40. void printJson(Map input) {
  41. const JsonEncoder encoder = JsonEncoder.withIndent(' ');
  42. final dynamic prettyString = encoder.convert(input);
  43. prettyString.split('\n').forEach((dynamic element) => print(element));
  44. }
  45. // move to fileUtils.dart
  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. }