toDurationHumanReadAble method

String toDurationHumanReadAble()

text to duration example:

days - hours - minutes - seconds - milliseconds - microseconds

0 - 0 - 0 - 0 - 0 - 0

"0-0-0-0-0-0".toDuration();

Implementation

String toDurationHumanReadAble() {
  DurationDataScheme durationData = DurationDataScheme({
    "@type": "durationDataScheme",
  });
  RegExpMatch? regExpMatch = RegExp(
          r"(([0-9]+)[ \-\~+]+([0-9]+)[ \-\~+]+([0-9]+)[ \-\~+]+([0-9]+)[ \-\~+]+([0-9]+)[ \-\~+]+([0-9]+))",
          caseSensitive: false)
      .firstMatch(this);
  if (regExpMatch != null) {
    List<String?> res = regExpMatch.groups([2, 3, 4, 5, 6, 7]);
    List<String> keys = [
      "days",
      "hours",
      "minutes",
      "seconds",
      "milliseconds",
      "microseconds",
    ];
    for (var i = 0; i < res.length; i++) {
      String? datas = res[i];
      if (datas == null) {
        continue;
      }
      try {
        durationData[keys[i]] = int.parse(datas);
      } catch (e) {}
    }
  }
  String message = "";
  if ((durationData.days ?? 0) > 0) {
    return "${durationData.days} Hari";
  }
  if ((durationData.hours ?? 0) > 0) {
    return "${durationData.hours} Jam";
  }
  if ((durationData.minutes ?? 0) > 0) {
    return "${durationData.minutes} Menit";
  }
  if (message.isEmpty) {
    message += "${durationData.seconds} Detik";
    if ((durationData.milliseconds ?? 0) > 1) {
      message += " ${durationData.milliseconds} Milli Detik";
    }

    if ((durationData.microseconds ?? 0) > 1) {
      message += " ${durationData.microseconds} Micro Detik";
    }
  }

  return message;
}