Files
PiliPlus/lib/utils/date_utils.dart
My-Responsitories 37fb63c3b1 tweaks (#1252)
* opt: cache

* opt: MediaListPanel

* feat: nested replyreply panel

* tweaks

* opt: abstract class

* opt: PageStorageKey

* opt: contextExt

* opt: EpisodePanel

* opt

* opt: context instead GlobalKey

* feat: jump to reply

* refa: reply_reply

* fix: jump

* fix: index

* update

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>

* opt: keepalive

* reapply: nested replyreply

* mod: spacing

* opt: CommonSlidePageState

* fix drag bottomsheet

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>

* opt reply jump

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>

* opt reply2reply

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>

* tweaks

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>

* tweaks

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>

* reapply: jumpToReply

* fix: padding

* fix: anim

* fix some panels

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>

* opt: implements Scaffold

* opt: remove keepalive

* revert: GlobalKey

* tweaks

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>

---------

Co-authored-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
2025-09-15 18:45:28 +08:00

81 lines
2.5 KiB
Dart

import 'package:intl/intl.dart' show DateFormat;
abstract class DateFormatUtils {
static final shortFormat = DateFormat('MM-dd');
static final longFormat = DateFormat('yyyy-MM-dd');
static final _shortFormatD = DateFormat('MM-dd HH:mm');
static final longFormatD = DateFormat('yyyy-MM-dd HH:mm');
static final longFormatDs = DateFormat('yyyy-MM-dd HH:mm:ss');
static String dateFormat(
int? time, {
DateFormat? short,
DateFormat? long,
}) {
if (time == null || time == 0) {
return '';
}
final now = DateTime.now();
final date = DateTime.fromMillisecondsSinceEpoch(time * 1000);
final diff = now.difference(date);
final diffInMins = diff.inMinutes;
if (diffInMins < 1) return '刚刚';
if (diffInMins < 60) return '$diffInMins分钟前';
final diffInHours = diff.inHours;
if (diffInHours < 24) return '$diffInHours小时前';
final today = DateTime(now.year, now.month, now.day);
final dateDay = DateTime(date.year, date.month, date.day);
final dayDiff = today.difference(dateDay).inDays;
if (dayDiff == 1) {
return '昨天 ${_twoDigits(date.hour)}:${_twoDigits(date.minute)}';
}
if (dayDiff < 4) {
return '$dayDiff天前';
}
final DateFormat sdf = now.year == date.year
? short ?? shortFormat
: long ?? DateFormatUtils.longFormat;
return sdf.format(date);
}
static String _twoDigits(int n) => n.toString().padLeft(2, '0');
static String chatFormat(int? time, {bool isHistory = false}) {
if (time == null || time == 0) {
return '';
}
final now = DateTime.now();
final date = DateTime.fromMillisecondsSinceEpoch(time * 1000);
final today = DateTime(now.year, now.month, now.day);
final dateDay = DateTime(date.year, date.month, date.day);
if (today == dateDay) {
return '${isHistory ? '今天 ' : ''}${_twoDigits(date.hour)}:${_twoDigits(date.minute)}';
}
final isYesterday = today.subtract(const Duration(days: 1)) == dateDay;
if (isYesterday) {
return '昨天 ${_twoDigits(date.hour)}:${_twoDigits(date.minute)}';
}
if (isHistory) {
final DateFormat sdf = now.year == date.year
? _shortFormatD
: longFormatD;
return sdf.format(date);
}
return longFormatD.format(date);
}
static String format(int? time, {DateFormat? format}) {
if (time == null || time == 0) {
return '';
}
final date = DateTime.fromMillisecondsSinceEpoch(time * 1000);
return (format ?? longFormatD).format(date);
}
}