cal.zeller

ツェラーの公式

cal.zeller.rs

use std::fmt;

#[derive(Debug)]
enum Day {
  Sat, Sun, Mon, Tue, Wed, Thu, Fri
}

impl Day {
    fn from(i: usize) -> Day {
        match i {
            0 => Day::Sat,
            1 => Day::Sun,
            2 => Day::Mon,
            3 => Day::Tue,
            4 => Day::Wed,
            5 => Day::Thu,
            6 => Day::Fri,
            _ => panic!("no day")
        }
    }
}

impl fmt::Display for Day {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}",
            match self {
                Day::Sat => "Saturday",
                Day::Sun => "Sunday",
                Day::Mon => "Monday",
                Day::Tue => "Tuesday",
                Day::Wed => "Wednesday",
                Day::Thu => "Thursday",
                Day::Fri => "Friday"
            })
    }
}

fn zeller(y: usize, m: usize, d: usize) -> Day {
    assert!(m >= 1 && d >= 1);
    let (year, month) = if m <= 2 {
        (y - 1, m + 12)
    } else {
        (y, m)
    };
    let y_up = year % 100;
    let y_down = year / 100;
    let gamma;
    // Gregorian if (1582 <= y)
    {
        gamma = 5 * y_down + y_down / 4;
    }
    // Julian if 4 <= y && y <= 1582
    // {
    //     gamma = 6 * c + 5;
    // };
    let h = d + (26 * (month + 1) / 10) + y_up + y_up / 4 + gamma;
    Day::from(h % 7)
}

fn main() {
    for m in 11..13 {
        for d in 1..32 {
            let day = zeller(2018, m, d);
            println!("2018-{}-{}: {} {:?}", m, d, day, day);
        }
    }
}

cal.zeller.cc

enum Day {
  Sat = 0,
  Sun = 1,
  Mon, Tue, Wed, Thu, Fri
};

Day zeller(int y, int m, int d) {
  if (m == 1 || m == 2) {
    --y; m += 12;
  }
  int Y = y % 100;
  int c = y / 100;
  int gamma = 5 * c + c / 4; // Gregorian (1582 <= y)
  //int gamma = 6 * c + 5; // Julian (4 <= y <= 1582)
  int h = d + int(26 * (m+1) / 10)
    + Y + Y / 4 + gamma;
  h %= 7;
  return Day(h);
}

string show_day (Day a) {
  switch (a) {
    case Mon: return "Monday";
    case Tue: return "Tuesday";
    case Wed: return "Wednesday";
    case Thu: return "Thursday";
    case Fri: return "Friday";
    case Sat: return "Saturday";
    case Sun: return "Sunday";
    default: return "Holiday";
  }
}

int main() {
  cout << (Fri == zeller(2014, 11,28)) << endl;
  cout << (Tue == zeller(1990, 11, 6)) << endl;
  return 0;
}