toRealtime static method
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 onRealtime(
- Realtime value
- String onRealtimeByHours(
- Realtime value
- String onRealtimeByMinutes(
- Realtime value
- String onRealtimeBySeconds(
- Realtime value
- String onRealtimeByAfterHours(
- Realtime value
- String onRealtimeByAfterMinutes(
- Realtime value
- String onRealtimeByAfterSeconds(
- Realtime value
- String onRealtimeByTomorrow()?,
- String onRealtimeByToday()?,
- String onRealtimeByYesterday()?,
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,
);
}
}