Advertisement
Guest User

easter.c

a guest
Apr 21st, 2019
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1.  
  2. /**
  3. // easter.c - calculate date of easter
  4. //
  5. // based on https://youtu.be/u7UNEDYN2xY
  6. // "How to Calculate the Date of the Easter Weekend \
  7. //     - Simple Formula - Step by Step Tutorial"
  8. //
  9. // see also:
  10. // http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html
  11. // https://www.timeanddate.com/calendar/determining-easter-date.html
  12. //
  13. // previous: https://pastebin.com/e0QDUfJW
  14. //           https://boards.4channel.org/g/thread/70603712#p70608743
  15. //
  16. // new: use current year if no arg given (2019-04-21)
  17. **/
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22.  
  23. int main(int argc, char *argv[]) {
  24.     int year, month, day, dow;
  25.     int x, metonic;
  26.     struct tm date;
  27.     time_t t;
  28.  
  29.     x = 1;
  30.     t = time(NULL); localtime_r(&t, &date);
  31.     year = 1900 + date.tm_year;
  32.  
  33.     if(argc-1 == 1) {
  34.         x = sscanf(argv[1], "%d", &year);
  35.         if(x == 1) {
  36.             if(year < 32) {
  37.                 puts("easter is under construction...");
  38.                 exit(0);
  39.             }
  40.  
  41.             if(year < 1970) {
  42.                 puts("unix time routines don't work before 1970");
  43.                 exit(1);
  44.             }
  45.         }
  46.     }
  47.  
  48.     if(1 < argc-1 || x <= 0) {
  49.         puts("usage: easter [year]\nreport the date of easter");
  50.     }
  51.  
  52.     metonic = 1 + (year % 19); // lookup table [1..20]
  53.  
  54.     typedef struct { int month; int day; } month_day;
  55.     enum
  56.     {
  57.         Jan=0, Feb, Mar, Apr, May, Jun,
  58.         Jul,   Aug, Sep, Oct, Nov, Dec
  59.     };
  60.  
  61.     month_day table[] = {
  62.         { Mar, 27 }, { Apr, 14 }, { Apr,  3 }, { Mar, 23 },
  63.         { Apr, 11 }, { Mar, 31 }, { Apr, 18 }, { Apr,  8 },
  64.         { Mar, 28 }, { Apr, 16 }, { Apr,  5 }, { Mar, 25 },
  65.         { Apr, 13 }, { Apr,  2 }, { Mar, 22 }, { Apr, 10 },
  66.         { Mar, 30 }, { Apr, 17 }, { Apr,  7 }, { Mar, 27 }
  67.     };
  68.  
  69.     month = table[metonic].month;
  70.     day   = table[metonic].day;
  71.  
  72.     date.tm_year = year-1900;
  73.     date.tm_mon  = month;
  74.     date.tm_mday = day;
  75.  
  76.     /* round-trip conversion to fill in day of week */
  77.     t = mktime(&date); localtime_r(&t, &date);
  78.  
  79.     dow = date.tm_wday % 7; // Sun == 0
  80.     date.tm_mday += (7-dow);
  81.     t = mktime(&date); localtime_r(&t, &date);
  82.  
  83.     char str[128];
  84.     strftime(str, sizeof(str)-1, "%a, %b %d, %Y", &date);
  85.     printf("%s\n", str);
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement