My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* A little program to test the limits of your system's time functions */

#include <time.h>
#include <stdio.h>
#include <math.h>

struct tm Test_TM;

time_t Time_Max;
time_t Time_Min;

time_t Time_Zero = 0;


/* Visual C++ 2008's difftime() can't do negative times */
double my_difftime(time_t left, time_t right) {
double diff = (double)left - (double)right;
return diff;
}

void check_date_max( struct tm * (*date_func)(const time_t *), char *func_name ) {
struct tm *date;
time_t time = Time_Max;
time_t good_time = 0;
time_t time_change = Time_Max;

/* Binary search for the exact failure point */
do {
printf("# Trying %s(%.0f) max...", func_name, my_difftime(time, Time_Zero));
date = (*date_func)(&time);

time_change /= 2;

/* date_func() broke or tm_year overflowed or time_t overflowed */
if(date == NULL || date->tm_year < 69 || time < good_time) {
printf(" failed\n");
time -= time_change;
}
else {
printf(" success\n");
good_time = time;
time += time_change;
}
} while(time_change > 0 && good_time < Time_Max);

printf("%s_max %.0f\n", func_name, my_difftime(good_time, Time_Zero));
}


void check_date_min( struct tm * (*date_func)(const time_t *), char *func_name ) {
struct tm *date;
time_t time = Time_Min;
time_t good_time = 0;
time_t time_change = Time_Min;

/* Binary search for the exact failure point */
do {
printf("# Trying %s(%.0f) min...", func_name, my_difftime(time, Time_Zero));
date = (*date_func)(&time);

time_change /= 2;

/* gmtime() broke or tm_year overflowed or time_t overflowed */
if(date == NULL || date->tm_year > 70 || time > good_time) {
printf(" failed\n");
time -= time_change;
}
else {
printf(" success\n");
good_time = time;
time += time_change;
}
} while((time_change > 0) && (good_time > Time_Min));

printf("%s_min %.0f\n", func_name, my_difftime(good_time, Time_Zero));
}


void guess_time_limits_from_types(void) {
if( sizeof(time_t) == 4 ) {
/* y2038 bug, out to 2**31-1 */
Time_Max = 2147483647;
Time_Min = -2147483648;
}
else if( sizeof(time_t) >= 8 ) {
/* The compiler might warn about overflowing in the assignments
below. Don't worry, these won't get run in that case */
if( sizeof(Test_TM.tm_year) == 4 ) {
/* y2**31-1 bug */
Time_Max = 67768036160140799LL;
Time_Min = -67768036191676800LL;
}
else {
/* All the way out to 2**63-1 */
Time_Max = 9223372036854775807LL;
Time_Min = -9223372036854775807LL;
}
}
else {
printf("Weird sizeof(time_t): %ld\n", sizeof(time_t));
}
}

int main(void) {
guess_time_limits_from_types();
check_date_max(gmtime, "gmtime");
check_date_max(localtime, "localtime");
check_date_min(gmtime, "gmtime");
check_date_min(localtime, "localtime");

return 0;
}

Change log

r177 by schwern on Nov 17, 2008   Diff
Detabify
Go to: 
Sign in to write a code review

Older revisions

r162 by schwern on Nov 9, 2008   Diff
Use difftime() to produce a sensible
time for printf().  This prevents
overflow on some systems (HP/UX so
far).
r161 by schwern on Nov 9, 2008   Diff
Fix debugging print typo
r160 by schwern on Nov 9, 2008   Diff
Add some permenant debugging
statements to check_max.

Fix the boundry for going over the
Max/Min times.  It was off-by-one.
All revisions of this file

File info

Size: 3188 bytes, 112 lines
Powered by Google Project Hosting