parseAndValidateDateString static method

Map<String, int> parseAndValidateDateString(
  1. String dateStr, {
  2. bool isEthiopian = false,
})

Validate and parse date string with strict format checking

Implementation

static Map<String, int> parseAndValidateDateString(String dateStr, {bool isEthiopian = false}) {
  if (!_isValidDateFormat(dateStr)) {
    throw Exception('Invalid date format. Use day/month/year or day-month-year');
  }

  final parts = dateStr.contains('/') ? dateStr.split('/') : dateStr.split('-');

  final day = int.parse(parts[0]);
  final month = int.parse(parts[1]);
  final year = int.parse(parts[2]);

  if (isEthiopian) {
    /// Validate Ethiopian date
    if (month < 1 || month > 13) {
      throw Exception('Ethiopian month must be between 1 and 13');
    }
    if (month == 13 && day > 6) {
      throw Exception('Pagume month can only have up to 6 days');
    }
    if (day < 1 || day > 30) {
      throw Exception('Day must be between 1 and 30');
    }
  } else {
    /// Validate Gregorian date
    if (month < 1 || month > 12) {
      throw Exception('Month must be between 1 and 12');
    }
    if (day < 1 || day > 31) {
      throw Exception('Day must be between 1 and 31');
    }
  }

  return {'day': day, 'month': month, 'year': year};
}