Skip to content

Instantly share code, notes, and snippets.

@mjf
Created September 9, 2011 17:48
Show Gist options
  • Save mjf/1206860 to your computer and use it in GitHub Desktop.
Save mjf/1206860 to your computer and use it in GitHub Desktop.
Year - normal, leap or calendar change 1752 year?
/**
* Year - normal, leap or calendar change 1752 year?
* Copyright (C) 2011 Matous J. Fialka, <http://mjf.cz/>
* Released under the terms of The MIT License
*
* Install as follows:
*
* $ cc -pedantic -ansi -pedantic-errors -Wall -Werror year.c -o year
* $ su -c 'cp year /usr/local/bin'
*/
#include <stdlib.h>
#include <stdio.h>
int
january1(const int y)
{
int d = y + 4 + (y + 3) / 4;
if(y > 1800)
d -= (y - 1701) / 100, d += (y - 1601) / 400;
if(y > 1752)
d += 3;
return d % 7;
}
int
main(int argc, char *argv[])
{
int y = 0;
if(argc < 2) {
fprintf(stderr, "Usage: year [year ...]\n");
return 1;
}
while(*++argv) {
y = atoi(*argv);
printf("%d is ", y);
switch ((january1(y + 1) + 7 - january1(y)) % 7) {
case 1:
printf("normal");
break;
case 2:
printf("leap");
break;
default:
printf("calendar change");
break;
}
printf(" year\n");
}
return 0;
}
@mjf
Copy link
Author

mjf commented Sep 9, 2011

Example usage may be as follows:

$ year 1620 1752 2011
1620 is leap year
1752 is calendar change year
2011 is normal year

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment