aboutsummaryrefslogtreecommitdiffstats
path: root/19-Counting-Sundays/countsundays.c
blob: e3f4d5af3dc7e86be2c51d091ad2792eb348a3f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>
#include <time.h>

// Implemented a timing routine for fun :)

int main(){
    clock_t now, then;
    int c = 0;
    struct tm time = {0};

    then = clock();
    time.tm_mday = 1;

    // Starting from 1-1-1901
    for(int y = 1; y <= 100; y++){
	for(int m = 0; m < 12; m++){ // Apparently January is the 0th month, 
            time.tm_mon = m;        // even though there's no 0th day.... wtf K&R??
	    time.tm_year = y;
	    time_t inttime = mktime(&time);
	    struct tm *t = localtime(&inttime);
	    if(t->tm_wday == 0)
		c++;
	}
    }
    printf("%d\n", c);
    now = clock();
    printf("Only took %f ms\n", (double)(now - then) / (CLOCKS_PER_SEC / 1000));
    return 0;
}