SHOW:
|
|
- or go back to the newest paste.
1 | #include <stdio.h> | |
2 | #include <string.h> | |
3 | #include <stdlib.h> | |
4 | #include <time.h> | |
5 | ||
6 | - | #if 0 |
6 | + | |
7 | #define Q(a,b) ((a)>0 ? (a)/(b) : -(((b)-(a)-1)/(b))) | |
8 | ||
9 | time_t tm_to_time(struct tm *tm) | |
10 | - | /* copied from Musl C awesome small implementation, see LICENSE. */ |
10 | + | |
11 | - | time_t |
11 | + | |
12 | - | tm_to_time(struct tm *tm) { |
12 | + | |
13 | - | time_t year = tm->tm_year - 100; |
13 | + | |
14 | - | int day = tm->tm_mday; |
14 | + | |
15 | ||
16 | - | /* TODO: shouldnt 335 be 334, if so file a bug for Musl, test this */ |
16 | + | |
17 | - | int daysbeforemon[] = { 0,31,59,90,120,151,181,212,243,273,304,335 }; |
17 | + | |
18 | year += month/12; | |
19 | - | z4 = Q(year - (tm->tm_mon < 2), 4); |
19 | + | |
20 | } else if (month < 0) { | |
21 | year += month/12; | |
22 | - | day += year * 365 + z4 - z100 + z400 + |
22 | + | |
23 | - | daysbeforemon[tm->tm_mon]; |
23 | + | |
24 | - | return (time_t)day * 86400 |
24 | + | |
25 | - | + tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec |
25 | + | |
26 | } | |
27 | } | |
28 | - | #endif |
28 | + | |
29 | z100 = Q(z4, 25); | |
30 | z400 = Q(z100, 4); | |
31 | /* BUG MAYBE: change 335 to 334? */ | |
32 | day += year*365 + z4 - z100 + z400 + | |
33 | month[(int []){0,31,59,90,120,151,181,212,243,273,304,335}]; | |
34 | return (long long)day*86400 | |
35 | + tm->tm_hour*3600 + tm->tm_min*60 + tm->tm_sec | |
36 | - -946684800; /* the dawn of time :) */ | |
37 | } | |
38 | ||
39 | int | |
40 | main(int argc, char **argv) { | |
41 | time_t t1; | |
42 | time_t t2; | |
43 | unsigned int i; | |
44 | struct tm tm; | |
45 | ||
46 | setenv("TZ", "UTC", 1); | |
47 | tzset(); | |
48 | ||
49 | for(i = 100; i < 133; i++) { | |
50 | memset(&tm, 0, sizeof(tm)); | |
51 | tm.tm_mon = 11; | |
52 | tm.tm_mday = 20; | |
53 | tm.tm_year = i; | |
54 | tm.tm_isdst = -1; | |
55 | t1 = tm_to_time(&tm); | |
56 | memset(&tm, 0, sizeof(tm)); | |
57 | tm.tm_mon = 11; | |
58 | tm.tm_mday = 20; | |
59 | tm.tm_year = i; | |
60 | tm.tm_isdst = -1; | |
61 | t2 = timegm(&tm); | |
62 | if(t1 != t2) { | |
63 | printf("Different time: year: %d, %d, %d\n", tm.tm_year + 1900, t1, t2); | |
64 | } | |
65 | } | |
66 | return EXIT_SUCCESS; | |
67 | } |