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
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, int min_year, int max_year)
75 {
76     tmp = gmtime (&t);
77     if ( tmp == NULL ||
78         /* Check tm_year overflow */
79          tmp->tm_year < min_year || tmp->tm_year > max_year)
80         tmp = NULL;
81     else
82         pt = t;
83     } /* gm_check */
84
85 int check_max ()
86 {
87     tmp = NULL;
88     pt  = 0;
89 #ifdef MAXLONG
90     gm_check (MAXLONG, 69, 0x7fffffff);
91 #endif
92     if (tmp == NULL || tmp->tm_year < 0) {
93         for (i = 63; i >= 0; i--) {
94             time_t x = pt | ((time_t)1 << i);
95             if (x < 0 || x < pt) continue;
96             gm_check (x, 69, 0x7fffffff);
97             }
98         }
99     printf ("sGMTIME_max=%ld\n", pt);
100     return (0);
101     } /* check_max */
102
103 int check_min ()
104 {
105     tmp = NULL;
106     pt  = 0;
107 #ifdef MINLONG
108     gm_check (MINLONG, -1900, 70);
109 #endif
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;
114             gm_check (x, -1900, 70);
115             }
116         }
117     printf ("sGMTIME_min=%ld\n", pt);
118     return (0);
119     } /* check_min */
120
121 int main (int argc, char *argv[])
122 {
123     /* fprintf (stderr, "Sizeof time_t = %ld\n", sizeof (time_t)); */
124     check_max ();
125     check_min ();
126     return (0);
127     } /* main */
128 EOCP
129         set try
130         if eval $compile; then
131             eval `$run ./try 2>/dev/null`
132         else
133             echo "Cannot determine sGMTIME_max and sGMTIME_min." >&4
134             fi
135         $rm_try
136         ;;
137     esac
138
139 echo "Checking max offsets that localtime () accepts"
140
141 case "$sLOCALTIME_min/$sLOCALTIME_max" in
142     0/0|/)
143         $cat >try.c <<EOCP
144 #include <sys/types.h>
145 #include <stdio.h>
146 #include <time.h>
147 $yyy
148
149 int i;
150 struct tm *tmp;
151 time_t pt;
152
153 void local_check (time_t t, int min_year, int max_year)
154 {
155     if (sizeof (time_t) > 4 && t > 0x7ffffffffffff000LL)
156         tmp = NULL;
157     else
158         tmp = localtime (&t);
159     if ( tmp == NULL ||
160         /* Check tm_year overflow */
161          tmp->tm_year < min_year || tmp->tm_year > max_year)
162         tmp = NULL;
163     else
164         pt = t;
165     } /* local_check */
166
167 int check_max ()
168 {
169     tmp = NULL;
170     pt  = 0;
171 #ifdef MAXLONG
172     local_check (MAXLONG, 69, 0x7fffffff);
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);
177             if (x < 0 || x < pt) continue;
178             local_check (x, 69, 0x7fffffff);
179             }
180         }
181     printf ("sLOCALTIME_max=%ld\n", pt);
182     return (0);
183    } /* check_max */
184
185 int check_min ()
186 {
187     tmp = NULL;
188     pt  = 0;
189 #ifdef MINLONG
190     local_check (MINLONG, -1900, 70);
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;
196             local_check (x, -1900, 70);
197             }
198         }
199     printf ("sLOCALTIME_min=%ld\n", pt);
200     return (0);
201     } /* check_min */
202
203 int main (int argc, char *argv[])
204 {
205     check_max ();
206     check_min ();
207     return (0);
208     } /* main */
209 EOCP
210         set try
211         if eval $compile; then
212             eval `$run ./try 2>/dev/null`
213         else
214             echo "Cannot determine sLOCALTIME_max and sLOCALTIME_min." >&4
215             fi
216         $rm_try
217         ;;
218     esac
219