Hey guys!

Looking to implement some functions that relate to time within a project I am working on and decided to look at what other devs have done to handle time within C. I decided to look into the C standard libraries, in particular muslc (I've read that this is a good standard library) in hopes to guide my work and to learn more about how it is implemented.

I have encounted a function that is beyond my comprehension by just reading the source alone. There is no documentation I can find on it, however I am new to poking around large libraries like this, so I am not entirely sure where to look either. The function in question is as follows:

Code:
long long __year_to_secs(long long year, int *is_leap)
{
    if (year-2ULL <= 136) {
        int y = year;
        int leaps = (y-68)>>2;
        if (!((y-68)&3)) {
            leaps--;
            if (is_leap) *is_leap = 1;
        } else if (is_leap) *is_leap = 0;
        return 31536000*(y-70) + 86400*leaps;
    }

    int cycles, centuries, leaps, rem;

    if (!is_leap) is_leap = &(int){0};
    cycles = (year-100) / 400;
    rem = (year-100) % 400;
    if (rem < 0) {
        cycles--;
        rem += 400;
    }
    if (!rem) {
        *is_leap = 1;
        centuries = 0;
        leaps = 0;
    } else {
        if (rem >= 200) {
            if (rem >= 300) centuries = 3, rem -= 300;
            else centuries = 2, rem -= 200;
        } else {
            if (rem >= 100) centuries = 1, rem -= 100;
            else centuries = 0;
        }
        if (!rem) {
            *is_leap = 0;
            leaps = 0;
        } else {
            leaps = rem / 4U;
            rem %= 4U;
            *is_leap = !rem;
        }
    }

    leaps += 97*cycles + 24*centuries - *is_leap;

    return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
}
I am posting this here to hopefully get some insight into understanding this code (concepts and algorithms used), and perhaps be pointed to some resources that might be available to me to aid in understanding it (documentation I may be missing?).