getWeek method

int getWeek()

获取星期,0代表周日,1代表周一 @return 0123456

Implementation

int getWeek() {
  Solar start = Solar.fromYmd(1582, 10, 15);
  int y = _year;
  int m = _month;
  int d = _day;
  Solar current = Solar.fromYmd(y, m, d);
  // 蔡勒公式
  if (m < 3) {
    m += 12;
    y--;
  }
  int c = (y / 100).floor();
  y = y - c * 100;
  int x = y + (y / 4).floor() + (c / 4).floor() - 2 * c;
  int w;
  if (current.isBefore(start)) {
    w = (x + (13 * (m + 1) / 5).floor() + d + 2) % 7;
  } else {
    w = (x + (26 * (m + 1) / 10).floor() + d - 1) % 7;
  }
  return (w + 7) % 7;
}