calculateEthiopianAge static method

Map<String, int> calculateEthiopianAge({
  1. required int birthYear,
  2. required int birthMonth,
  3. required int birthDay,
})

Calculate age from birthdate in Ethiopian calendar Returns Map with years, months, and days

Implementation

static Map<String, int> calculateEthiopianAge({
  required int birthYear,
  required int birthMonth,
  required int birthDay,
}) {
  final today = getTodayEthiopian();

  int years = today['year']! - birthYear;
  int months = today['month']! - birthMonth;
  int days = today['day']! - birthDay;

  /// Adjust for negative days using the exact number of days in the previous Ethiopian month
  if (days < 0) {
    months--;

    /// Compute the exact number of days in the previous Ethiopian month by:
    /// - Taking the first day of the current Ethiopian month
    /// - Converting to Gregorian and subtracting one day
    /// - Converting back to Ethiopian and reading the day number (length)
    final firstOfThisMonthEt = EtDatetime(
      year: today['year']!,
      month: today['month']!,
      day: 1,
    );
    final firstOfThisMonthGr = DateTime.fromMillisecondsSinceEpoch(firstOfThisMonthEt.moment);
    final lastOfPrevMonthGr = firstOfThisMonthGr.subtract(const Duration(days: 1));
    final lastOfPrevMonthEt = EtDatetime.fromMillisecondsSinceEpoch(lastOfPrevMonthGr.millisecondsSinceEpoch);
    final prevMonthDays = lastOfPrevMonthEt.day; // exact length of previous month (5/6/30)

    days += prevMonthDays;
  }

  /// Adjust for negative months
  if (months < 0) {
    years--;
    months += 13; /// Ethiopian calendar has 13 months
  }

  return {
    'years': years,
    'months': months,
    'days': days,
  };
}