toRealtime static method

String toRealtime(
  1. int? ms, {
  2. bool showRealtime = true,
  3. int whenShowNow = 10,
  4. String? format,
  5. String? local,
  6. TimeFormats timeFormat = TimeFormats.timeHMa,
  7. DateFormats dateFormat = DateFormats.dateDMCY,
  8. String separator = " at ",
  9. RealtimeTextFormat textFormat = const RealtimeTextFormat(),
  10. String onRealtime(
    1. Realtime value
    )?,
  11. String onRealtimeByHours(
    1. Realtime value
    )?,
  12. String onRealtimeByMinutes(
    1. Realtime value
    )?,
  13. String onRealtimeBySeconds(
    1. Realtime value
    )?,
  14. String onRealtimeByAfterHours(
    1. Realtime value
    )?,
  15. String onRealtimeByAfterMinutes(
    1. Realtime value
    )?,
  16. String onRealtimeByAfterSeconds(
    1. Realtime value
    )?,
  17. String onRealtimeByTomorrow(
    1. Realtime value,
    2. String time
    )?,
  18. String onRealtimeByToday(
    1. Realtime value,
    2. String time
    )?,
  19. String onRealtimeByYesterday(
    1. Realtime value,
    2. String time
    )?,
})

Converts milliseconds since epoch to a customizable relative time string.

Example:

int myTime = DateProvider.currentMS;
String customRelativeTime = DateProvider.toRealtime(
  myTime,
  onRealtimeByHours: (value) => "Custom hours: ${value.hours}",
  onRealtimeByMinutes: (value) => "Custom minutes: ${value.minutes}",
);
print(customRelativeTime);  // Output: Custom hours: 2

Implementation

static String toRealtime(
  int? ms, {
  bool showRealtime = true,
  int whenShowNow = 10,
  String? format,
  String? local,
  TimeFormats timeFormat = TimeFormats.timeHMa,
  DateFormats dateFormat = DateFormats.dateDMCY,
  String separator = " at ",
  RealtimeTextFormat textFormat = const RealtimeTextFormat(),
  String Function(Realtime value)? onRealtime,
  String Function(Realtime value)? onRealtimeByHours,
  String Function(Realtime value)? onRealtimeByMinutes,
  String Function(Realtime value)? onRealtimeBySeconds,
  String Function(Realtime value)? onRealtimeByAfterHours,
  String Function(Realtime value)? onRealtimeByAfterMinutes,
  String Function(Realtime value)? onRealtimeByAfterSeconds,
  String Function(Realtime value, String time)? onRealtimeByTomorrow,
  String Function(Realtime value, String time)? onRealtimeByToday,
  String Function(Realtime value, String time)? onRealtimeByYesterday,
}) {
  var time = DateTime.fromMillisecondsSinceEpoch(ms ?? 0);
  var realtime = Realtime.fromDateTime(time);

  /// for yesterday time
  if (time.isYesterday) {
    var yesterdayTime = time.toDate(
      timeFormat: timeFormat,
      dateFormat: DateFormats.none,
      local: local,
    );
    if (onRealtimeByYesterday != null) {
      return onRealtimeByYesterday(realtime, yesterdayTime);
    } else {
      return "${textFormat.yesterday}$separator$yesterdayTime";
    }
  }

  /// for today time
  else if (time.isToday) {
    ///
    /// for realtime
    if (showRealtime) {
      ///
      /// for hours
      if (realtime.isHourMode) {
        if (onRealtimeByHours != null) {
          return onRealtimeByHours(realtime);
        } else {
          int _ = realtime.hours;
          return "$_ ${textFormat.hour.apply(_)} ${textFormat.ago}";
        }
      }

      /// for minutes
      else if (realtime.isMinuteMode) {
        if (onRealtimeByMinutes != null) {
          return onRealtimeByMinutes(realtime);
        } else {
          int _ = realtime.minutes;
          return "$_ ${textFormat.minute.apply(_)} ${textFormat.ago}";
        }
      }

      /// for after hours
      else if (realtime.isAfterHourMode) {
        if (onRealtimeByAfterHours != null) {
          return onRealtimeByAfterHours(realtime);
        } else {
          int _ = realtime.afterHours;
          return "${textFormat.after} $_ ${textFormat.hour.apply(_)}";
        }
      }

      /// for after minutes
      else if (realtime.isAfterMinuteMode) {
        if (onRealtimeByAfterMinutes != null) {
          return onRealtimeByAfterMinutes(realtime);
        } else {
          int _ = realtime.afterMinutes;
          return "${textFormat.after} $_ ${textFormat.minute.apply(_)}";
        }
      }

      /// for after seconds
      else if (realtime.isAfterSecondMode) {
        if (onRealtimeByAfterSeconds != null) {
          return onRealtimeByAfterSeconds(realtime);
        } else {
          int _ = realtime.afterSeconds;
          return "${textFormat.after} $_ ${textFormat.second.apply(_)}";
        }
      }

      /// for seconds
      else {
        if (onRealtimeBySeconds != null) {
          return onRealtimeBySeconds(realtime);
        } else {
          int _ = realtime.seconds;
          if (whenShowNow < _) {
            return "$_ ${textFormat.second.apply(_)} ${textFormat.ago}";
          } else {
            return textFormat.now;
          }
        }
      }
    }

    /// for today default time
    else {
      var todayTime = time.toDate(
        timeFormat: timeFormat,
        dateFormat: DateFormats.none,
        local: local,
      );
      if (onRealtimeByToday != null) {
        return onRealtimeByToday(realtime, todayTime);
      } else {
        return "${textFormat.today}$separator$todayTime";
      }
    }
  }

  /// for tomorrow time
  else if (time.isTomorrow) {
    var tomorrowTime = time.toDate(
      timeFormat: timeFormat,
      dateFormat: DateFormats.none,
      local: local,
    );
    if (onRealtimeByTomorrow != null) {
      return onRealtimeByTomorrow(realtime, tomorrowTime);
    } else {
      return "${textFormat.tomorrow}$separator$tomorrowTime";
    }
  }

  /// for default time
  else {
    return time.toDate(
      timeFormat: timeFormat,
      dateFormat: dateFormat,
      format: format,
      separator: separator,
      local: local,
    );
  }
}