| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- import 'dart:convert';
- import 'dart:math';
- // DateFormat('dd.MM.yy HH:mm:ss').format(dateCreated); // ??
- String formatTimeStamp({int? timeStamp, DateTime? date, format='yyyy-MM-dd HH:mm:ss'}) {
- try {
- var time = (date != null) ? date
- : ((timeStamp != null) ? DateTime.fromMillisecondsSinceEpoch(timeStamp) : DateTime.now());
- var dateObj = {
- 'M+': time.month, //月份
- 'd+': time.day, //日
- 'H+': time.hour, //小时
- 'm+': time.minute, //分
- 's+': time.second, //秒
- };
- RegExp yearReg = RegExp(r"(y+)");
- if (yearReg.hasMatch(format)) {
- var matches = yearReg.allMatches(format);
- String match = "${matches.elementAt(0).group(1)}";
- format = format.replaceAll(match, "${time.year}".substring(4 - match.length));
- }
- dateObj.forEach((key, value) {
- RegExp replaceReg = RegExp(r'(' + key + ')');
- if (replaceReg.hasMatch(format)) {
- var matches = replaceReg.allMatches(format);
- String match = "${matches.elementAt(0).group(1)}";
- format = format.replaceAll(match, (match.length == 1) ? "$value" : "00$value".substring("$value".length));
- }
- });
- return format;
- } catch (error) {
- return '';
- }
- }
- void printJsonStr(String input) {
- const JsonDecoder decoder = JsonDecoder();
- const JsonEncoder encoder = JsonEncoder.withIndent(' ');
- final dynamic object = decoder.convert(input);
- final dynamic prettyString = encoder.convert(object);
- prettyString.split('\n').forEach((dynamic element) => print(element));
- }
- void printJson(Map input) {
- const JsonEncoder encoder = JsonEncoder.withIndent(' ');
- final dynamic prettyString = encoder.convert(input);
- prettyString.split('\n').forEach((dynamic element) => print(element));
- }
- // move to fileUtils.dart
- // String formatFileSize(int size) {
- // final fileSizeInKB = size / 1000;
- // if (fileSizeInKB < 1) {
- // return '$size bytes';
- // }
- // final fileSizeInMB = fileSizeInKB / 1000;
- // if (fileSizeInMB < 1) {
- // return '${fileSizeInKB.toStringAsFixed(2)} KB';
- // }
- // final fileSizeInGB = fileSizeInMB / 1000;
- // if (fileSizeInGB < 1) {
- // return '${fileSizeInMB.toStringAsFixed(2)} MB';
- // }
- // final fileSizeInTB = fileSizeInGB / 1000;
- // if (fileSizeInTB < 1) {
- // return '${fileSizeInGB.toStringAsFixed(2)} GB';
- // }
- // return '${fileSizeInTB.toStringAsFixed(2)} TB';
- // }
- // 均匀分组
- List<int> uniformGrouping(int totalSize, int defaultGroupSize) {
- if (totalSize < 0) {
- return [];
- } else if (totalSize == 0) {
- return [0];
- }
- final int theUpperLimit = (defaultGroupSize * 1.5).toInt(); // 折行上限
- if (totalSize <= theUpperLimit) {
- return [totalSize];
- } else if ((totalSize > theUpperLimit) && (totalSize <= (defaultGroupSize * 2))) {
- int half = totalSize ~/ 2;
- return [half, totalSize - half];
- }
- final int groupNumber = totalSize ~/ defaultGroupSize;
- final int remainder = totalSize % defaultGroupSize;
- List<int> group = [];
- if (remainder == 0) {
- for (int i = 0; i < groupNumber; i++) {
- group.add(defaultGroupSize);
- }
- } else {
- final splitRemainder = remainder ~/ groupNumber;
- final groupRemainder = remainder % groupNumber;
- for (int i = 0; i < groupRemainder; i++) {
- group.add(defaultGroupSize + splitRemainder + 1);
- }
- for (int i = groupRemainder; i < groupNumber; i++) {
- group.add((defaultGroupSize + splitRemainder));
- }
- }
- return group;
- }
- String ellipsizeStrMiddle(String str, {
- int headerLen = 4,
- int tailLen = 4,
- String splitter = '...',
- }) {
- if (str.length > 2) {
- int liveHeaderLen = headerLen;
- int liveTailLen = tailLen;
- int loopN = min(liveHeaderLen, liveTailLen);
- for (int i = 0; i < loopN; i++) {
- if (str.length > (liveHeaderLen + liveTailLen)) {
- String prefix = str.substring(0, liveHeaderLen);
- String suffix = str.substring(str.length - liveTailLen);
- return '$prefix$splitter$suffix';
- }
- liveHeaderLen--;
- liveTailLen--;
- }
- String prefix = str.substring(0, liveHeaderLen);
- String suffix = str.substring(str.length - liveTailLen);
- return '$prefix$splitter$suffix';
- } else {
- return str;
- }
- }
|