add correct s ending to dates and times

This commit is contained in:
lukas-heiligenbrunner 2024-02-22 22:45:00 +01:00
parent f319facda1
commit 3108b9a658

View File

@ -1,20 +1,22 @@
String _s(num n) => n == 1 ? '' : 's';
extension TimeFormatter on DateTime { extension TimeFormatter on DateTime {
String readableDuration() { String readableDuration() {
final now = DateTime.now(); final now = DateTime.now();
final duration = now.difference(this); final duration = now.difference(this);
if (duration.inSeconds < 60) { if (duration.inSeconds < 60) {
return '${duration.inSeconds} seconds ago'; return '${duration.inSeconds} Second${_s(duration.inSeconds)} ago';
} else if (duration.inMinutes < 60) { } else if (duration.inMinutes < 60) {
return '${duration.inMinutes} minutes ago'; return '${duration.inMinutes} Minute${_s(duration.inMinutes)})} ago';
} else if (duration.inHours < 24) { } else if (duration.inHours < 24) {
return '${duration.inHours} hours ago'; return '${duration.inHours} Hour${_s(duration.inHours)} ago';
} else if (duration.inDays < 30) { } else if (duration.inDays < 30) {
return '${duration.inDays} days ago'; return '${duration.inDays} Day${_s(duration.inDays)} ago';
} else if ((duration.inDays / 30) < 12) { } else if ((duration.inDays / 30) < 12) {
return '${duration.inDays ~/ 30} months ago'; return '${duration.inDays ~/ 30} Month${_s(duration.inDays ~/ 30)} ago';
} else { } else {
return '${duration.inDays ~/ 365} years ago'; return '${duration.inDays ~/ 365} Year${_s(duration.inDays ~/ 365)} ago';
} }
} }
} }
@ -22,17 +24,17 @@ extension TimeFormatter on DateTime {
extension DurationFormatter on Duration { extension DurationFormatter on Duration {
String readableDuration() { String readableDuration() {
if (inSeconds < 60) { if (inSeconds < 60) {
return '$inSeconds second${inSeconds != 1 ? 's' : ''}'; return '$inSeconds Second${_s(inSeconds)}';
} else if (inMinutes < 60) { } else if (inMinutes < 60) {
return '$inMinutes minute${inMinutes != 1 ? 's' : ''}'; return '$inMinutes Minute${_s(inMinutes)}';
} else if (inHours < 24) { } else if (inHours < 24) {
return '$inHours hour${inHours != 1 ? 's' : ''}'; return '$inHours Hour${_s(inHours)}';
} else if (inDays < 30) { } else if (inDays < 30) {
return '$inDays day${inDays != 1 ? 's' : ''}'; return '$inDays Day${_s(inDays)}';
} else if ((inDays / 30) < 12) { } else if ((inDays / 30) < 12) {
return '${inDays ~/ 30} month${(inDays ~/ 30) != 1 ? 's' : ''}'; return '${inDays ~/ 30} Month${_s(inDays ~/ 30)}';
} else { } else {
return '${inDays ~/ 365} year${(inDays ~/ 365) != 1 ? 's' : ''}'; return '${inDays ~/ 365} Year${_s(inDays ~/ 365)}';
} }
} }
} }