1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <sys/types.h>
32 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
33 #define LONG_MIN (-LONG_MAX - 1)
34 #define INT_MAX (~(1 << (sizeof (int) * 8 - 1)))
35 #define INT_MIN (-INT_MAX - 1)
43 /* Nonzero if YEAR is a leap year (every 4 years,
44 except every 100th isn't, and every 1000th is). */
45 #define __isleap(year) \
46 ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 1000 == 0))
49 /* How many days are in each month. */
50 static unsigned short int __mon_lengths
[2][12] =
53 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
55 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
58 #define invalid() return (time_t) -1
60 /* Return the `time_t' representation of TP and normalizes TP.
61 Return (time_t) -1 if TP is not representable as a `time_t'.
62 Note that 31 Dec 1969 23:59:59 is not representable
63 because it is represented as (time_t) -1. */
66 register struct tm
*tp
;
68 static struct tm min
, max
;
71 register time_t result
;
74 register unsigned short *l
;
75 register struct tm
*new;
87 end
= (time_t) LONG_MIN
;
92 min
.tm_sec
= min
.tm_min
= min
.tm_hour
=
93 min
.tm_mday
= min
.tm_mon
= min
.tm_year
= INT_MIN
;
95 end
= (time_t) LONG_MAX
;
100 max
.tm_sec
= max
.tm_min
= max
.tm_hour
=
101 max
.tm_mday
= max
.tm_mon
= max
.tm_year
= INT_MAX
;
104 /* Make all the elements of TP that we pay attention to
105 be within the ranges of reasonable values for those things. */
106 #define normalize(elt, min, max, nextelt) \
107 while (tp->elt < min) \
110 tp->elt += max + 1; \
112 while (tp->elt > max) \
115 tp->elt -= max + 1; \
118 normalize (tm_sec
, 0, 59, tm_min
);
119 normalize (tm_min
, 0, 59, tm_hour
);
120 normalize (tm_hour
, 0, 24, tm_mday
);
122 /* Normalize the month first so we can use
123 it to figure the range for the day. */
124 normalize (tm_mon
, 0, 11, tm_year
);
125 normalize (tm_mday
, 1, __mon_lengths
[__isleap (tp
->tm_year
)][tp
->tm_mon
],
128 /* Normalize the month again, since normalizing
129 the day may have pushed it out of range. */
130 normalize (tm_mon
, 0, 11, tm_year
);
132 /* Normalize the day again, because normalizing
133 the month may have changed the range. */
134 normalize (tm_mday
, 1, __mon_lengths
[__isleap (tp
->tm_year
)][tp
->tm_mon
],
137 /* Check for out-of-range values. */
138 #define lowhigh(field, minmax, cmp) (tp->field cmp minmax.field)
139 #define low(field) lowhigh(field, min, <)
140 #define high(field) lowhigh(field, max, >)
141 #define oor(field) (low(field) || high(field))
142 #define lowbound(field) (tp->field == min.field)
143 #define highbound(field) (tp->field == max.field)
146 else if (lowbound(tm_year
))
150 else if (lowbound(tm_mon
))
154 else if (lowbound(tm_mday
))
158 else if (lowbound(tm_hour
))
162 else if (lowbound(tm_min
))
171 else if (highbound(tm_year
))
175 else if (highbound(tm_mon
))
179 else if (highbound(tm_mday
))
183 else if (highbound(tm_hour
))
187 else if (highbound(tm_min
))
198 for (i
= 1970; i
> 1900 + tp
->tm_year
; --i
)
199 t
-= __isleap(i
) ? 366 : 365;
200 for (i
= 1970; i
< 1900 + tp
->tm_year
; ++i
)
201 t
+= __isleap(i
) ? 366 : 365;
202 l
= __mon_lengths
[__isleap(1900 + tp
->tm_year
)];
203 for (i
= 0; i
< tp
->tm_mon
; ++i
)
205 t
+= tp
->tm_mday
- 1;
206 result
= ((t
* 60 * 60 * 24) +
207 (tp
->tm_hour
* 60 * 60) +
212 #if 0 /* This code breaks it, on SunOS anyway. */
213 if (tp
->tm_isdst
< 0)
214 new = localtime(&end
);
220 new->tm_isdst
= tp
->tm_isdst
;