mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
feat: match info
opt dateformat Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
79
lib/utils/date_util.dart
Normal file
79
lib/utils/date_util.dart
Normal file
@@ -0,0 +1,79 @@
|
||||
import 'package:intl/intl.dart' show DateFormat;
|
||||
|
||||
class DateUtil {
|
||||
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? shortFormat,
|
||||
DateFormat? longFormat,
|
||||
}) {
|
||||
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
|
||||
? shortFormat ?? _shortFormat
|
||||
: longFormat ?? DateUtil.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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user