From 3b2c4fdff53e625feb92023f0825ea70a337a4f1 Mon Sep 17 00:00:00 2001 From: mso7nueh Date: Mon, 16 Jun 2025 13:41:39 +0300 Subject: [PATCH] fix(chat): time --- lib/core/utils/date_time_utils.dart | 34 ++++++++++++------- .../widgets/chat_bubble_widget.dart | 4 +-- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/lib/core/utils/date_time_utils.dart b/lib/core/utils/date_time_utils.dart index b6c1c86e..f10ae9ec 100644 --- a/lib/core/utils/date_time_utils.dart +++ b/lib/core/utils/date_time_utils.dart @@ -8,31 +8,39 @@ extension DateTimeExtension on DateTime { String pattern = dateTimeFormatPattern, String? locale, ]) { + final localDate = isUtc ? toLocal() : this; if (locale != null && locale.isNotEmpty) { initializeDateFormatting(locale); } - return DateFormat(pattern, locale).format(this); + return DateFormat(pattern, locale).format(localDate); } - bool isYesterday(DateTime date) { - final DateTime localDate = date.isUtc ? date.toLocal() : date; - final yesterday = DateTime.now().subtract(const Duration(days: 1)); - return yesterday.day == localDate.day && yesterday.month == localDate.month && yesterday.year == localDate.year; - } - - bool isToday(DateTime date) { - final DateTime localDate = date.isUtc ? date.toLocal() : date; + bool isYesterday() { + final localDate = isUtc ? toLocal() : this; final now = DateTime.now(); - return now.day == localDate.day && now.month == localDate.month && now.year == localDate.year; + final yesterday = DateTime(now.year, now.month, now.day).subtract(const Duration(days: 1)); + return localDate.year == yesterday.year && + localDate.month == yesterday.month && + localDate.day == yesterday.day; + } + + bool isToday() { + final localDate = isUtc ? toLocal() : this; + final now = DateTime.now(); + return now.year == localDate.year && + now.month == localDate.month && + now.day == localDate.day; } } String calculateDifference(DateTime dateTime) { - if (dateTime.isToday(dateTime)) { + final localDateTime = dateTime.isUtc ? dateTime.toLocal() : dateTime; + + if (localDateTime.isToday()) { return 'Сегодня'; - } else if (dateTime.isYesterday(dateTime)) { + } else if (localDateTime.isYesterday()) { return 'Вчера'; } else { - return dateTime.format('d MMMM', 'ru'); + return localDateTime.format('d MMMM', 'ru'); } } diff --git a/lib/features/chat/presentation/widgets/chat_bubble_widget.dart b/lib/features/chat/presentation/widgets/chat_bubble_widget.dart index b80b12ac..74e6f6a6 100644 --- a/lib/features/chat/presentation/widgets/chat_bubble_widget.dart +++ b/lib/features/chat/presentation/widgets/chat_bubble_widget.dart @@ -39,7 +39,7 @@ class ChatBubbleWidget extends StatelessWidget { Padding( padding: const EdgeInsets.only(right: 4, bottom: 4), child: Text( - sendTime.format('hh:mm'), + sendTime.format('HH:mm', 'ru'), style: const TextStyle( color: Color(0xFF7B8794), height: 1, @@ -144,7 +144,7 @@ class ChatBubbleWidget extends StatelessWidget { Padding( padding: const EdgeInsets.only(left: 4, bottom: 4), child: Text( - sendTime.format('hh:mm'), + sendTime.format('HH:mm', 'ru'), style: const TextStyle( color: Color(0xFF7B8794), height: 1,