xref: /haiku/src/system/libroot/posix/musl/time/__year_to_secs.c (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
1 long long __year_to_secs(long long year, int *is_leap)
2 {
3 	if (year-2ULL <= 136) {
4 		int y = year;
5 		int leaps = (y-68)>>2;
6 		if (!((y-68)&3)) {
7 			leaps--;
8 			if (is_leap) *is_leap = 1;
9 		} else if (is_leap) *is_leap = 0;
10 		return 31536000*(y-70) + 86400*leaps;
11 	}
12 
13 	{
14 	int cycles, centuries, leaps, rem;
15 
16 	if (!is_leap) {
17 		static int isleap = 0;
18 		is_leap = &isleap;
19 	}
20 	cycles = (year-100) / 400;
21 	rem = (year-100) % 400;
22 	if (rem < 0) {
23 		cycles--;
24 		rem += 400;
25 	}
26 	if (!rem) {
27 		*is_leap = 1;
28 		centuries = 0;
29 		leaps = 0;
30 	} else {
31 		if (rem >= 200) {
32 			if (rem >= 300) centuries = 3, rem -= 300;
33 			else centuries = 2, rem -= 200;
34 		} else {
35 			if (rem >= 100) centuries = 1, rem -= 100;
36 			else centuries = 0;
37 		}
38 		if (!rem) {
39 			*is_leap = 0;
40 			leaps = 0;
41 		} else {
42 			leaps = rem / 4U;
43 			rem %= 4U;
44 			*is_leap = !rem;
45 		}
46 	}
47 
48 	leaps += 97*cycles + 24*centuries - *is_leap;
49 
50 	return (year-100) * 31536000LL + leaps * 86400LL + 946684800 + 86400;
51 	}
52 }
53