This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
A builtin hard upper bound for sLOCALTIME_max to prevent AIX from stalling in the...
[metaconfig.git] / U / perl / time_size.U
1 ?RCS: $Id$
2 ?RCS:
3 ?RCS: Copyright (c) 2008 H.Merijn Brand
4 ?RCS:
5 ?RCS: You may distribute under the terms of either the GNU General Public
6 ?RCS: License or the Artistic License, as specified in the README file.
7 ?RCS:
8 ?MAKE:sGMTIME_max sGMTIME_min sLOCALTIME_max sLOCALTIME_min: cat rm_try \
9         Setvar Compile run i_values
10 ?MAKE:  -pick add $@ %<
11 ?S:sGMTIME_max:
12 ?S:     This variable defines the maximum value of the time_t offset that
13 ?S:     the system function gmtime () accepts
14 ?S:.
15 ?S:sGMTIME_min:
16 ?S:     This variable defines the minimum value of the time_t offset that
17 ?S:     the system function gmtime () accepts
18 ?S:.
19 ?S:sLOCALTIME_max:
20 ?S:     This variable defines the maximum value of the time_t offset that
21 ?S:     the system function localtime () accepts
22 ?S:.
23 ?S:sLOCALTIME_min:
24 ?S:     This variable defines the minimum value of the time_t offset that
25 ?S:     the system function localtime () accepts
26 ?S:.
27 ?C:GMTIME_MAX:
28 ?C:     This symbol contains the maximum value for the time_t offset that
29 ?C:     the system function gmtime () accepts, and defaults to 0
30 ?C:.
31 ?C:GMTIME_MIN:
32 ?C:     This symbol contains the minimum value for the time_t offset that
33 ?C:     the system function gmtime () accepts, and defaults to 0
34 ?C:.
35 ?C:LOCALTIME_MAX:
36 ?C:     This symbol contains the maximum value for the time_t offset that
37 ?C:     the system function localtime () accepts, and defaults to 0
38 ?C:.
39 ?C:LOCALTIME_MIN:
40 ?C:     This symbol contains the minimum value for the time_t offset that
41 ?C:     the system function localtime () accepts, and defaults to 0
42 ?C:.
43 ?H:#define GMTIME_MAX           $sGMTIME_max    /**/
44 ?H:#define GMTIME_MIN           $sGMTIME_min    /**/
45 ?H:#define LOCALTIME_MAX        $sLOCALTIME_max /**/
46 ?H:#define LOCALTIME_MIN        $sLOCALTIME_min /**/
47 ?H:.
48 ?D:sGMTIME_max=0
49 ?D:sGMTIME_min=0
50 ?D:sLOCALTIME_max=0
51 ?D:sLOCALTIME_min=0
52 ?F:!try
53 ?T:yyy
54 : Check the max offset that gmtime and localtime accept
55 echo "Checking max offsets that gmtime () accepts"
56
57 case $i_values in
58     define) yyy="#include <values.h>" ;;
59     *)      yyy="" ;;
60     esac
61
62 case "$sGMTIME_min/$sGMTIME_max" in
63     0/0|/)
64         $cat >try.c <<EOCP
65 #include <sys/types.h>
66 #include <stdio.h>
67 #include <time.h>
68 $yyy
69
70 int i;
71 struct tm *tmp;
72 time_t pt;
73
74 void gm_check (time_t t)
75 {
76     tmp = gmtime (&t);
77     if (tmp == NULL || tmp->tm_year < -1900)
78         tmp = NULL;
79     else
80         pt = t;
81     } /* gm_check */
82
83 int check_max ()
84 {
85     tmp = NULL;
86     pt  = 0;
87 #ifdef MAXLONG
88     gm_check (MAXLONG);
89 #endif
90     if (tmp == NULL || tmp->tm_year < 0) {
91         for (i = 63; i >= 0; i--) {
92             time_t x = pt | ((time_t)1 << i);
93             if (x < 0) continue;
94             gm_check (x);
95             }
96         }
97     printf ("sGMTIME_max=%ld\n", pt);
98     return (0);
99   } /* check_max */
100
101 int check_min ()
102 {
103     tmp = NULL;
104     pt  = 0;
105 #ifdef MINLONG
106     gm_check (MINLONG);
107 #endif
108     if (tmp == NULL) {
109         for (i = 36; i >= 0; i--) {
110             time_t x = pt - ((time_t)1 << i);
111             if (x > 0) continue;
112             gm_check (x);
113             }
114         }
115     printf ("sGMTIME_min=%ld\n", pt);
116     return (0);
117     } /* check_min */
118
119 int main (int argc, char *argv[])
120 {
121     fprintf (stderr, "Sizeof time_t = %ld\n", sizeof (time_t));
122     check_max ();
123     check_min ();
124     return (0);
125     } /* main */
126 EOCP
127         set try
128         if eval $compile; then
129             eval `$run ./try`
130         else
131             echo "Cannot determine sGMTIME_max and sGMTIME_min." >&4
132             fi
133         $rm_try
134         ;;
135     esac
136
137 echo "Checking max offsets that localtime () accepts"
138
139 case "$sLOCALTIME_min/$sLOCALTIME_max" in
140     0/0|/)
141         $cat >try.c <<EOCP
142 #include <sys/types.h>
143 #include <stdio.h>
144 #include <time.h>
145 $yyy
146
147 int i;
148 struct tm *tmp;
149 time_t pt;
150
151 void local_check (time_t t)
152 {
153     if (sizeof (time_t) > 4 && t > 0x7ffffffffffff000LL)
154         tmp = NULL;
155     else
156         tmp = localtime (&t);
157     if (tmp == NULL || tmp->tm_year < -1900)
158         tmp = NULL;
159     else
160         pt = t;
161     } /* local_check */
162
163 int check_max ()
164 {
165     tmp = NULL;
166     pt  = 0;
167 #ifdef MAXLONG
168     local_check (MAXLONG);
169 #endif
170     if (tmp == NULL || tmp->tm_year < 0) {
171         for (i = 63; i >= 0; i--) {
172             time_t x = pt | ((time_t)1 << i);
173             if (x < 0) continue;
174             local_check (x);
175             }
176         }
177     printf ("sLOCALTIME_max=%ld\n", pt);
178     return (0);
179   } /* check_max */
180
181 int check_min ()
182 {
183     tmp = NULL;
184     pt  = 0;
185 #ifdef MINLONG
186     local_check (MINLONG);
187 #endif
188     if (tmp == NULL) {
189         for (i = 36; i >= 0; i--) {
190             time_t x = pt - ((time_t)1 << i);
191             if (x > 0) continue;
192             local_check (x);
193             }
194         }
195     printf ("sLOCALTIME_min=%ld\n", pt);
196     return (0);
197     } /* check_min */
198
199 int main (int argc, char *argv[])
200 {
201     check_max ();
202     check_min ();
203     return (0);
204     } /* main */
205 EOCP
206         set try
207         if eval $compile; then
208             eval `$run ./try`
209         else
210             echo "Cannot determine sLOCALTIME_max and sLOCALTIME_min." >&4
211             fi
212         $rm_try
213         ;;
214     esac
215