Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // easter.c - calculate date of easter
- //
- // based on https://youtu.be/u7UNEDYN2xY
- // "How to Calculate the Date of the Easter Weekend \
- // - Simple Formula - Step by Step Tutorial"
- //
- // see also:
- // http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
- // https://www.timeanddate.com/calendar/determining-easter-date.html
- //
- // previous: https://pastebin.com/e0QDUfJW
- // https://boards.4channel.org/g/thread/70603712#p70608743
- // coding style is still inconsistant...
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main(int argc, char *argv[]) {
- int x, year;
- if(argc-1 == 1) {
- x = sscanf(argv[1], "%d", &year);
- if(x == 1) {
- if(year < 32) {
- puts("easter is under construction...");
- exit(0);
- }
- // unix epoch: Jan 1st, 1970
- // MS-DOS epoch: Jan 1st, 1980
- // Common Lisp epoch: Jan 1st, 1900 etc...
- if(year < 1970) {
- puts("unix time routines don't work before 1970");
- exit(1)
- }
- }
- }
- if(argc-1 != 1 || x <= 0) {
- printf("usage: easter <year>\n");
- exit(1);
- }
- int metonic = 1 + (year % 19); // lookup table [1..20]
- typedef struct { int month; int day; } month_day;
- enum
- {
- Jan=0, Feb, Mar, Apr, May, Jun,
- Jul, Aug, Sep, Oct, Nov, Dec
- };
- month_day table[] = {
- // entry [0] is dummy, end wrap-around
- { Mar, 27 }, { Apr, 14 }, { Apr, 3 }, { Mar, 23 },
- { Apr, 11 }, { Mar, 31 }, { Apr, 18 }, { Apr, 8 },
- { Mar, 28 }, { Apr, 16 }, { Apr, 5 }, { Mar, 25 },
- { Apr, 13 }, { Apr, 2 }, { Mar, 22 }, { Apr, 10 },
- { Mar, 30 }, { Apr, 17 }, { Apr, 7 }, { Mar, 27 }
- };
- int month = table[metonic].month;
- int day = table[metonic].day;
- struct tm date;
- date.tm_year = year-1900;
- date.tm_mon = month;
- date.tm_mday = day;
- /* round-trip conversion to fill in day of week */
- time_t t = mktime(&date); localtime_r(&t, &date);
- /* can DST or leap year/seconds cause negative weekday? */
- int dow = (date.tm_wday + 7) % 7; // Sun == 0
- date.tm_mday += (7-dow);
- /* round-trip to fix day of week (and possibly month) */
- t = mktime(&date); localtime_r(&t, &date);
- char str[128];
- strftime(str, sizeof(str)-1, "%a, %b %d, %Y", &date);
- printf("%s\n", str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement