This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix .gitignore: only ignore 'perl' in root of repo, not U/perl subdir
[metaconfig.git] / U / perl / time_size.U
CommitLineData
1879bbb7
MB
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:
e2dd4a61
MB
8?MAKE:sGMTIME_max sGMTIME_min sLOCALTIME_max sLOCALTIME_min: cat rm_try \
9 Setvar Compile run i_values
1879bbb7
MB
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:.
e2dd4a61
MB
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:.
1879bbb7
MB
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:.
e2dd4a61
MB
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 /**/
1879bbb7
MB
47?H:.
48?D:sGMTIME_max=0
49?D:sGMTIME_min=0
e2dd4a61
MB
50?D:sLOCALTIME_max=0
51?D:sLOCALTIME_min=0
1879bbb7 52?F:!try
9b506102 53?T:yyy
e2dd4a61 54: Check the max offset that gmtime and localtime accept
9b506102 55echo "Checking max offsets that gmtime () accepts"
1879bbb7 56
9b506102
MB
57case $i_values in
58 define) yyy="#include <values.h>" ;;
59 *) yyy="" ;;
60 esac
61
683dcd9d
MB
62case "$sGMTIME_min/$sGMTIME_max" in
63 0/0|/)
64 $cat >try.c <<EOCP
1879bbb7
MB
65#include <sys/types.h>
66#include <stdio.h>
67#include <time.h>
9b506102
MB
68$yyy
69
70int i;
71struct tm *tmp;
72time_t pt;
1879bbb7 73
42c7afc8 74void gm_check (time_t t, int min_year, int max_year)
9b506102 75{
1879bbb7 76 tmp = gmtime (&t);
42c7afc8
MB
77 if ( tmp == NULL ||
78 /* Check tm_year overflow */
79 tmp->tm_year < min_year || tmp->tm_year > max_year)
9b506102
MB
80 tmp = NULL;
81 else
82 pt = t;
83 } /* gm_check */
1879bbb7 84
9b506102
MB
85int check_max ()
86{
e2dd4a61
MB
87 tmp = NULL;
88 pt = 0;
9b506102 89#ifdef MAXLONG
42c7afc8 90 gm_check (MAXLONG, 69, 0x7fffffff);
9b506102 91#endif
e2dd4a61
MB
92 if (tmp == NULL || tmp->tm_year < 0) {
93 for (i = 63; i >= 0; i--) {
94 time_t x = pt | ((time_t)1 << i);
42c7afc8
MB
95 if (x < 0 || x < pt) continue;
96 gm_check (x, 69, 0x7fffffff);
e2dd4a61
MB
97 }
98 }
99 printf ("sGMTIME_max=%ld\n", pt);
100 return (0);
ac97ac3d 101 } /* check_max */
1879bbb7 102
9b506102
MB
103int check_min ()
104{
e2dd4a61
MB
105 tmp = NULL;
106 pt = 0;
9b506102 107#ifdef MINLONG
42c7afc8 108 gm_check (MINLONG, -1900, 70);
9b506102 109#endif
e2dd4a61
MB
110 if (tmp == NULL) {
111 for (i = 36; i >= 0; i--) {
112 time_t x = pt - ((time_t)1 << i);
113 if (x > 0) continue;
42c7afc8 114 gm_check (x, -1900, 70);
e2dd4a61
MB
115 }
116 }
117 printf ("sGMTIME_min=%ld\n", pt);
118 return (0);
119 } /* check_min */
9b506102
MB
120
121int main (int argc, char *argv[])
122{
a58debf2 123 /* fprintf (stderr, "Sizeof time_t = %ld\n", sizeof (time_t)); */
e2dd4a61
MB
124 check_max ();
125 check_min ();
126 return (0);
127 } /* main */
1879bbb7 128EOCP
683dcd9d
MB
129 set try
130 if eval $compile; then
85f8323d 131 eval `$run ./try 2>/dev/null`
683dcd9d
MB
132 else
133 echo "Cannot determine sGMTIME_max and sGMTIME_min." >&4
134 fi
135 $rm_try
136 ;;
137 esac
1879bbb7 138
e2dd4a61
MB
139echo "Checking max offsets that localtime () accepts"
140
683dcd9d
MB
141case "$sLOCALTIME_min/$sLOCALTIME_max" in
142 0/0|/)
143 $cat >try.c <<EOCP
e2dd4a61
MB
144#include <sys/types.h>
145#include <stdio.h>
146#include <time.h>
147$yyy
148
149int i;
150struct tm *tmp;
151time_t pt;
152
42c7afc8 153void local_check (time_t t, int min_year, int max_year)
e2dd4a61 154{
20289907
MB
155 if (sizeof (time_t) > 4 && t > 0x7ffffffffffff000LL)
156 tmp = NULL;
157 else
158 tmp = localtime (&t);
42c7afc8
MB
159 if ( tmp == NULL ||
160 /* Check tm_year overflow */
161 tmp->tm_year < min_year || tmp->tm_year > max_year)
e2dd4a61
MB
162 tmp = NULL;
163 else
164 pt = t;
165 } /* local_check */
166
167int check_max ()
168{
169 tmp = NULL;
170 pt = 0;
171#ifdef MAXLONG
42c7afc8 172 local_check (MAXLONG, 69, 0x7fffffff);
e2dd4a61
MB
173#endif
174 if (tmp == NULL || tmp->tm_year < 0) {
175 for (i = 63; i >= 0; i--) {
176 time_t x = pt | ((time_t)1 << i);
42c7afc8
MB
177 if (x < 0 || x < pt) continue;
178 local_check (x, 69, 0x7fffffff);
e2dd4a61
MB
179 }
180 }
181 printf ("sLOCALTIME_max=%ld\n", pt);
182 return (0);
ac97ac3d 183 } /* check_max */
e2dd4a61
MB
184
185int check_min ()
186{
187 tmp = NULL;
188 pt = 0;
189#ifdef MINLONG
42c7afc8 190 local_check (MINLONG, -1900, 70);
e2dd4a61
MB
191#endif
192 if (tmp == NULL) {
193 for (i = 36; i >= 0; i--) {
194 time_t x = pt - ((time_t)1 << i);
195 if (x > 0) continue;
42c7afc8 196 local_check (x, -1900, 70);
e2dd4a61
MB
197 }
198 }
199 printf ("sLOCALTIME_min=%ld\n", pt);
200 return (0);
201 } /* check_min */
202
203int main (int argc, char *argv[])
204{
205 check_max ();
206 check_min ();
207 return (0);
208 } /* main */
209EOCP
683dcd9d
MB
210 set try
211 if eval $compile; then
85f8323d 212 eval `$run ./try 2>/dev/null`
683dcd9d
MB
213 else
214 echo "Cannot determine sLOCALTIME_max and sLOCALTIME_min." >&4
215 fi
216 $rm_try
217 ;;
218 esac
e2dd4a61 219