convertTimeTo24Hour function

List<int> convertTimeTo24Hour(
  1. String time
)

Implementation

List<int> convertTimeTo24Hour(String time) {
  // If input is empty, return current hour and minute
  if (!isNotEmpty(time)) {
    final now = DateTime.now();
    return [now.hour, now.minute];
  }

  // Normalize input
  final normalized = time.toUpperCase().replaceAll(' ', '');

  List<int> timeList = [];
  try {
    if (normalized.contains('AM') || normalized.contains('PM')) {
      final hasPM = normalized.contains('PM');
      final clean = normalized.replaceAll('AM', '').replaceAll('PM', '');
      final parts = clean.split(':');
      final h = int.tryParse(parts[0]) ?? 0;
      final m = parts.length > 1 ? int.tryParse(parts[1]) ?? 0 : 0;
      var hour24 = h % 12;
      if (hasPM) hour24 += 12;
      timeList = [hour24, m];
    } else {
      // Assume already 24-hour format like 14:30 or 4:05
      final parts = normalized.split(':');
      final h = int.tryParse(parts[0]) ?? DateTime.now().hour;
      final m = parts.length > 1 ? int.tryParse(parts[1]) ?? DateTime.now().minute : DateTime.now().minute;
      timeList = [h, m];
    }
  } catch (e) {
    // On any parsing error, fall back to current time
    final now = DateTime.now();
    logMessage('Error in convertTimeTo24Hour: $e');
    return [now.hour, now.minute];
  }

  return timeList;
}