Function to return date of Easter for the given year

前端 未结 8 1443
眼角桃花
眼角桃花 2020-12-08 05:12

So, here\'s a funny little programming challenge. I was writing a quick method to determine all the market holidays for a particular year, and then I started reading about E

8条回答
  •  天涯浪人
    2020-12-08 05:51

    When it came for me to write this (traffic prediction based on day of week and holiday), I gave up on trying to write it by myself. I found it somewhere on the net. The code was public domain, but...

    sigh

    see for yourself.

    void dateOfEaster(struct tm* p)
    {
        int Y = p->tm_year;
        int a = Y % 19;
        int b = Y / 100;
        int c = Y % 100;
        int d = b / 4;
        int e = b % 4;
        int f = (b + 8) / 25;
        int g = (b - f + 1) / 3;
        int h = (19 * a + b - d - g + 15) % 30;
        int i = c / 4;
        int k = c % 4;
        int L = (32 + 2 * e + 2 * i - h - k) % 7;
        int m = (a + 11 * h + 22 * L) / 451;
        p->tm_mon = ((h + L - 7 * m + 114) / 31 ) - 1;
        p->tm_mday = ((h + L - 7 * m + 114) % 31) + 1;
        p->tm_hour = 12;
        const time_t tmp = mktime(p);
        *p = *localtime(&tmp);  //recover yday from mon+mday
    }
    

    Some questions are better left unasked.

    I feel lucky that all moving holidays in my country are a fixed offset from the date of Easter.

提交回复
热议问题