From e267cf3ebd405b213126d77fa258bd4581a1939e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cao=20Gia=20Hi=E1=BA=BFu?= <57854043+CaoGiaHieu-dev@users.noreply.github.com> Date: Fri, 21 Feb 2025 13:57:34 +0700 Subject: [PATCH 1/4] add locale to DateFormat --- lib/src/_get_time_ago.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/_get_time_ago.dart b/lib/src/_get_time_ago.dart index b66855e..67947bf 100644 --- a/lib/src/_get_time_ago.dart +++ b/lib/src/_get_time_ago.dart @@ -60,7 +60,7 @@ class GetTimeAgo { // Format the dateTime using the provided pattern or the default pattern. final formattedDate = - DateFormat(pattern ?? "dd MMM, yyyy hh:mm aa").format(dateTime); + DateFormat(pattern ?? "dd MMM, yyyy hh:mm aa", locale).format(dateTime); // Get the current time for comparison. final currentClock = DateTime.now(); From b1d5795dbaef8ec62d26a266fbf0a171d569b797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cao=20Gia=20Hie=CC=82=CC=81u?= Date: Mon, 24 Feb 2025 08:21:07 +0700 Subject: [PATCH 2/4] bump package --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 9b3b873..6f615da 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: get_time_ago description: A Dart package to convert and format `DateTime` objects into human-readable 'time ago' strings like '20 seconds ago', 'a minute ago', or '7 hours ago'. -version: 2.2.0 +version: 2.2.1 homepage: https://pub.dev/packages/get_time_ago repository: https://github.com/nixrajput/get_time_ago From 8ca80ad240d04886271b5c01843ae84f87562882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cao=20Gia=20Hie=CC=82=CC=81u?= Date: Mon, 24 Feb 2025 16:07:43 +0700 Subject: [PATCH 3/4] fix test case --- lib/src/_get_time_ago.dart | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/src/_get_time_ago.dart b/lib/src/_get_time_ago.dart index 67947bf..cc6a7f0 100644 --- a/lib/src/_get_time_ago.dart +++ b/lib/src/_get_time_ago.dart @@ -1,4 +1,7 @@ +import 'dart:developer' as dev; + import 'package:get_time_ago/src/utils/data.dart'; +import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/intl.dart'; import 'messages/messages.dart'; @@ -52,6 +55,9 @@ class GetTimeAgo { String? locale, String? pattern, }) { + // Make sure Flutter Locale data has been initialized + initializeDateFormatting(); + // Get the locale, if not provided, fallback to the default locale. final selectedLocale = locale ?? _defaultLocale; @@ -59,8 +65,19 @@ class GetTimeAgo { final message = _messageMap[selectedLocale] ?? Data.defaultMessages; // Format the dateTime using the provided pattern or the default pattern. - final formattedDate = - DateFormat(pattern ?? "dd MMM, yyyy hh:mm aa", locale).format(dateTime); + + String formattedDate; + try { + formattedDate = + DateFormat(pattern ?? "dd MMM, yyyy hh:mm aa", selectedLocale) + .format(dateTime); + } on ArgumentError catch (e) { + // In case locate not support by intl package + dev.log(e.toString()); + + formattedDate = + DateFormat(pattern ?? "dd MMM, yyyy hh:mm aa").format(dateTime); + } // Get the current time for comparison. final currentClock = DateTime.now(); From ab86d887a8e15a5356bbb5946463e78d5cca2994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cao=20Gia=20Hie=CC=82=CC=81u?= Date: Mon, 24 Feb 2025 16:09:36 +0700 Subject: [PATCH 4/4] fix test case --- lib/src/_get_time_ago.dart | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/src/_get_time_ago.dart b/lib/src/_get_time_ago.dart index cc6a7f0..8e8683a 100644 --- a/lib/src/_get_time_ago.dart +++ b/lib/src/_get_time_ago.dart @@ -55,7 +55,7 @@ class GetTimeAgo { String? locale, String? pattern, }) { - // Make sure Flutter Locale data has been initialized + // ensure Flutter Locale data has been initialized initializeDateFormatting(); // Get the locale, if not provided, fallback to the default locale. @@ -65,16 +65,14 @@ class GetTimeAgo { final message = _messageMap[selectedLocale] ?? Data.defaultMessages; // Format the dateTime using the provided pattern or the default pattern. - String formattedDate; try { formattedDate = DateFormat(pattern ?? "dd MMM, yyyy hh:mm aa", selectedLocale) .format(dateTime); } on ArgumentError catch (e) { - // In case locate not support by intl package + // In case locale not support by intl package dev.log(e.toString()); - formattedDate = DateFormat(pattern ?? "dd MMM, yyyy hh:mm aa").format(dateTime); }