summaryrefslogtreecommitdiff
path: root/Porting/timecheck.c
blob: cf0303aaf4979fe4c2bf91ce4ce8d3dc936a5a14 (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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* A helper tool for perl's 2038 support.
 *	See Porting/README.y2038 for details
 */

#include <sys/types.h>
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <values.h>

int opt_v = 0;
int i;
struct tm *tmp;
time_t pt, pt_max, pt_min;

static char hexbuf[80];
char *hex (time_t t)
{
    if ((long long)t < 0)
	sprintf (hexbuf, " -0x%016lx", -t);
    else
	sprintf (hexbuf, "  0x%016lx",  t);
    return (hexbuf);
    } /* hex */

void gm_check (time_t t, int min_year, int max_year)
{
    tmp = gmtime (&t);
    if ( tmp == NULL ||
	/* Check tm_year overflow */
	 tmp->tm_year < min_year || tmp->tm_year > max_year) {
	if (opt_v)
	    fprintf (stderr, "gmtime (%ld) failed with errno %d\n", t, errno);
	}
    else {
	if (opt_v)
	    fprintf (stderr, "%3d:%s: %12ld-%02d-%02d %02d:%02d:%02d\n",
		i, hex (t),
		tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
	pt = t;
	}
    } /* gm_check */

int check_gm_max ()
{
    tmp = NULL;
    pt  = 0;
    if (tmp == NULL || tmp->tm_year < 0) {
	for (i = 63; i >= 0; i--) {
	    time_t x = pt | ((time_t)1 << i);
	    if (x < 0 || x < pt) continue;
	    gm_check (x, 69, 0x7fffffff);
	    }
	}
    pt_max = pt;
    return (0);
    } /* check_gm_max */

int check_gm_min ()
{
    tmp = NULL;
    pt  = 0;
    if (tmp == NULL) {
	for (i = 36; i >= 0; i--) {
	    time_t x = pt - ((time_t)1 << i);
	    if (x > 0) continue;
	    gm_check (x, -1900, 70);
	    }
	}
    pt_min = pt;
    return (0);
    } /* check_gm_min */

void lt_check (time_t t, int min_year, int max_year)
{
    if (sizeof (time_t) > 4 && t > 0x7ffffffffffff000LL)
	tmp = NULL;
    else
	tmp = localtime (&t);
    if ( tmp == NULL ||
	/* Check tm_year overflow */
	 tmp->tm_year < min_year || tmp->tm_year > max_year) {
	if (opt_v)
	    fprintf (stderr, "localtime (%ld) failed with errno %d\n", t, errno);
	}
    else {
	if (opt_v)
	    fprintf (stderr, "%3d:%s: %12ld-%02d-%02d %02d:%02d:%02d\n",
		i, hex (t),
		tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
		tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
	pt = t;
	}
    } /* lt_check */

int check_lt_max ()
{
    tmp = NULL;
    pt  = 0;
    if (tmp == NULL || tmp->tm_year < 0) {
	for (i = 63; i >= 0; i--) {
	    time_t x = pt | ((time_t)1 << i);
	    if (x < 0 || x < pt) continue;
	    lt_check (x, 69, 0x7fffffff);
	    }
	}
    pt_max = pt;
    return (0);
    } /* check_lt_max */

int check_lt_min ()
{
    tmp = NULL;
    pt  = 0;
    if (tmp == NULL) {
	for (i = 36; i >= 0; i--) {
	    time_t x = pt - ((time_t)1 << i);
	    if (x > 0) continue;
	    lt_check (x, -1900, 70);
	    }
	}
    pt_min = pt;
    return (0);
    } /* check_lt_min */

int main (int argc, char *argv[])
{
    time_t gm_max, gm_min, lt_max, lt_min;
    if (argc > 1 && strcmp (argv[1], "-v") == 0) opt_v++;

    check_gm_max (); gm_max = pt_max;
    check_gm_min (); gm_min = pt_min;
    check_lt_max (); lt_max = pt_max;
    check_lt_min (); lt_min = pt_min;

    opt_v++;
    printf ("======================\n");
    printf ("Sizeof time_t = %ld\n", (i = sizeof (time_t)));
    printf ("gmtime () boundaries:\n");
    gm_check (gm_max,    69, 0x7fffffff);
    gm_check (gm_min, -1900,         70);
    printf ("localtime () boundaries:\n");
    lt_check (lt_max,    69, 0x7fffffff);
    lt_check (lt_min, -1900,         70);
    printf ("Configure variables:\n");
    printf ("sGMTIME_max='%ld'\n",    gm_max);
    printf ("sGMTIME_min='%ld'\n",    gm_min);
    printf ("sLOCALTIME_max='%ld'\n", lt_max);
    printf ("sLOCALTIME_min='%ld'\n", lt_min);
    return (0);
    } /* main */