implemented short relative date formatting for chat and added unit tests
This commit is contained in:
@@ -66,7 +66,7 @@ class GetChatResponseObject {
|
|||||||
|
|
||||||
static GetChatResponseObject getDateDummy(int timestamp) {
|
static GetChatResponseObject getDateDummy(int timestamp) {
|
||||||
var elementDate = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
|
var elementDate = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
|
||||||
return getTextDummy(elementDate.formatDate());
|
return getTextDummy(elementDate.formatDateRelativeShort());
|
||||||
}
|
}
|
||||||
|
|
||||||
static GetChatResponseObject getTextDummy(String text) =>
|
static GetChatResponseObject getTextDummy(String text) =>
|
||||||
|
|||||||
@@ -46,4 +46,18 @@ extension DateTimeFormatting on DateTime {
|
|||||||
String formatRelative() => Jiffy.parseFromDateTime(this).fromNow();
|
String formatRelative() => Jiffy.parseFromDateTime(this).fromNow();
|
||||||
|
|
||||||
String timeRangeTo(DateTime end) => '${formatHm()} - ${end.formatHm()}';
|
String timeRangeTo(DateTime end) => '${formatHm()} - ${end.formatHm()}';
|
||||||
|
|
||||||
|
String formatDateRelativeShort({DateTime? now}) {
|
||||||
|
final reference = now ?? DateTime.now();
|
||||||
|
final today = DateTime(reference.year, reference.month, reference.day);
|
||||||
|
final self = DateTime(year, month, day);
|
||||||
|
final diff = today.difference(self).inDays;
|
||||||
|
|
||||||
|
if (diff == 0) return 'Heute';
|
||||||
|
if (diff == 1) return 'Gestern';
|
||||||
|
if (diff > 1 && diff <= 6) {
|
||||||
|
return Jiffy.parseFromDateTime(this).format(pattern: 'EEEE');
|
||||||
|
}
|
||||||
|
return formatDate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,4 +58,53 @@ void main() {
|
|||||||
expect(dt.timeRangeTo(end), '09:07 - 09:52');
|
expect(dt.timeRangeTo(end), '09:07 - 09:52');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
group('formatDateRelativeShort', () {
|
||||||
|
final now = DateTime(2026, 5, 9, 14, 0); // Saturday
|
||||||
|
|
||||||
|
test('today returns "Heute"', () {
|
||||||
|
expect(
|
||||||
|
DateTime(2026, 5, 9, 8, 0).formatDateRelativeShort(now: now),
|
||||||
|
'Heute',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('yesterday returns "Gestern"', () {
|
||||||
|
expect(
|
||||||
|
DateTime(2026, 5, 8, 23, 30).formatDateRelativeShort(now: now),
|
||||||
|
'Gestern',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('2 to 6 days ago returns the German weekday name', () {
|
||||||
|
// 2026-05-07 is a Thursday
|
||||||
|
expect(
|
||||||
|
DateTime(2026, 5, 7).formatDateRelativeShort(now: now),
|
||||||
|
'Donnerstag',
|
||||||
|
);
|
||||||
|
// 2026-05-03 is a Sunday (6 days before Saturday 9th)
|
||||||
|
expect(
|
||||||
|
DateTime(2026, 5, 3).formatDateRelativeShort(now: now),
|
||||||
|
'Sonntag',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('7 days or more ago falls back to dd.MM.yyyy', () {
|
||||||
|
expect(
|
||||||
|
DateTime(2026, 5, 2).formatDateRelativeShort(now: now),
|
||||||
|
'02.05.2026',
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
DateTime(2026, 1, 1).formatDateRelativeShort(now: now),
|
||||||
|
'01.01.2026',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('future dates fall back to dd.MM.yyyy', () {
|
||||||
|
expect(
|
||||||
|
DateTime(2026, 5, 10).formatDateRelativeShort(now: now),
|
||||||
|
'10.05.2026',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user