1- String age (DateTime dt) => difference (DateTime .now ().difference (dt));
1+ import 'package:intl/intl.dart' ;
2+ import 'package:taskwarrior/app/utils/app_settings/app_settings.dart' ;
23
3- String when (DateTime dt) => difference (dt.difference (DateTime .now ()));
4+ String age (DateTime dt, {bool ? use24HourFormat}) {
5+ final format = AppSettings .use24HourFormatRx.value;
46
5- String difference (Duration difference) {
67 String result;
7- var days = difference.abs ().inDays;
8+ var days = DateTime . now (). difference (dt) .abs ().inDays;
89 if (days > 365 ) {
910 result =
1011 '${(days / 365 ).toStringAsFixed (1 ).replaceFirst (RegExp (r'\.0$' ), '' )}y' ;
@@ -14,12 +15,46 @@ String difference(Duration difference) {
1415 result = '${days ~/ 7 }w' ;
1516 } else if (days > 0 ) {
1617 result = '${days }d' ;
17- } else if (difference.abs ().inHours > 0 ) {
18- result = '${difference .abs ().inHours }h' ;
19- } else if (difference.abs ().inMinutes > 0 ) {
20- result = '${difference .abs ().inMinutes }min' ;
18+ } else if (DateTime . now (). difference (dt) .abs ().inHours > 0 ) {
19+ result = '${DateTime . now (). difference ( dt ) .abs ().inHours }h' ;
20+ } else if (DateTime . now (). difference (dt) .abs ().inMinutes > 0 ) {
21+ result = '${DateTime . now (). difference ( dt ) .abs ().inMinutes }min' ;
2122 } else {
22- result = '${difference .abs ().inSeconds }s' ;
23+ result = '${DateTime . now (). difference ( dt ) .abs ().inSeconds }s' ;
2324 }
24- return '$result ${(difference .isNegative ) ? 'ago ' : '' }' ;
25+
26+ // Format the time part according to the format preference
27+ String timeFormat = format ? 'HH:mm' : 'hh:mm a' ;
28+ String formattedTime = DateFormat (timeFormat).format (dt);
29+
30+ return '$result ago ($formattedTime )' ;
31+ }
32+
33+ String when (DateTime dt, {bool ? use24HourFormat}) {
34+ final format = AppSettings .use24HourFormatRx.value;
35+
36+ String result;
37+ var days = dt.difference (DateTime .now ()).abs ().inDays;
38+ if (days > 365 ) {
39+ result =
40+ '${(days / 365 ).toStringAsFixed (1 ).replaceFirst (RegExp (r'\.0$' ), '' )}y' ;
41+ } else if (days > 30 ) {
42+ result = '${days ~/ 30 }mo' ;
43+ } else if (days > 7 ) {
44+ result = '${days ~/ 7 }w' ;
45+ } else if (days > 0 ) {
46+ result = '${days }d' ;
47+ } else if (dt.difference (DateTime .now ()).abs ().inHours > 0 ) {
48+ result = '${dt .difference (DateTime .now ()).abs ().inHours }h' ;
49+ } else if (dt.difference (DateTime .now ()).abs ().inMinutes > 0 ) {
50+ result = '${dt .difference (DateTime .now ()).abs ().inMinutes }min' ;
51+ } else {
52+ result = '${dt .difference (DateTime .now ()).abs ().inSeconds }s' ;
53+ }
54+
55+ // Format the time part according to the format preference
56+ String timeFormat = format ? 'HH:mm' : 'hh:mm a' ;
57+ String formattedTime = DateFormat (timeFormat).format (dt);
58+
59+ return '$result ($formattedTime )' ;
2560}
0 commit comments