2024-11-07 14:46:08 +00:00
|
|
|
import 'package:cake_wallet/generated/i18n.dart';
|
2020-09-21 11:50:26 +00:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:cake_wallet/di.dart';
|
|
|
|
import 'package:cake_wallet/store/settings_store.dart';
|
|
|
|
|
|
|
|
class DateFormatter {
|
2022-09-02 13:10:54 +00:00
|
|
|
static String currentLocalFormat({bool hasTime = true, bool reverse = false}) {
|
2020-09-28 15:47:43 +00:00
|
|
|
final isUSA = getIt.get<SettingsStore>().languageCode.toLowerCase() == 'en';
|
2024-11-07 14:46:08 +00:00
|
|
|
final format = isUSA ? usaStyleFormat(hasTime, reverse) : regularStyleFormat(hasTime, reverse);
|
2020-09-21 11:50:26 +00:00
|
|
|
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
2022-09-02 13:10:54 +00:00
|
|
|
static DateFormat withCurrentLocal({bool hasTime = true, bool reverse = false}) => DateFormat(
|
|
|
|
currentLocalFormat(hasTime: hasTime, reverse: reverse),
|
2020-10-22 15:11:52 +00:00
|
|
|
getIt.get<SettingsStore>().languageCode);
|
|
|
|
|
2022-09-02 13:10:54 +00:00
|
|
|
static String usaStyleFormat(bool hasTime, bool reverse) =>
|
|
|
|
hasTime ? (reverse ? 'HH:mm yyyy.MM.dd' : 'yyyy.MM.dd, HH:mm') : 'yyyy.MM.dd';
|
2020-10-22 15:11:52 +00:00
|
|
|
|
2022-09-02 13:10:54 +00:00
|
|
|
static String regularStyleFormat(bool hasTime, bool reverse) =>
|
|
|
|
hasTime ? (reverse ? 'HH:mm dd.MM.yyyy' : 'dd.MM.yyyy, HH:mm') : 'dd.MM.yyyy';
|
2024-11-07 14:46:08 +00:00
|
|
|
|
|
|
|
static String convertDateTimeToReadableString(DateTime date) {
|
|
|
|
final nowDate = DateTime.now();
|
|
|
|
final diffDays = date.difference(nowDate).inDays;
|
|
|
|
final isToday =
|
|
|
|
nowDate.day == date.day && nowDate.month == date.month && nowDate.year == date.year;
|
|
|
|
final dateSectionDateFormat = withCurrentLocal(hasTime: false);
|
|
|
|
var title = "";
|
|
|
|
|
|
|
|
if (isToday) {
|
|
|
|
title = S.current.today;
|
|
|
|
} else if (diffDays == 0) {
|
|
|
|
title = S.current.yesterday;
|
|
|
|
} else if (diffDays > -7 && diffDays < 0) {
|
|
|
|
final dateFormat = DateFormat.EEEE();
|
|
|
|
title = dateFormat.format(date);
|
|
|
|
} else {
|
|
|
|
title = dateSectionDateFormat.format(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
return title;
|
|
|
|
}
|
2020-09-21 11:50:26 +00:00
|
|
|
}
|