formatTimestamp static method

String formatTimestamp(
  1. DateTime time,
  2. bool humanReadable
)

Formats a timestamp based on the specified format.

If humanReadable is true, the timestamp is formatted as a human-readable string. Otherwise, it is formatted as an ISO 8601 string.

Example usage:

final timestamp = LogCommons.formatTimestamp(DateTime.now(), true);
print(timestamp); // e.g., "2025-06-18 14:22:01"

Implementation

static String formatTimestamp(DateTime time, bool humanReadable) {
  if (!humanReadable) {
    return time.toIso8601String();
  }

  final local = time.toLocal();

  final weekday = _weekdayName(local.weekday);
  final day = local.day;
  final suffix = _daySuffix(day);
  final month = _monthName(local.month);
  final year = local.year;

  final hour = local.hour % 12 == 0 ? 12 : local.hour % 12;
  final minute = local.minute.toString().padLeft(2, '0');
  final period = local.hour >= 12 ? 'PM' : 'AM';

  return '$weekday, $day$suffix $month, $year | $hour:$minute$period';
}