2024-02-22 21:45:00 +00:00
|
|
|
String _s(num n) => n == 1 ? '' : 's';
|
|
|
|
|
2024-01-01 22:07:52 +00:00
|
|
|
extension TimeFormatter on DateTime {
|
|
|
|
String readableDuration() {
|
|
|
|
final now = DateTime.now();
|
|
|
|
final duration = now.difference(this);
|
|
|
|
|
|
|
|
if (duration.inSeconds < 60) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '${duration.inSeconds} Second${_s(duration.inSeconds)} ago';
|
2024-01-01 22:07:52 +00:00
|
|
|
} else if (duration.inMinutes < 60) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '${duration.inMinutes} Minute${_s(duration.inMinutes)})} ago';
|
2024-01-01 22:07:52 +00:00
|
|
|
} else if (duration.inHours < 24) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '${duration.inHours} Hour${_s(duration.inHours)} ago';
|
2024-01-01 22:07:52 +00:00
|
|
|
} else if (duration.inDays < 30) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '${duration.inDays} Day${_s(duration.inDays)} ago';
|
2024-01-01 22:07:52 +00:00
|
|
|
} else if ((duration.inDays / 30) < 12) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '${duration.inDays ~/ 30} Month${_s(duration.inDays ~/ 30)} ago';
|
2024-01-01 22:07:52 +00:00
|
|
|
} else {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '${duration.inDays ~/ 365} Year${_s(duration.inDays ~/ 365)} ago';
|
2024-01-01 22:07:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-17 18:50:15 +00:00
|
|
|
|
|
|
|
extension DurationFormatter on Duration {
|
|
|
|
String readableDuration() {
|
|
|
|
if (inSeconds < 60) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '$inSeconds Second${_s(inSeconds)}';
|
2024-02-17 18:50:15 +00:00
|
|
|
} else if (inMinutes < 60) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '$inMinutes Minute${_s(inMinutes)}';
|
2024-02-17 18:50:15 +00:00
|
|
|
} else if (inHours < 24) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '$inHours Hour${_s(inHours)}';
|
2024-02-17 18:50:15 +00:00
|
|
|
} else if (inDays < 30) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '$inDays Day${_s(inDays)}';
|
2024-02-17 18:50:15 +00:00
|
|
|
} else if ((inDays / 30) < 12) {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '${inDays ~/ 30} Month${_s(inDays ~/ 30)}';
|
2024-02-17 18:50:15 +00:00
|
|
|
} else {
|
2024-02-22 21:45:00 +00:00
|
|
|
return '${inDays ~/ 365} Year${_s(inDays ~/ 365)}';
|
2024-02-17 18:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|