2020-01-09 11:16:34 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-10-26 16:50:34 +00:00
|
|
|
|
2020-10-03 11:11:07 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2023-05-16 16:45:39 +00:00
|
|
|
import 'package:intl/intl.dart';
|
2020-01-09 11:16:34 +00:00
|
|
|
|
|
|
|
/// Provides extra functionality for formatting the time.
|
|
|
|
extension DateTimeExtension on DateTime {
|
2020-05-13 13:58:59 +00:00
|
|
|
bool operator <(DateTime other) {
|
|
|
|
return millisecondsSinceEpoch < other.millisecondsSinceEpoch;
|
2020-01-09 11:16:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:58:59 +00:00
|
|
|
bool operator >(DateTime other) {
|
|
|
|
return millisecondsSinceEpoch > other.millisecondsSinceEpoch;
|
2020-01-09 11:16:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:58:59 +00:00
|
|
|
bool operator >=(DateTime other) {
|
|
|
|
return millisecondsSinceEpoch >= other.millisecondsSinceEpoch;
|
2020-01-09 11:16:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 13:58:59 +00:00
|
|
|
bool operator <=(DateTime other) {
|
|
|
|
return millisecondsSinceEpoch <= other.millisecondsSinceEpoch;
|
2020-01-09 11:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Two message events can belong to the same environment. That means that they
|
|
|
|
/// don't need to display the time they were sent because they are close
|
|
|
|
/// enaugh.
|
2023-07-22 10:05:24 +00:00
|
|
|
static const minutesBetweenEnvironments = 10;
|
2020-01-09 11:16:34 +00:00
|
|
|
|
|
|
|
/// Checks if two DateTimes are close enough to belong to the same
|
|
|
|
/// environment.
|
|
|
|
bool sameEnvironment(DateTime prevTime) {
|
|
|
|
return millisecondsSinceEpoch - prevTime.millisecondsSinceEpoch <
|
|
|
|
1000 * 60 * minutesBetweenEnvironments;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a simple time String.
|
2024-06-27 13:26:56 +00:00
|
|
|
String localizedTimeOfDay(BuildContext context) =>
|
2024-07-04 08:59:52 +00:00
|
|
|
MediaQuery.of(context).alwaysUse24HourFormat
|
|
|
|
? DateFormat('HH:mm', L10n.of(context)!.localeName).format(this)
|
|
|
|
: DateFormat('h:mm a', L10n.of(context)!.localeName).format(this);
|
2020-01-09 11:16:34 +00:00
|
|
|
|
|
|
|
/// Returns [localizedTimeOfDay()] if the ChatTime is today, the name of the week
|
|
|
|
/// day if the ChatTime is this week and a date string else.
|
2024-03-27 07:54:15 +00:00
|
|
|
String localizedTimeShort(BuildContext context) {
|
2021-04-14 08:37:15 +00:00
|
|
|
final now = DateTime.now();
|
2020-01-09 11:16:34 +00:00
|
|
|
|
2021-04-14 08:37:15 +00:00
|
|
|
final sameYear = now.year == year;
|
2020-01-09 11:16:34 +00:00
|
|
|
|
2021-04-14 08:37:15 +00:00
|
|
|
final sameDay = sameYear && now.month == month && now.day == day;
|
2020-01-09 11:16:34 +00:00
|
|
|
|
2021-04-14 08:37:15 +00:00
|
|
|
final sameWeek = sameYear &&
|
2020-01-09 11:16:34 +00:00
|
|
|
!sameDay &&
|
2020-05-13 13:58:59 +00:00
|
|
|
now.millisecondsSinceEpoch - millisecondsSinceEpoch <
|
2020-01-09 11:16:34 +00:00
|
|
|
1000 * 60 * 60 * 24 * 7;
|
|
|
|
|
|
|
|
if (sameDay) {
|
2024-03-27 07:54:15 +00:00
|
|
|
return localizedTimeOfDay(context);
|
2020-01-09 11:16:34 +00:00
|
|
|
} else if (sameWeek) {
|
2023-05-16 08:18:09 +00:00
|
|
|
return DateFormat.EEEE(Localizations.localeOf(context).languageCode)
|
|
|
|
.format(this);
|
2020-01-09 11:16:34 +00:00
|
|
|
} else if (sameYear) {
|
2021-12-03 16:29:32 +00:00
|
|
|
return L10n.of(context)!.dateWithoutYear(
|
2023-03-02 09:57:52 +00:00
|
|
|
month.toString().padLeft(2, '0'),
|
|
|
|
day.toString().padLeft(2, '0'),
|
|
|
|
);
|
2020-01-09 11:16:34 +00:00
|
|
|
}
|
2023-03-02 09:57:52 +00:00
|
|
|
return L10n.of(context)!.dateWithYear(
|
|
|
|
year.toString(),
|
|
|
|
month.toString().padLeft(2, '0'),
|
|
|
|
day.toString().padLeft(2, '0'),
|
|
|
|
);
|
2020-01-09 11:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If the DateTime is today, this returns [localizedTimeOfDay()], if not it also
|
|
|
|
/// shows the date.
|
|
|
|
/// TODO: Add localization
|
|
|
|
String localizedTime(BuildContext context) {
|
2021-04-14 08:37:15 +00:00
|
|
|
final now = DateTime.now();
|
2020-01-09 11:16:34 +00:00
|
|
|
|
2021-04-14 08:37:15 +00:00
|
|
|
final sameYear = now.year == year;
|
2020-01-09 11:16:34 +00:00
|
|
|
|
2021-04-14 08:37:15 +00:00
|
|
|
final sameDay = sameYear && now.month == month && now.day == day;
|
2020-01-09 11:16:34 +00:00
|
|
|
|
|
|
|
if (sameDay) return localizedTimeOfDay(context);
|
2021-12-03 16:29:32 +00:00
|
|
|
return L10n.of(context)!.dateAndTimeOfDay(
|
2023-03-02 09:57:52 +00:00
|
|
|
localizedTimeShort(context),
|
|
|
|
localizedTimeOfDay(context),
|
|
|
|
);
|
2020-01-09 11:16:34 +00:00
|
|
|
}
|
|
|
|
}
|