solarToLunar static method

Lunar solarToLunar(
  1. Solar solar
)

Implementation

static Lunar solarToLunar(Solar solar) {
  Lunar lunar = Lunar(solar.dateTime);
  int index = solar.solarYear - _solar_1_1[0];
  int data =
      (solar.solarYear << 9) | (solar.solarMonth << 5) | (solar.solarDay);
  int solar11 = 0;

  if (_solar_1_1[index] > data) {
    index--;
  }

  solar11 = _solar_1_1[index];
  int y = _getBitInt(solar11, 12, 9);
  int m = _getBitInt(solar11, 4, 5);
  int d = _getBitInt(solar11, 5, 0);
  int offset =
      _solarToInt(solar.solarYear, solar.solarMonth, solar.solarDay) -
          _solarToInt(y, m, d);

  int days = _lunarMonthDays[index];
  int leap = _getBitInt(days, 4, 13);

  int lunarY = index + _solar_1_1[0];
  int lunarM = 1;
  int lunarD = 1;
  offset += 1;

  for (int i = 0; i < 13; i++) {
    int dm = _getBitInt(days, 1, 12 - i) == 1 ? 30 : 29;

    if (offset > dm) {
      lunarM++;
      offset -= dm;
    } else {
      break;
    }
  }

  lunarD = offset;
  lunar.lunarYear = lunarY;
  lunar.lunarMonth = lunarM;
  lunar.isLeap = false;

  if (leap != 0 && lunarM > leap) {
    lunar.lunarMonth = lunarM - 1;

    if (lunarM == leap + 1) {
      lunar.isLeap = true;
    }
  }

  lunar.lunarDay = lunarD;
  return lunar;
}