This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Eliminate 4 build-time warnings in timecheck.c.
[perl5.git] / Porting / timecheck.c
CommitLineData
233e9d1c
JV
1/* A helper tool for perl's 2038 support.
2 * See Porting/README.y2038 for details
3 */
4
1b289682
MB
5#include <sys/types.h>
6#include <stdio.h>
7#include <time.h>
8#include <errno.h>
9#include <values.h>
252e731f 10#include <string.h>
1b289682
MB
11
12int opt_v = 0;
13int i;
14struct tm *tmp;
15time_t pt, pt_max, pt_min;
16
17static char hexbuf[80];
18char *hex (time_t t)
19{
20 if ((long long)t < 0)
21 sprintf (hexbuf, " -0x%016lx", -t);
22 else
23 sprintf (hexbuf, " 0x%016lx", t);
24 return (hexbuf);
25 } /* hex */
26
27void gm_check (time_t t, int min_year, int max_year)
28{
29 tmp = gmtime (&t);
30 if ( tmp == NULL ||
31 /* Check tm_year overflow */
32 tmp->tm_year < min_year || tmp->tm_year > max_year) {
33 if (opt_v)
34 fprintf (stderr, "gmtime (%ld) failed with errno %d\n", t, errno);
35 }
36 else {
37 if (opt_v)
38 fprintf (stderr, "%3d:%s: %12ld-%02d-%02d %02d:%02d:%02d\n",
39 i, hex (t),
252e731f 40 (long)(tmp->tm_year) + 1900, tmp->tm_mon + 1, tmp->tm_mday,
1b289682
MB
41 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
42 pt = t;
43 }
44 } /* gm_check */
45
46int check_gm_max ()
47{
48 tmp = NULL;
49 pt = 0;
50 if (tmp == NULL || tmp->tm_year < 0) {
51 for (i = 63; i >= 0; i--) {
52 time_t x = pt | ((time_t)1 << i);
53 if (x < 0 || x < pt) continue;
54 gm_check (x, 69, 0x7fffffff);
55 }
56 }
57 pt_max = pt;
58 return (0);
59 } /* check_gm_max */
60
61int check_gm_min ()
62{
63 tmp = NULL;
64 pt = 0;
65 if (tmp == NULL) {
66 for (i = 36; i >= 0; i--) {
67 time_t x = pt - ((time_t)1 << i);
68 if (x > 0) continue;
69 gm_check (x, -1900, 70);
70 }
71 }
72 pt_min = pt;
73 return (0);
74 } /* check_gm_min */
75
76void lt_check (time_t t, int min_year, int max_year)
77{
78 if (sizeof (time_t) > 4 && t > 0x7ffffffffffff000LL)
79 tmp = NULL;
80 else
81 tmp = localtime (&t);
82 if ( tmp == NULL ||
83 /* Check tm_year overflow */
84 tmp->tm_year < min_year || tmp->tm_year > max_year) {
85 if (opt_v)
86 fprintf (stderr, "localtime (%ld) failed with errno %d\n", t, errno);
87 }
88 else {
89 if (opt_v)
90 fprintf (stderr, "%3d:%s: %12ld-%02d-%02d %02d:%02d:%02d\n",
91 i, hex (t),
252e731f 92 (long)(tmp->tm_year) + 1900, tmp->tm_mon + 1, tmp->tm_mday,
1b289682
MB
93 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
94 pt = t;
95 }
96 } /* lt_check */
97
98int check_lt_max ()
99{
100 tmp = NULL;
101 pt = 0;
102 if (tmp == NULL || tmp->tm_year < 0) {
103 for (i = 63; i >= 0; i--) {
104 time_t x = pt | ((time_t)1 << i);
105 if (x < 0 || x < pt) continue;
106 lt_check (x, 69, 0x7fffffff);
107 }
108 }
109 pt_max = pt;
110 return (0);
111 } /* check_lt_max */
112
113int check_lt_min ()
114{
115 tmp = NULL;
116 pt = 0;
117 if (tmp == NULL) {
118 for (i = 36; i >= 0; i--) {
119 time_t x = pt - ((time_t)1 << i);
120 if (x > 0) continue;
121 lt_check (x, -1900, 70);
122 }
123 }
124 pt_min = pt;
125 return (0);
126 } /* check_lt_min */
127
128int main (int argc, char *argv[])
129{
130 time_t gm_max, gm_min, lt_max, lt_min;
131 if (argc > 1 && strcmp (argv[1], "-v") == 0) opt_v++;
132
133 check_gm_max (); gm_max = pt_max;
134 check_gm_min (); gm_min = pt_min;
135 check_lt_max (); lt_max = pt_max;
136 check_lt_min (); lt_min = pt_min;
137
138 opt_v++;
139 printf ("======================\n");
252e731f 140 printf ("Sizeof time_t = %ld\n", (long)(i = sizeof (time_t)));
1b289682
MB
141 printf ("gmtime () boundaries:\n");
142 gm_check (gm_max, 69, 0x7fffffff);
143 gm_check (gm_min, -1900, 70);
144 printf ("localtime () boundaries:\n");
145 lt_check (lt_max, 69, 0x7fffffff);
146 lt_check (lt_min, -1900, 70);
147 printf ("Configure variables:\n");
148 printf ("sGMTIME_max='%ld'\n", gm_max);
149 printf ("sGMTIME_min='%ld'\n", gm_min);
150 printf ("sLOCALTIME_max='%ld'\n", lt_max);
151 printf ("sLOCALTIME_min='%ld'\n", lt_min);
152 return (0);
153 } /* main */