getSolarTime method

SolarTime getSolarTime()

获取对应的公历时间

Implementation

SolarTime getSolarTime() {
  double jd = _day + 0.5;
  int z = jd.floor();
  double f = jd - z;

  late int year, month, day;
  late int hour, minute;
  late double second;

  if (z < 2299161) {
    int a = z;
    int b = a + 1524;
    int c = ((b - 122.1) / 365.25).floor();
    int d = (365.25 * c).floor();
    int e = ((b - d) / 30.6001).floor();

    day = (b - d - (30.6001 * e).floor() + f).floor();

    if (e < 14) {
      month = e - 1;
    } else {
      month = e - 13;
    }

    if (month > 2) {
      year = c - 4716;
    } else {
      year = c - 4715;
    }
  } else {
    int a = ((z - 1867216.25) / 36524.25).floor();
    int b = z + 1 + a - (a / 4).floor();
    int c = b + 1524;
    int d = ((c - 122.1) / 365.25).floor();
    int e = (365.25 * d).floor();
    int g = ((c - e) / 30.6001).floor();

    day = (c - e - (30.6001 * g).floor() + f).floor();

    if (g < 14) {
      month = g - 1;
    } else {
      month = g - 13;
    }

    if (month > 2) {
      year = d - 4716;
    } else {
      year = d - 4715;
    }
  }

  // 计算时分秒
  double totalSeconds = f * 86400.0; // 一天有86400秒
  hour = (totalSeconds / 3600).floor();
  minute = ((totalSeconds - hour * 3600) / 60).floor();
  second = totalSeconds - hour * 3600 - minute * 60;

  return SolarTime(year, month, day, hour, minute, second);
}