import 'package:intl/intl.dart'; /// The time next to a room in the list: the time today, the weekday this /// week, the date before that. String formatListTime(DateTime time, {DateTime? now}) { final local = time.toLocal(); final today = _day(now ?? DateTime.now()); final day = _day(local); final days = today.difference(day).inDays; if (days <= 0) return DateFormat.Hm().format(local); if (days == 1) return 'Yesterday'; if (days < 7) return DateFormat.E().format(local); if (local.year == today.year) return DateFormat.MMMd().format(local); return DateFormat.yMd().format(local); } /// The separator above a day's messages. String formatDay(DateTime day, {DateTime? now}) { final today = _day(now ?? DateTime.now()); final days = today.difference(_day(day)).inDays; if (days == 0) return 'Today'; if (days == 1) return 'Yesterday'; if (days < 7) return DateFormat.EEEE().format(day); if (day.year == today.year) return DateFormat.MMMMEEEEd().format(day); return DateFormat.yMMMMEEEEd().format(day); } /// The time under a message. String formatTime(DateTime time) => DateFormat.Hm().format(time.toLocal()); /// A file size for people: 980 B, 12 KB, 3.4 MB. String formatBytes(int bytes) { if (bytes < 1000) return '$bytes B'; const units = ['KB', 'MB', 'GB']; var value = bytes / 1000; var unit = 0; while (value >= 1000 && unit < units.length - 1) { value /= 1000; unit++; } final digits = value < 10 ? 1 : 0; return '${value.toStringAsFixed(digits)} ${units[unit]}'; } DateTime _day(DateTime t) => DateTime(t.year, t.month, t.day);