safeDateTime static method

DateTime? safeDateTime(
  1. dynamic value
)

Helper para conversión segura de DateTime

Implementation

static DateTime? safeDateTime(dynamic value) {
  if (value == null) return null;

  // Handle numeric timestamps (milliseconds since epoch)
  if (value is int) {
    return DateTime.fromMillisecondsSinceEpoch(value, isUtc: true);
  }
  if (value is double) {
    return DateTime.fromMillisecondsSinceEpoch(value.toInt(), isUtc: true);
  }

  // Handle string timestamps (ISO format)
  if (value is String) {
    // Try to parse as number first (in case it's a numeric string)
    final numericValue = int.tryParse(value);
    if (numericValue != null) {
      return DateTime.fromMillisecondsSinceEpoch(numericValue, isUtc: true);
    }
    // Otherwise try to parse as ISO date string
    return DateTime.tryParse(value);
  }

  // For other types, try to convert to string and parse
  return DateTime.tryParse(value.toString());
}