Skip to content

Instantly share code, notes, and snippets.

@halftrue
Created August 12, 2016 15:01
Show Gist options
  • Save halftrue/01942dcbb4a2f79003e1c0f4b1e0c46f to your computer and use it in GitHub Desktop.
Save halftrue/01942dcbb4a2f79003e1c0f4b1e0c46f to your computer and use it in GitHub Desktop.
万年历计算星期-基姆拉尔森公式
#include <stdio.h>
/*
* 基姆拉尔森计算公式
* W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7
*/
int week(int y, int m, int d)
{
if (m < 3) {
m += 12;
y--;
}
int w = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
return w;
}
int main()
{
printf("%d\n", week(2015, 4, 16)); // => 3 星期四
printf("%d\n", week(1989, 2, 3)); // => 4 星期五
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment