OLD | NEW |
1 /* | 1 /* |
2 ** This file is in the public domain, so clarified as of | 2 ** This file is in the public domain, so clarified as of |
3 ** 1996-06-05 by Arthur David Olson. | 3 ** 1996-06-05 by Arthur David Olson. |
4 */ | 4 */ |
5 | 5 |
6 #ifndef lint | |
7 #ifndef NOID | |
8 static char elsieid[] = "@(#)localtime.c 8.9"; | |
9 #endif /* !defined NOID */ | |
10 #endif /* !defined lint */ | |
11 | |
12 /* | 6 /* |
13 ** Leap second handling from Bradley White. | 7 ** Leap second handling from Bradley White. |
14 ** POSIX-style TZ environment variable handling from Guy Harris. | 8 ** POSIX-style TZ environment variable handling from Guy Harris. |
15 */ | 9 */ |
16 | 10 |
17 /*LINTLIBRARY*/ | 11 /*LINTLIBRARY*/ |
18 | 12 |
19 #include "private.h" | 13 #include "private.h" |
20 #include "tzfile.h" | 14 #include "tzfile.h" |
21 #include "fcntl.h" | 15 #include "fcntl.h" |
22 #include "float.h" /* for FLT_MAX and DBL_MAX */ | |
23 | 16 |
24 #ifndef TZ_ABBR_MAX_LEN | 17 #ifndef TZ_ABBR_MAX_LEN |
25 #define TZ_ABBR_MAX_LEN 16 | 18 #define TZ_ABBR_MAX_LEN 16 |
26 #endif /* !defined TZ_ABBR_MAX_LEN */ | 19 #endif /* !defined TZ_ABBR_MAX_LEN */ |
27 | 20 |
28 #ifndef TZ_ABBR_CHAR_SET | 21 #ifndef TZ_ABBR_CHAR_SET |
29 #define TZ_ABBR_CHAR_SET \ | 22 #define TZ_ABBR_CHAR_SET \ |
30 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._" | 23 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._" |
31 #endif /* !defined TZ_ABBR_CHAR_SET */ | 24 #endif /* !defined TZ_ABBR_CHAR_SET */ |
32 | 25 |
(...skipping 28 matching lines...) Expand all Loading... |
61 ** for now, we just set things up so that in any of the five cases | 54 ** for now, we just set things up so that in any of the five cases |
62 ** WILDABBR is used. Another possibility: initialize tzname[0] to the | 55 ** WILDABBR is used. Another possibility: initialize tzname[0] to the |
63 ** string "tzname[0] used before set", and similarly for the other cases. | 56 ** string "tzname[0] used before set", and similarly for the other cases. |
64 ** And another: initialize tzname[0] to "ERA", with an explanation in the | 57 ** And another: initialize tzname[0] to "ERA", with an explanation in the |
65 ** manual page of what this "time zone abbreviation" means (doing this so | 58 ** manual page of what this "time zone abbreviation" means (doing this so |
66 ** that tzname[0] has the "normal" length of three characters). | 59 ** that tzname[0] has the "normal" length of three characters). |
67 */ | 60 */ |
68 #define WILDABBR " " | 61 #define WILDABBR " " |
69 #endif /* !defined WILDABBR */ | 62 #endif /* !defined WILDABBR */ |
70 | 63 |
71 static char» » wildabbr[] = WILDABBR; | 64 static const char» wildabbr[] = WILDABBR; |
72 | 65 |
73 static const char gmt[] = "GMT"; | 66 static const char gmt[] = "GMT"; |
74 | 67 |
75 /* | 68 /* |
76 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES. | 69 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES. |
77 ** We default to US rules as of 1999-08-17. | 70 ** We default to US rules as of 1999-08-17. |
78 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are | 71 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are |
79 ** implementation dependent; for historical reasons, US rules are a | 72 ** implementation dependent; for historical reasons, US rules are a |
80 ** common default. | 73 ** common default. |
81 */ | 74 */ |
82 #ifndef TZDEFRULESTRING | 75 #ifndef TZDEFRULESTRING |
83 #define TZDEFRULESTRING ",M4.1.0,M10.5.0" | 76 #define TZDEFRULESTRING ",M4.1.0,M10.5.0" |
84 #endif /* !defined TZDEFDST */ | 77 #endif /* !defined TZDEFDST */ |
85 | 78 |
86 struct ttinfo { /* time type information */ | 79 struct ttinfo { /* time type information */ |
87 » long» » tt_gmtoff;» /* UTC offset in seconds */ | 80 » int_fast32_t» tt_gmtoff;» /* UT offset in seconds */ |
88 int tt_isdst; /* used to set tm_isdst */ | 81 int tt_isdst; /* used to set tm_isdst */ |
89 int tt_abbrind; /* abbreviation list index */ | 82 int tt_abbrind; /* abbreviation list index */ |
90 int tt_ttisstd; /* TRUE if transition is std time */ | 83 int tt_ttisstd; /* TRUE if transition is std time */ |
91 » int» » tt_ttisgmt;» /* TRUE if transition is UTC */ | 84 » int» » tt_ttisgmt;» /* TRUE if transition is UT */ |
92 }; | 85 }; |
93 | 86 |
94 struct lsinfo { /* leap second information */ | 87 struct lsinfo { /* leap second information */ |
95 time_t ls_trans; /* transition time */ | 88 time_t ls_trans; /* transition time */ |
96 » long» » ls_corr;» /* correction to apply */ | 89 » int_fast64_t» ls_corr;» /* correction to apply */ |
97 }; | 90 }; |
98 | 91 |
99 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) | 92 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b)) |
100 | 93 |
101 #ifdef TZNAME_MAX | 94 #ifdef TZNAME_MAX |
102 #define MY_TZNAME_MAX TZNAME_MAX | 95 #define MY_TZNAME_MAX TZNAME_MAX |
103 #endif /* defined TZNAME_MAX */ | 96 #endif /* defined TZNAME_MAX */ |
104 #ifndef TZNAME_MAX | 97 #ifndef TZNAME_MAX |
105 #define MY_TZNAME_MAX 255 | 98 #define MY_TZNAME_MAX 255 |
106 #endif /* !defined TZNAME_MAX */ | 99 #endif /* !defined TZNAME_MAX */ |
107 | 100 |
108 struct state { | 101 struct state { |
109 int leapcnt; | 102 int leapcnt; |
110 int timecnt; | 103 int timecnt; |
111 int typecnt; | 104 int typecnt; |
112 int charcnt; | 105 int charcnt; |
113 int goback; | 106 int goback; |
114 int goahead; | 107 int goahead; |
115 time_t ats[TZ_MAX_TIMES]; | 108 time_t ats[TZ_MAX_TIMES]; |
116 unsigned char types[TZ_MAX_TIMES]; | 109 unsigned char types[TZ_MAX_TIMES]; |
117 struct ttinfo ttis[TZ_MAX_TYPES]; | 110 struct ttinfo ttis[TZ_MAX_TYPES]; |
118 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), | 111 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt), |
119 (2 * (MY_TZNAME_MAX + 1)))]; | 112 (2 * (MY_TZNAME_MAX + 1)))]; |
120 struct lsinfo lsis[TZ_MAX_LEAPS]; | 113 struct lsinfo lsis[TZ_MAX_LEAPS]; |
| 114 int defaulttype; /* for early times or if no transitions */ |
121 }; | 115 }; |
122 | 116 |
123 struct rule { | 117 struct rule { |
124 int r_type; /* type of rule--see below */ | 118 int r_type; /* type of rule--see below */ |
125 int r_day; /* day number of rule */ | 119 int r_day; /* day number of rule */ |
126 int r_week; /* week number of rule */ | 120 int r_week; /* week number of rule */ |
127 int r_mon; /* month number of rule */ | 121 int r_mon; /* month number of rule */ |
128 » long» » r_time;»» /* transition time of rule */ | 122 » int_fast32_t» r_time;»» /* transition time of rule */ |
129 }; | 123 }; |
130 | 124 |
131 #define JULIAN_DAY 0 /* Jn - Julian day */ | 125 #define JULIAN_DAY 0 /* Jn - Julian day */ |
132 #define DAY_OF_YEAR 1 /* n - day of year */ | 126 #define DAY_OF_YEAR 1 /* n - day of year */ |
133 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ | 127 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */ |
134 | 128 |
135 /* | 129 /* |
136 ** Prototypes for static functions. | 130 ** Prototypes for static functions. |
137 */ | 131 */ |
138 | 132 |
139 static long» » detzcode(const char * codep); | 133 static int_fast32_t» detzcode(const char * codep); |
140 static time_t» » detzcode64(const char * codep); | 134 static int_fast64_t» detzcode64(const char * codep); |
141 static int differ_by_repeat(time_t t1, time_t t0); | 135 static int differ_by_repeat(time_t t1, time_t t0); |
142 static const char *» getzname(const char * strp); | 136 static const char *» getzname(const char * strp) ATTRIBUTE_PURE; |
143 static const char *» getqzname(const char * strp, const int delim); | 137 static const char *» getqzname(const char * strp, const int delim) |
| 138 ATTRIBUTE_PURE; |
144 static const char * getnum(const char * strp, int * nump, int min, | 139 static const char * getnum(const char * strp, int * nump, int min, |
145 int max); | 140 int max); |
146 static const char *» getsecs(const char * strp, long * secsp); | 141 static const char *» getsecs(const char * strp, int_fast32_t * secsp); |
147 static const char *» getoffset(const char * strp, long * offsetp); | 142 static const char *» getoffset(const char * strp, int_fast32_t * offsetp); |
148 static const char * getrule(const char * strp, struct rule * rulep); | 143 static const char * getrule(const char * strp, struct rule * rulep); |
149 static void gmtload(struct state * sp); | 144 static void gmtload(struct state * sp); |
150 static struct tm *» gmtsub(const time_t * timep, long offset, | 145 static struct tm *» gmtsub(const time_t * timep, int_fast32_t offset, |
151 struct tm * tmp); | 146 struct tm * tmp); |
152 static struct tm *» localsub(const time_t * timep, long offset, | 147 static struct tm *» localsub(const time_t * timep, int_fast32_t offset, |
153 struct tm * tmp); | 148 struct tm * tmp); |
154 static int increment_overflow(int * number, int delta); | 149 static int increment_overflow(int * number, int delta); |
155 static int» » leaps_thru_end_of(int y); | 150 static int» » leaps_thru_end_of(int y) ATTRIBUTE_PURE; |
156 static int» » long_increment_overflow(long * number, int delta); | 151 static int» » increment_overflow32(int_fast32_t * number, int delta); |
157 static int» » long_normalize_overflow(long * tensptr, | 152 static int» » increment_overflow_time(time_t *t, int_fast32_t delta); |
| 153 static int» » normalize_overflow32(int_fast32_t * tensptr, |
158 int * unitsptr, int base); | 154 int * unitsptr, int base); |
159 static int normalize_overflow(int * tensptr, int * unitsptr, | 155 static int normalize_overflow(int * tensptr, int * unitsptr, |
160 int base); | 156 int base); |
161 static void settzname(void); | 157 static void settzname(void); |
162 static time_t time1(struct tm * tmp, | 158 static time_t time1(struct tm * tmp, |
163 struct tm * (*funcp)(const time_t *, | 159 struct tm * (*funcp)(const time_t *, |
164 » » » » long, struct tm *), | 160 » » » » int_fast32_t, struct tm *), |
165 » » » » long offset); | 161 » » » » int_fast32_t offset); |
166 static time_t time2(struct tm *tmp, | 162 static time_t time2(struct tm *tmp, |
167 struct tm * (*funcp)(const time_t *, | 163 struct tm * (*funcp)(const time_t *, |
168 » » » » long, struct tm*), | 164 » » » » int_fast32_t, struct tm*), |
169 » » » » long offset, int * okayp); | 165 » » » » int_fast32_t offset, int * okayp); |
170 static time_t time2sub(struct tm *tmp, | 166 static time_t time2sub(struct tm *tmp, |
171 struct tm * (*funcp)(const time_t *, | 167 struct tm * (*funcp)(const time_t *, |
172 » » » » long, struct tm*), | 168 » » » » int_fast32_t, struct tm*), |
173 » » » » long offset, int * okayp, int do_norm_secs); | 169 » » » » int_fast32_t offset, int * okayp, int do_norm_se
cs); |
174 static struct tm *» timesub(const time_t * timep, long offset, | 170 static struct tm *» timesub(const time_t * timep, int_fast32_t offset, |
175 const struct state * sp, struct tm * tmp); | 171 const struct state * sp, struct tm * tmp); |
176 static int tmcomp(const struct tm * atmp, | 172 static int tmcomp(const struct tm * atmp, |
177 const struct tm * btmp); | 173 const struct tm * btmp); |
178 static time_t» » transtime(time_t janfirst, int year, | 174 static int_fast32_t» transtime(int year, const struct rule * rulep, |
179 » » » » const struct rule * rulep, long offset); | 175 » » » » int_fast32_t offset) |
| 176 ATTRIBUTE_PURE; |
180 static int typesequiv(const struct state * sp, int a, int b); | 177 static int typesequiv(const struct state * sp, int a, int b); |
181 static int tzload(const char * name, struct state * sp, | 178 static int tzload(const char * name, struct state * sp, |
182 int doextend); | 179 int doextend); |
183 static int tzparse(const char * name, struct state * sp, | 180 static int tzparse(const char * name, struct state * sp, |
184 int lastditch); | 181 int lastditch); |
185 | 182 |
186 #ifdef ALL_STATE | 183 #ifdef ALL_STATE |
187 static struct state * lclptr; | 184 static struct state * lclptr; |
188 static struct state * gmtptr; | 185 static struct state * gmtptr; |
189 #endif /* defined ALL_STATE */ | 186 #endif /* defined ALL_STATE */ |
190 | 187 |
191 #ifndef ALL_STATE | 188 #ifndef ALL_STATE |
192 static struct state lclmem; | 189 static struct state lclmem; |
193 static struct state gmtmem; | 190 static struct state gmtmem; |
194 #define lclptr (&lclmem) | 191 #define lclptr (&lclmem) |
195 #define gmtptr (&gmtmem) | 192 #define gmtptr (&gmtmem) |
196 #endif /* State Farm */ | 193 #endif /* State Farm */ |
197 | 194 |
198 #ifndef TZ_STRLEN_MAX | 195 #ifndef TZ_STRLEN_MAX |
199 #define TZ_STRLEN_MAX 255 | 196 #define TZ_STRLEN_MAX 255 |
200 #endif /* !defined TZ_STRLEN_MAX */ | 197 #endif /* !defined TZ_STRLEN_MAX */ |
201 | 198 |
202 static char lcl_TZname[TZ_STRLEN_MAX + 1]; | 199 static char lcl_TZname[TZ_STRLEN_MAX + 1]; |
203 static int lcl_is_set; | 200 static int lcl_is_set; |
204 static int gmt_is_set; | 201 static int gmt_is_set; |
205 | 202 |
206 char * tzname[2] = { | 203 char * tzname[2] = { |
207 » wildabbr, | 204 » (char *) wildabbr, |
208 » wildabbr | 205 » (char *) wildabbr |
209 }; | 206 }; |
210 | 207 |
211 /* | 208 /* |
212 ** Section 4.12.3 of X3.159-1989 requires that | 209 ** Section 4.12.3 of X3.159-1989 requires that |
213 ** Except for the strftime function, these functions [asctime, | 210 ** Except for the strftime function, these functions [asctime, |
214 ** ctime, gmtime, localtime] return values in one of two static | 211 ** ctime, gmtime, localtime] return values in one of two static |
215 ** objects: a broken-down time structure and an array of char. | 212 ** objects: a broken-down time structure and an array of char. |
216 ** Thanks to Paul Eggert for noting this. | 213 ** Thanks to Paul Eggert for noting this. |
217 */ | 214 */ |
218 | 215 |
219 static struct tm tm; | 216 static struct tm tm; |
220 | 217 |
221 #ifdef USG_COMPAT | 218 #ifdef USG_COMPAT |
222 time_t» » » timezone = 0; | 219 long» » » timezone = 0; |
223 int daylight = 0; | 220 int daylight = 0; |
224 #endif /* defined USG_COMPAT */ | 221 #endif /* defined USG_COMPAT */ |
225 | 222 |
226 #ifdef ALTZONE | 223 #ifdef ALTZONE |
227 time_t» » » altzone = 0; | 224 long» » » altzone = 0; |
228 #endif /* defined ALTZONE */ | 225 #endif /* defined ALTZONE */ |
229 | 226 |
230 static long | 227 static int_fast32_t |
231 detzcode(codep) | 228 detzcode(const char *const codep) |
232 const char * const» codep; | |
233 { | 229 { |
234 » register long» result; | 230 » register int_fast32_t» result; |
235 » register int» i; | 231 » register int» » i; |
236 | 232 |
237 » result = (codep[0] & 0x80) ? ~0L : 0; | 233 » result = (codep[0] & 0x80) ? -1 : 0; |
238 for (i = 0; i < 4; ++i) | 234 for (i = 0; i < 4; ++i) |
239 result = (result << 8) | (codep[i] & 0xff); | 235 result = (result << 8) | (codep[i] & 0xff); |
240 return result; | 236 return result; |
241 } | 237 } |
242 | 238 |
243 static time_t | 239 static int_fast64_t |
244 detzcode64(codep) | 240 detzcode64(const char *const codep) |
245 const char * const» codep; | |
246 { | 241 { |
247 » register time_t»result; | 242 » register int_fast64_t result; |
248 register int i; | 243 register int i; |
249 | 244 |
250 » result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0; | 245 » result = (codep[0] & 0x80) ? -1 : 0; |
251 for (i = 0; i < 8; ++i) | 246 for (i = 0; i < 8; ++i) |
252 » » result = result * 256 + (codep[i] & 0xff); | 247 » » result = (result << 8) | (codep[i] & 0xff); |
253 return result; | 248 return result; |
254 } | 249 } |
255 | 250 |
256 static void | 251 static void |
257 settzname(void) | 252 settzname(void) |
258 { | 253 { |
259 register struct state * const sp = lclptr; | 254 register struct state * const sp = lclptr; |
260 register int i; | 255 register int i; |
261 | 256 |
262 » tzname[0] = wildabbr; | 257 » tzname[0] = tzname[1] = (char *) wildabbr; |
263 » tzname[1] = wildabbr; | |
264 #ifdef USG_COMPAT | 258 #ifdef USG_COMPAT |
265 daylight = 0; | 259 daylight = 0; |
266 timezone = 0; | 260 timezone = 0; |
267 #endif /* defined USG_COMPAT */ | 261 #endif /* defined USG_COMPAT */ |
268 #ifdef ALTZONE | 262 #ifdef ALTZONE |
269 altzone = 0; | 263 altzone = 0; |
270 #endif /* defined ALTZONE */ | 264 #endif /* defined ALTZONE */ |
271 #ifdef ALL_STATE | |
272 if (sp == NULL) { | 265 if (sp == NULL) { |
273 » » tzname[0] = tzname[1] = gmt; | 266 » » tzname[0] = tzname[1] = (char *) gmt; |
274 return; | 267 return; |
275 } | 268 } |
276 #endif /* defined ALL_STATE */ | |
277 for (i = 0; i < sp->typecnt; ++i) { | |
278 register const struct ttinfo * const ttisp = &sp->ttis[i]; | |
279 | |
280 tzname[ttisp->tt_isdst] = | |
281 &sp->chars[ttisp->tt_abbrind]; | |
282 #ifdef USG_COMPAT | |
283 if (ttisp->tt_isdst) | |
284 daylight = 1; | |
285 if (i == 0 || !ttisp->tt_isdst) | |
286 timezone = -(ttisp->tt_gmtoff); | |
287 #endif /* defined USG_COMPAT */ | |
288 #ifdef ALTZONE | |
289 if (i == 0 || ttisp->tt_isdst) | |
290 altzone = -(ttisp->tt_gmtoff); | |
291 #endif /* defined ALTZONE */ | |
292 } | |
293 /* | 269 /* |
294 ** And to get the latest zone names into tzname. . . | 270 ** And to get the latest zone names into tzname. . . |
295 */ | 271 */ |
| 272 for (i = 0; i < sp->typecnt; ++i) { |
| 273 register const struct ttinfo * const ttisp = &sp->ttis[i]; |
| 274 |
| 275 tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind]; |
| 276 } |
296 for (i = 0; i < sp->timecnt; ++i) { | 277 for (i = 0; i < sp->timecnt; ++i) { |
297 register const struct ttinfo * const ttisp = | 278 register const struct ttinfo * const ttisp = |
298 &sp->ttis[ | 279 &sp->ttis[ |
299 sp->types[i]]; | 280 sp->types[i]]; |
300 | 281 |
301 tzname[ttisp->tt_isdst] = | 282 tzname[ttisp->tt_isdst] = |
302 &sp->chars[ttisp->tt_abbrind]; | 283 &sp->chars[ttisp->tt_abbrind]; |
| 284 #ifdef USG_COMPAT |
| 285 if (ttisp->tt_isdst) |
| 286 daylight = 1; |
| 287 if (!ttisp->tt_isdst) |
| 288 timezone = -(ttisp->tt_gmtoff); |
| 289 #endif /* defined USG_COMPAT */ |
| 290 #ifdef ALTZONE |
| 291 if (ttisp->tt_isdst) |
| 292 altzone = -(ttisp->tt_gmtoff); |
| 293 #endif /* defined ALTZONE */ |
303 } | 294 } |
304 /* | 295 /* |
305 ** Finally, scrub the abbreviations. | 296 ** Finally, scrub the abbreviations. |
306 ** First, replace bogus characters. | 297 ** First, replace bogus characters. |
307 */ | 298 */ |
308 for (i = 0; i < sp->charcnt; ++i) | 299 for (i = 0; i < sp->charcnt; ++i) |
309 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL) | 300 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL) |
310 sp->chars[i] = TZ_ABBR_ERR_CHAR; | 301 sp->chars[i] = TZ_ABBR_ERR_CHAR; |
311 /* | 302 /* |
312 ** Second, truncate long abbreviations. | 303 ** Second, truncate long abbreviations. |
313 */ | 304 */ |
314 for (i = 0; i < sp->typecnt; ++i) { | 305 for (i = 0; i < sp->typecnt; ++i) { |
315 register const struct ttinfo * const ttisp = &sp->ttis[i]; | 306 register const struct ttinfo * const ttisp = &sp->ttis[i]; |
316 register char * cp = &sp->chars[ttisp->t
t_abbrind]; | 307 register char * cp = &sp->chars[ttisp->t
t_abbrind]; |
317 | 308 |
318 if (strlen(cp) > TZ_ABBR_MAX_LEN && | 309 if (strlen(cp) > TZ_ABBR_MAX_LEN && |
319 strcmp(cp, GRANDPARENTED) != 0) | 310 strcmp(cp, GRANDPARENTED) != 0) |
320 *(cp + TZ_ABBR_MAX_LEN) = '\0'; | 311 *(cp + TZ_ABBR_MAX_LEN) = '\0'; |
321 } | 312 } |
322 } | 313 } |
323 | 314 |
324 static int | 315 static int |
325 differ_by_repeat(t1, t0) | 316 differ_by_repeat(const time_t t1, const time_t t0) |
326 const time_t» t1; | |
327 const time_t» t0; | |
328 { | 317 { |
329 » if (TYPE_INTEGRAL(time_t) && | 318 » if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) |
330 » » TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS) | 319 » » return 0; |
331 » » » return 0; | |
332 return t1 - t0 == SECSPERREPEAT; | 320 return t1 - t0 == SECSPERREPEAT; |
333 } | 321 } |
334 | 322 |
335 static int | 323 static int |
336 tzload(name, sp, doextend) | 324 tzload(register const char *name, register struct state *const sp, |
337 register const char *» » name; | 325 register const int doextend) |
338 register struct state * const» sp; | |
339 register const int» » doextend; | |
340 { | 326 { |
341 register const char * p; | 327 register const char * p; |
342 register int i; | 328 register int i; |
343 register int fid; | 329 register int fid; |
344 register int stored; | 330 register int stored; |
345 register int nread; | 331 register int nread; |
346 » union { | 332 » typedef union { |
347 struct tzhead tzhead; | 333 struct tzhead tzhead; |
348 char buf[2 * sizeof(struct tzhead) + | 334 char buf[2 * sizeof(struct tzhead) + |
349 2 * sizeof *sp + | 335 2 * sizeof *sp + |
350 4 * TZ_MAX_TIMES]; | 336 4 * TZ_MAX_TIMES]; |
351 » } u; | 337 » } u_t; |
| 338 #ifdef ALL_STATE |
| 339 » register u_t * const» » up = malloc(sizeof *up); |
| 340 #else /* !defined ALL_STATE */ |
| 341 » u_t» » » » u; |
| 342 » register u_t * const» » up = &u; |
| 343 #endif /* !defined ALL_STATE */ |
| 344 |
| 345 » sp->goback = sp->goahead = FALSE; |
| 346 |
| 347 » if (up == NULL) |
| 348 » » return -1; |
352 | 349 |
353 if (name == NULL && (name = TZDEFAULT) == NULL) | 350 if (name == NULL && (name = TZDEFAULT) == NULL) |
354 » » return -1; | 351 » » goto oops; |
355 { | 352 { |
356 register int doaccess; | 353 register int doaccess; |
357 /* | 354 /* |
358 ** Section 4.9.1 of the C standard says that | 355 ** Section 4.9.1 of the C standard says that |
359 ** "FILENAME_MAX expands to an integral constant expression | 356 ** "FILENAME_MAX expands to an integral constant expression |
360 ** that is the size needed for an array of char large enough | 357 ** that is the size needed for an array of char large enough |
361 ** to hold the longest file name string that the implementation | 358 ** to hold the longest file name string that the implementation |
362 ** guarantees can be opened." | 359 ** guarantees can be opened." |
363 */ | 360 */ |
364 char fullname[FILENAME_MAX + 1]; | 361 char fullname[FILENAME_MAX + 1]; |
365 | 362 |
366 if (name[0] == ':') | 363 if (name[0] == ':') |
367 ++name; | 364 ++name; |
368 doaccess = name[0] == '/'; | 365 doaccess = name[0] == '/'; |
369 if (!doaccess) { | 366 if (!doaccess) { |
370 if ((p = TZDIR) == NULL) | 367 if ((p = TZDIR) == NULL) |
371 » » » » return -1; | 368 » » » » goto oops; |
372 if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) | 369 if ((strlen(p) + strlen(name) + 1) >= sizeof fullname) |
373 » » » » return -1; | 370 » » » » goto oops; |
374 (void) strcpy(fullname, p); | 371 (void) strcpy(fullname, p); |
375 (void) strcat(fullname, "/"); | 372 (void) strcat(fullname, "/"); |
376 (void) strcat(fullname, name); | 373 (void) strcat(fullname, name); |
377 /* | 374 /* |
378 ** Set doaccess if '.' (as in "../") shows up in name. | 375 ** Set doaccess if '.' (as in "../") shows up in name. |
379 */ | 376 */ |
380 if (strchr(name, '.') != NULL) | 377 if (strchr(name, '.') != NULL) |
381 doaccess = TRUE; | 378 doaccess = TRUE; |
382 name = fullname; | 379 name = fullname; |
383 } | 380 } |
384 if (doaccess && access(name, R_OK) != 0) | 381 if (doaccess && access(name, R_OK) != 0) |
385 » » » return -1; | 382 » » » goto oops; |
386 if ((fid = open(name, OPEN_MODE)) == -1) | 383 if ((fid = open(name, OPEN_MODE)) == -1) |
387 » » » return -1; | 384 » » » goto oops; |
388 } | 385 } |
389 » nread = read(fid, u.buf, sizeof u.buf); | 386 » nread = read(fid, up->buf, sizeof up->buf); |
390 if (close(fid) < 0 || nread <= 0) | 387 if (close(fid) < 0 || nread <= 0) |
391 » » return -1; | 388 » » goto oops; |
392 for (stored = 4; stored <= 8; stored *= 2) { | 389 for (stored = 4; stored <= 8; stored *= 2) { |
393 int ttisstdcnt; | 390 int ttisstdcnt; |
394 int ttisgmtcnt; | 391 int ttisgmtcnt; |
| 392 int timecnt; |
395 | 393 |
396 » » ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt); | 394 » » ttisstdcnt = (int) detzcode(up->tzhead.tzh_ttisstdcnt); |
397 » » ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt); | 395 » » ttisgmtcnt = (int) detzcode(up->tzhead.tzh_ttisgmtcnt); |
398 » » sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt); | 396 » » sp->leapcnt = (int) detzcode(up->tzhead.tzh_leapcnt); |
399 » » sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt); | 397 » » sp->timecnt = (int) detzcode(up->tzhead.tzh_timecnt); |
400 » » sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt); | 398 » » sp->typecnt = (int) detzcode(up->tzhead.tzh_typecnt); |
401 » » sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt); | 399 » » sp->charcnt = (int) detzcode(up->tzhead.tzh_charcnt); |
402 » » p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt; | 400 » » p = up->tzhead.tzh_charcnt + sizeof up->tzhead.tzh_charcnt; |
403 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || | 401 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || |
404 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || | 402 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || |
405 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || | 403 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || |
406 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || | 404 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || |
407 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || | 405 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || |
408 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) | 406 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) |
409 » » » » return -1; | 407 » » » » goto oops; |
410 » » if (nread - (p - u.buf) < | 408 » » if (nread - (p - up->buf) < |
411 sp->timecnt * stored + /* ats */ | 409 sp->timecnt * stored + /* ats */ |
412 sp->timecnt + /* types */ | 410 sp->timecnt + /* types */ |
413 sp->typecnt * 6 + /* ttinfos */ | 411 sp->typecnt * 6 + /* ttinfos */ |
414 sp->charcnt + /* chars */ | 412 sp->charcnt + /* chars */ |
415 sp->leapcnt * (stored + 4) + /* lsinfos */ | 413 sp->leapcnt * (stored + 4) + /* lsinfos */ |
416 ttisstdcnt + /* ttisstds */ | 414 ttisstdcnt + /* ttisstds */ |
417 ttisgmtcnt) /* ttisgmts */ | 415 ttisgmtcnt) /* ttisgmts */ |
418 » » » » return -1; | 416 » » » » goto oops; |
| 417 » » timecnt = 0; |
419 for (i = 0; i < sp->timecnt; ++i) { | 418 for (i = 0; i < sp->timecnt; ++i) { |
420 » » » sp->ats[i] = (stored == 4) ? | 419 » » » int_fast64_t at |
421 » » » » detzcode(p) : detzcode64(p); | 420 » » » = stored == 4 ? detzcode(p) : detzcode64(p); |
| 421 » » » sp->types[i] = ((TYPE_SIGNED(time_t) |
| 422 » » » » » ? time_t_min <= at |
| 423 » » » » » : 0 <= at) |
| 424 » » » » » && at <= time_t_max); |
| 425 » » » if (sp->types[i]) { |
| 426 » » » » if (i && !timecnt && at != time_t_min) { |
| 427 » » » » » /* |
| 428 » » » » » ** Keep the earlier record, but tweak |
| 429 » » » » » ** it so that it starts with the |
| 430 » » » » » ** minimum time_t value. |
| 431 » » » » » */ |
| 432 » » » » » sp->types[i - 1] = 1; |
| 433 » » » » » sp->ats[timecnt++] = time_t_min; |
| 434 » » » » } |
| 435 » » » » sp->ats[timecnt++] = at; |
| 436 » » » } |
422 p += stored; | 437 p += stored; |
423 } | 438 } |
| 439 timecnt = 0; |
424 for (i = 0; i < sp->timecnt; ++i) { | 440 for (i = 0; i < sp->timecnt; ++i) { |
425 » » » sp->types[i] = (unsigned char) *p++; | 441 » » » unsigned char typ = *p++; |
426 » » » if (sp->types[i] >= sp->typecnt) | 442 » » » if (sp->typecnt <= typ) |
427 » » » » return -1; | 443 » » » » goto oops; |
| 444 » » » if (sp->types[i]) |
| 445 » » » » sp->types[timecnt++] = typ; |
428 } | 446 } |
| 447 sp->timecnt = timecnt; |
429 for (i = 0; i < sp->typecnt; ++i) { | 448 for (i = 0; i < sp->typecnt; ++i) { |
430 register struct ttinfo * ttisp; | 449 register struct ttinfo * ttisp; |
431 | 450 |
432 ttisp = &sp->ttis[i]; | 451 ttisp = &sp->ttis[i]; |
433 ttisp->tt_gmtoff = detzcode(p); | 452 ttisp->tt_gmtoff = detzcode(p); |
434 p += 4; | 453 p += 4; |
435 ttisp->tt_isdst = (unsigned char) *p++; | 454 ttisp->tt_isdst = (unsigned char) *p++; |
436 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) | 455 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) |
437 » » » » return -1; | 456 » » » » goto oops; |
438 ttisp->tt_abbrind = (unsigned char) *p++; | 457 ttisp->tt_abbrind = (unsigned char) *p++; |
439 if (ttisp->tt_abbrind < 0 || | 458 if (ttisp->tt_abbrind < 0 || |
440 ttisp->tt_abbrind > sp->charcnt) | 459 ttisp->tt_abbrind > sp->charcnt) |
441 » » » » » return -1; | 460 » » » » » goto oops; |
442 } | 461 } |
443 for (i = 0; i < sp->charcnt; ++i) | 462 for (i = 0; i < sp->charcnt; ++i) |
444 sp->chars[i] = *p++; | 463 sp->chars[i] = *p++; |
445 sp->chars[i] = '\0'; /* ensure '\0' at end */ | 464 sp->chars[i] = '\0'; /* ensure '\0' at end */ |
446 for (i = 0; i < sp->leapcnt; ++i) { | 465 for (i = 0; i < sp->leapcnt; ++i) { |
447 register struct lsinfo * lsisp; | 466 register struct lsinfo * lsisp; |
448 | 467 |
449 lsisp = &sp->lsis[i]; | 468 lsisp = &sp->lsis[i]; |
450 lsisp->ls_trans = (stored == 4) ? | 469 lsisp->ls_trans = (stored == 4) ? |
451 detzcode(p) : detzcode64(p); | 470 detzcode(p) : detzcode64(p); |
452 p += stored; | 471 p += stored; |
453 lsisp->ls_corr = detzcode(p); | 472 lsisp->ls_corr = detzcode(p); |
454 p += 4; | 473 p += 4; |
455 } | 474 } |
456 for (i = 0; i < sp->typecnt; ++i) { | 475 for (i = 0; i < sp->typecnt; ++i) { |
457 register struct ttinfo * ttisp; | 476 register struct ttinfo * ttisp; |
458 | 477 |
459 ttisp = &sp->ttis[i]; | 478 ttisp = &sp->ttis[i]; |
460 if (ttisstdcnt == 0) | 479 if (ttisstdcnt == 0) |
461 ttisp->tt_ttisstd = FALSE; | 480 ttisp->tt_ttisstd = FALSE; |
462 else { | 481 else { |
463 ttisp->tt_ttisstd = *p++; | 482 ttisp->tt_ttisstd = *p++; |
464 if (ttisp->tt_ttisstd != TRUE && | 483 if (ttisp->tt_ttisstd != TRUE && |
465 ttisp->tt_ttisstd != FALSE) | 484 ttisp->tt_ttisstd != FALSE) |
466 » » » » » » return -1; | 485 » » » » » » goto oops; |
467 } | 486 } |
468 } | 487 } |
469 for (i = 0; i < sp->typecnt; ++i) { | 488 for (i = 0; i < sp->typecnt; ++i) { |
470 register struct ttinfo * ttisp; | 489 register struct ttinfo * ttisp; |
471 | 490 |
472 ttisp = &sp->ttis[i]; | 491 ttisp = &sp->ttis[i]; |
473 if (ttisgmtcnt == 0) | 492 if (ttisgmtcnt == 0) |
474 ttisp->tt_ttisgmt = FALSE; | 493 ttisp->tt_ttisgmt = FALSE; |
475 else { | 494 else { |
476 ttisp->tt_ttisgmt = *p++; | 495 ttisp->tt_ttisgmt = *p++; |
477 if (ttisp->tt_ttisgmt != TRUE && | 496 if (ttisp->tt_ttisgmt != TRUE && |
478 ttisp->tt_ttisgmt != FALSE) | 497 ttisp->tt_ttisgmt != FALSE) |
479 » » » » » » return -1; | 498 » » » » » » goto oops; |
480 } | 499 } |
481 } | 500 } |
482 /* | 501 /* |
483 ** Out-of-sort ats should mean we're running on a | |
484 ** signed time_t system but using a data file with | |
485 ** unsigned values (or vice versa). | |
486 */ | |
487 for (i = 0; i < sp->timecnt - 2; ++i) | |
488 if (sp->ats[i] > sp->ats[i + 1]) { | |
489 ++i; | |
490 if (TYPE_SIGNED(time_t)) { | |
491 /* | |
492 ** Ignore the end (easy). | |
493 */ | |
494 sp->timecnt = i; | |
495 } else { | |
496 /* | |
497 ** Ignore the beginning (harder). | |
498 */ | |
499 register int j; | |
500 | |
501 for (j = 0; j + i < sp->timecnt; ++j) { | |
502 sp->ats[j] = sp->ats[j + i]; | |
503 sp->types[j] = sp->types[j + i]; | |
504 } | |
505 sp->timecnt = j; | |
506 } | |
507 break; | |
508 } | |
509 /* | |
510 ** If this is an old file, we're done. | 502 ** If this is an old file, we're done. |
511 */ | 503 */ |
512 » » if (u.tzhead.tzh_version[0] == '\0') | 504 » » if (up->tzhead.tzh_version[0] == '\0') |
513 break; | 505 break; |
514 » » nread -= p - u.buf; | 506 » » nread -= p - up->buf; |
515 for (i = 0; i < nread; ++i) | 507 for (i = 0; i < nread; ++i) |
516 » » » u.buf[i] = p[i]; | 508 » » » up->buf[i] = p[i]; |
517 /* | 509 /* |
518 » » ** If this is a narrow integer time_t system, we're done. | 510 » » ** If this is a signed narrow time_t system, we're done. |
519 */ | 511 */ |
520 » » if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t)) | 512 » » if (TYPE_SIGNED(time_t) && stored >= (int) sizeof(time_t)) |
521 break; | 513 break; |
522 } | 514 } |
523 if (doextend && nread > 2 && | 515 if (doextend && nread > 2 && |
524 » » u.buf[0] == '\n' && u.buf[nread - 1] == '\n' && | 516 » » up->buf[0] == '\n' && up->buf[nread - 1] == '\n' && |
525 sp->typecnt + 2 <= TZ_MAX_TYPES) { | 517 sp->typecnt + 2 <= TZ_MAX_TYPES) { |
526 struct state ts; | 518 struct state ts; |
527 register int result; | 519 register int result; |
528 | 520 |
529 » » » u.buf[nread - 1] = '\0'; | 521 » » » up->buf[nread - 1] = '\0'; |
530 » » » result = tzparse(&u.buf[1], &ts, FALSE); | 522 » » » result = tzparse(&up->buf[1], &ts, FALSE); |
531 if (result == 0 && ts.typecnt == 2 && | 523 if (result == 0 && ts.typecnt == 2 && |
532 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) { | 524 sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) { |
533 for (i = 0; i < 2; ++i) | 525 for (i = 0; i < 2; ++i) |
534 ts.ttis[i].tt_abbrind += | 526 ts.ttis[i].tt_abbrind += |
535 sp->charcnt; | 527 sp->charcnt; |
536 for (i = 0; i < ts.charcnt; ++i) | 528 for (i = 0; i < ts.charcnt; ++i) |
537 sp->chars[sp->charcnt++] = | 529 sp->chars[sp->charcnt++] = |
538 ts.chars[i]; | 530 ts.chars[i]; |
539 i = 0; | 531 i = 0; |
540 while (i < ts.timecnt && | 532 while (i < ts.timecnt && |
541 ts.ats[i] <= | 533 ts.ats[i] <= |
542 sp->ats[sp->timecnt - 1]) | 534 sp->ats[sp->timecnt - 1]) |
543 ++i; | 535 ++i; |
544 while (i < ts.timecnt && | 536 while (i < ts.timecnt && |
545 sp->timecnt < TZ_MAX_TIMES) { | 537 sp->timecnt < TZ_MAX_TIMES) { |
546 sp->ats[sp->timecnt] = | 538 sp->ats[sp->timecnt] = |
547 ts.ats[i]; | 539 ts.ats[i]; |
548 sp->types[sp->timecnt] = | 540 sp->types[sp->timecnt] = |
549 sp->typecnt + | 541 sp->typecnt + |
550 ts.types[i]; | 542 ts.types[i]; |
551 ++sp->timecnt; | 543 ++sp->timecnt; |
552 ++i; | 544 ++i; |
553 } | 545 } |
554 sp->ttis[sp->typecnt++] = ts.ttis[0]; | 546 sp->ttis[sp->typecnt++] = ts.ttis[0]; |
555 sp->ttis[sp->typecnt++] = ts.ttis[1]; | 547 sp->ttis[sp->typecnt++] = ts.ttis[1]; |
556 } | 548 } |
557 } | 549 } |
558 sp->goback = sp->goahead = FALSE; | |
559 if (sp->timecnt > 1) { | 550 if (sp->timecnt > 1) { |
560 for (i = 1; i < sp->timecnt; ++i) | 551 for (i = 1; i < sp->timecnt; ++i) |
561 if (typesequiv(sp, sp->types[i], sp->types[0]) && | 552 if (typesequiv(sp, sp->types[i], sp->types[0]) && |
562 differ_by_repeat(sp->ats[i], sp->ats[0])) { | 553 differ_by_repeat(sp->ats[i], sp->ats[0])) { |
563 sp->goback = TRUE; | 554 sp->goback = TRUE; |
564 break; | 555 break; |
565 } | 556 } |
566 for (i = sp->timecnt - 2; i >= 0; --i) | 557 for (i = sp->timecnt - 2; i >= 0; --i) |
567 if (typesequiv(sp, sp->types[sp->timecnt - 1], | 558 if (typesequiv(sp, sp->types[sp->timecnt - 1], |
568 sp->types[i]) && | 559 sp->types[i]) && |
569 differ_by_repeat(sp->ats[sp->timecnt - 1], | 560 differ_by_repeat(sp->ats[sp->timecnt - 1], |
570 sp->ats[i])) { | 561 sp->ats[i])) { |
571 sp->goahead = TRUE; | 562 sp->goahead = TRUE; |
572 break; | 563 break; |
573 } | 564 } |
574 } | 565 } |
| 566 /* |
| 567 ** If type 0 is is unused in transitions, |
| 568 ** it's the type to use for early times. |
| 569 */ |
| 570 for (i = 0; i < sp->typecnt; ++i) |
| 571 if (sp->types[i] == 0) |
| 572 break; |
| 573 i = (i >= sp->typecnt) ? 0 : -1; |
| 574 /* |
| 575 ** Absent the above, |
| 576 ** if there are transition times |
| 577 ** and the first transition is to a daylight time |
| 578 ** find the standard type less than and closest to |
| 579 ** the type of the first transition. |
| 580 */ |
| 581 if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) { |
| 582 i = sp->types[0]; |
| 583 while (--i >= 0) |
| 584 if (!sp->ttis[i].tt_isdst) |
| 585 break; |
| 586 } |
| 587 /* |
| 588 ** If no result yet, find the first standard type. |
| 589 ** If there is none, punt to type zero. |
| 590 */ |
| 591 if (i < 0) { |
| 592 i = 0; |
| 593 while (sp->ttis[i].tt_isdst) |
| 594 if (++i >= sp->typecnt) { |
| 595 i = 0; |
| 596 break; |
| 597 } |
| 598 } |
| 599 sp->defaulttype = i; |
| 600 #ifdef ALL_STATE |
| 601 free(up); |
| 602 #endif /* defined ALL_STATE */ |
575 return 0; | 603 return 0; |
| 604 oops: |
| 605 #ifdef ALL_STATE |
| 606 free(up); |
| 607 #endif /* defined ALL_STATE */ |
| 608 return -1; |
576 } | 609 } |
577 | 610 |
578 static int | 611 static int |
579 typesequiv(sp, a, b) | 612 typesequiv(const struct state *const sp, const int a, const int b) |
580 const struct state * const» sp; | |
581 const int» » » a; | |
582 const int» » » b; | |
583 { | 613 { |
584 register int result; | 614 register int result; |
585 | 615 |
586 if (sp == NULL || | 616 if (sp == NULL || |
587 a < 0 || a >= sp->typecnt || | 617 a < 0 || a >= sp->typecnt || |
588 b < 0 || b >= sp->typecnt) | 618 b < 0 || b >= sp->typecnt) |
589 result = FALSE; | 619 result = FALSE; |
590 else { | 620 else { |
591 register const struct ttinfo * ap = &sp->ttis[a]; | 621 register const struct ttinfo * ap = &sp->ttis[a]; |
592 register const struct ttinfo * bp = &sp->ttis[b]; | 622 register const struct ttinfo * bp = &sp->ttis[b]; |
(...skipping 16 matching lines...) Expand all Loading... |
609 DAYSPERNYEAR, DAYSPERLYEAR | 639 DAYSPERNYEAR, DAYSPERLYEAR |
610 }; | 640 }; |
611 | 641 |
612 /* | 642 /* |
613 ** Given a pointer into a time zone string, scan until a character that is not | 643 ** Given a pointer into a time zone string, scan until a character that is not |
614 ** a valid character in a zone name is found. Return a pointer to that | 644 ** a valid character in a zone name is found. Return a pointer to that |
615 ** character. | 645 ** character. |
616 */ | 646 */ |
617 | 647 |
618 static const char * | 648 static const char * |
619 getzname(strp) | 649 getzname(register const char *strp) |
620 register const char *» strp; | |
621 { | 650 { |
622 register char c; | 651 register char c; |
623 | 652 |
624 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && | 653 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' && |
625 c != '+') | 654 c != '+') |
626 ++strp; | 655 ++strp; |
627 return strp; | 656 return strp; |
628 } | 657 } |
629 | 658 |
630 /* | 659 /* |
(...skipping 16 matching lines...) Expand all Loading... |
647 } | 676 } |
648 | 677 |
649 /* | 678 /* |
650 ** Given a pointer into a time zone string, extract a number from that string. | 679 ** Given a pointer into a time zone string, extract a number from that string. |
651 ** Check that the number is within a specified range; if it is not, return | 680 ** Check that the number is within a specified range; if it is not, return |
652 ** NULL. | 681 ** NULL. |
653 ** Otherwise, return a pointer to the first character not part of the number. | 682 ** Otherwise, return a pointer to the first character not part of the number. |
654 */ | 683 */ |
655 | 684 |
656 static const char * | 685 static const char * |
657 getnum(strp, nump, min, max) | 686 getnum(register const char *strp, int *const nump, const int min, const int max) |
658 register const char *» strp; | |
659 int * const» » nump; | |
660 const int» » min; | |
661 const int» » max; | |
662 { | 687 { |
663 register char c; | 688 register char c; |
664 register int num; | 689 register int num; |
665 | 690 |
666 if (strp == NULL || !is_digit(c = *strp)) | 691 if (strp == NULL || !is_digit(c = *strp)) |
667 return NULL; | 692 return NULL; |
668 num = 0; | 693 num = 0; |
669 do { | 694 do { |
670 num = num * 10 + (c - '0'); | 695 num = num * 10 + (c - '0'); |
671 if (num > max) | 696 if (num > max) |
672 return NULL; /* illegal value */ | 697 return NULL; /* illegal value */ |
673 c = *++strp; | 698 c = *++strp; |
674 } while (is_digit(c)); | 699 } while (is_digit(c)); |
675 if (num < min) | 700 if (num < min) |
676 return NULL; /* illegal value */ | 701 return NULL; /* illegal value */ |
677 *nump = num; | 702 *nump = num; |
678 return strp; | 703 return strp; |
679 } | 704 } |
680 | 705 |
681 /* | 706 /* |
682 ** Given a pointer into a time zone string, extract a number of seconds, | 707 ** Given a pointer into a time zone string, extract a number of seconds, |
683 ** in hh[:mm[:ss]] form, from the string. | 708 ** in hh[:mm[:ss]] form, from the string. |
684 ** If any error occurs, return NULL. | 709 ** If any error occurs, return NULL. |
685 ** Otherwise, return a pointer to the first character not part of the number | 710 ** Otherwise, return a pointer to the first character not part of the number |
686 ** of seconds. | 711 ** of seconds. |
687 */ | 712 */ |
688 | 713 |
689 static const char * | 714 static const char * |
690 getsecs(strp, secsp) | 715 getsecs(register const char *strp, int_fast32_t *const secsp) |
691 register const char *» strp; | |
692 long * const» » secsp; | |
693 { | 716 { |
694 int num; | 717 int num; |
695 | 718 |
696 /* | 719 /* |
697 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like | 720 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like |
698 ** "M10.4.6/26", which does not conform to Posix, | 721 ** "M10.4.6/26", which does not conform to Posix, |
699 ** but which specifies the equivalent of | 722 ** but which specifies the equivalent of |
700 ** ``02:00 on the first Sunday on or after 23 Oct''. | 723 ** ``02:00 on the first Sunday on or after 23 Oct''. |
701 */ | 724 */ |
702 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); | 725 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); |
703 if (strp == NULL) | 726 if (strp == NULL) |
704 return NULL; | 727 return NULL; |
705 » *secsp = num * (long) SECSPERHOUR; | 728 » *secsp = num * (int_fast32_t) SECSPERHOUR; |
706 if (*strp == ':') { | 729 if (*strp == ':') { |
707 ++strp; | 730 ++strp; |
708 strp = getnum(strp, &num, 0, MINSPERHOUR - 1); | 731 strp = getnum(strp, &num, 0, MINSPERHOUR - 1); |
709 if (strp == NULL) | 732 if (strp == NULL) |
710 return NULL; | 733 return NULL; |
711 *secsp += num * SECSPERMIN; | 734 *secsp += num * SECSPERMIN; |
712 if (*strp == ':') { | 735 if (*strp == ':') { |
713 ++strp; | 736 ++strp; |
714 /* `SECSPERMIN' allows for leap seconds. */ | 737 /* `SECSPERMIN' allows for leap seconds. */ |
715 strp = getnum(strp, &num, 0, SECSPERMIN); | 738 strp = getnum(strp, &num, 0, SECSPERMIN); |
716 if (strp == NULL) | 739 if (strp == NULL) |
717 return NULL; | 740 return NULL; |
718 *secsp += num; | 741 *secsp += num; |
719 } | 742 } |
720 } | 743 } |
721 return strp; | 744 return strp; |
722 } | 745 } |
723 | 746 |
724 /* | 747 /* |
725 ** Given a pointer into a time zone string, extract an offset, in | 748 ** Given a pointer into a time zone string, extract an offset, in |
726 ** [+-]hh[:mm[:ss]] form, from the string. | 749 ** [+-]hh[:mm[:ss]] form, from the string. |
727 ** If any error occurs, return NULL. | 750 ** If any error occurs, return NULL. |
728 ** Otherwise, return a pointer to the first character not part of the time. | 751 ** Otherwise, return a pointer to the first character not part of the time. |
729 */ | 752 */ |
730 | 753 |
731 static const char * | 754 static const char * |
732 getoffset(strp, offsetp) | 755 getoffset(register const char *strp, int_fast32_t *const offsetp) |
733 register const char *» strp; | |
734 long * const» » offsetp; | |
735 { | 756 { |
736 register int neg = 0; | 757 register int neg = 0; |
737 | 758 |
738 if (*strp == '-') { | 759 if (*strp == '-') { |
739 neg = 1; | 760 neg = 1; |
740 ++strp; | 761 ++strp; |
741 } else if (*strp == '+') | 762 } else if (*strp == '+') |
742 ++strp; | 763 ++strp; |
743 strp = getsecs(strp, offsetp); | 764 strp = getsecs(strp, offsetp); |
744 if (strp == NULL) | 765 if (strp == NULL) |
745 return NULL; /* illegal time */ | 766 return NULL; /* illegal time */ |
746 if (neg) | 767 if (neg) |
747 *offsetp = -*offsetp; | 768 *offsetp = -*offsetp; |
748 return strp; | 769 return strp; |
749 } | 770 } |
750 | 771 |
751 /* | 772 /* |
752 ** Given a pointer into a time zone string, extract a rule in the form | 773 ** Given a pointer into a time zone string, extract a rule in the form |
753 ** date[/time]. See POSIX section 8 for the format of "date" and "time". | 774 ** date[/time]. See POSIX section 8 for the format of "date" and "time". |
754 ** If a valid rule is not found, return NULL. | 775 ** If a valid rule is not found, return NULL. |
755 ** Otherwise, return a pointer to the first character not part of the rule. | 776 ** Otherwise, return a pointer to the first character not part of the rule. |
756 */ | 777 */ |
757 | 778 |
758 static const char * | 779 static const char * |
759 getrule(strp, rulep) | 780 getrule(const char *strp, register struct rule *const rulep) |
760 const char *» » » strp; | |
761 register struct rule * const» rulep; | |
762 { | 781 { |
763 if (*strp == 'J') { | 782 if (*strp == 'J') { |
764 /* | 783 /* |
765 ** Julian day. | 784 ** Julian day. |
766 */ | 785 */ |
767 rulep->r_type = JULIAN_DAY; | 786 rulep->r_type = JULIAN_DAY; |
768 ++strp; | 787 ++strp; |
769 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); | 788 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); |
770 } else if (*strp == 'M') { | 789 } else if (*strp == 'M') { |
771 /* | 790 /* |
(...skipping 19 matching lines...) Expand all Loading... |
791 rulep->r_type = DAY_OF_YEAR; | 810 rulep->r_type = DAY_OF_YEAR; |
792 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); | 811 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); |
793 } else return NULL; /* invalid format */ | 812 } else return NULL; /* invalid format */ |
794 if (strp == NULL) | 813 if (strp == NULL) |
795 return NULL; | 814 return NULL; |
796 if (*strp == '/') { | 815 if (*strp == '/') { |
797 /* | 816 /* |
798 ** Time specified. | 817 ** Time specified. |
799 */ | 818 */ |
800 ++strp; | 819 ++strp; |
801 » » strp = getsecs(strp, &rulep->r_time); | 820 » » strp = getoffset(strp, &rulep->r_time); |
802 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ | 821 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ |
803 return strp; | 822 return strp; |
804 } | 823 } |
805 | 824 |
806 /* | 825 /* |
807 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the | 826 ** Given a year, a rule, and the offset from UT at the time that rule takes |
808 ** year, a rule, and the offset from UTC at the time that rule takes effect, | 827 ** effect, calculate the year-relative time that rule takes effect. |
809 ** calculate the Epoch-relative time that rule takes effect. | |
810 */ | 828 */ |
811 | 829 |
812 static time_t | 830 static int_fast32_t |
813 transtime(janfirst, year, rulep, offset) | 831 transtime(const int year, register const struct rule *const rulep, |
814 const time_t» » » » janfirst; | 832 » const int_fast32_t offset) |
815 const int» » » » year; | |
816 register const struct rule * const» rulep; | |
817 const long» » » » offset; | |
818 { | 833 { |
819 register int leapyear; | 834 register int leapyear; |
820 » register time_t»value; | 835 » register int_fast32_t value; |
821 register int i; | 836 register int i; |
822 int d, m1, yy0, yy1, yy2, dow; | 837 int d, m1, yy0, yy1, yy2, dow; |
823 | 838 |
824 INITIALIZE(value); | 839 INITIALIZE(value); |
825 leapyear = isleap(year); | 840 leapyear = isleap(year); |
826 switch (rulep->r_type) { | 841 switch (rulep->r_type) { |
827 | 842 |
828 case JULIAN_DAY: | 843 case JULIAN_DAY: |
829 /* | 844 /* |
830 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap | 845 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap |
831 ** years. | 846 ** years. |
832 ** In non-leap years, or if the day number is 59 or less, just | 847 ** In non-leap years, or if the day number is 59 or less, just |
833 ** add SECSPERDAY times the day number-1 to the time of | 848 ** add SECSPERDAY times the day number-1 to the time of |
834 ** January 1, midnight, to get the day. | 849 ** January 1, midnight, to get the day. |
835 */ | 850 */ |
836 » » value = janfirst + (rulep->r_day - 1) * SECSPERDAY; | 851 » » value = (rulep->r_day - 1) * SECSPERDAY; |
837 if (leapyear && rulep->r_day >= 60) | 852 if (leapyear && rulep->r_day >= 60) |
838 value += SECSPERDAY; | 853 value += SECSPERDAY; |
839 break; | 854 break; |
840 | 855 |
841 case DAY_OF_YEAR: | 856 case DAY_OF_YEAR: |
842 /* | 857 /* |
843 ** n - day of year. | 858 ** n - day of year. |
844 ** Just add SECSPERDAY times the day number to the time of | 859 ** Just add SECSPERDAY times the day number to the time of |
845 ** January 1, midnight, to get the day. | 860 ** January 1, midnight, to get the day. |
846 */ | 861 */ |
847 » » value = janfirst + rulep->r_day * SECSPERDAY; | 862 » » value = rulep->r_day * SECSPERDAY; |
848 break; | 863 break; |
849 | 864 |
850 case MONTH_NTH_DAY_OF_WEEK: | 865 case MONTH_NTH_DAY_OF_WEEK: |
851 /* | 866 /* |
852 ** Mm.n.d - nth "dth day" of month m. | 867 ** Mm.n.d - nth "dth day" of month m. |
853 */ | 868 */ |
854 value = janfirst; | |
855 for (i = 0; i < rulep->r_mon - 1; ++i) | |
856 value += mon_lengths[leapyear][i] * SECSPERDAY; | |
857 | 869 |
858 /* | 870 /* |
859 ** Use Zeller's Congruence to get day-of-week of first day of | 871 ** Use Zeller's Congruence to get day-of-week of first day of |
860 ** month. | 872 ** month. |
861 */ | 873 */ |
862 m1 = (rulep->r_mon + 9) % 12 + 1; | 874 m1 = (rulep->r_mon + 9) % 12 + 1; |
863 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; | 875 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; |
864 yy1 = yy0 / 100; | 876 yy1 = yy0 / 100; |
865 yy2 = yy0 % 100; | 877 yy2 = yy0 % 100; |
866 dow = ((26 * m1 - 2) / 10 + | 878 dow = ((26 * m1 - 2) / 10 + |
(...skipping 12 matching lines...) Expand all Loading... |
879 for (i = 1; i < rulep->r_week; ++i) { | 891 for (i = 1; i < rulep->r_week; ++i) { |
880 if (d + DAYSPERWEEK >= | 892 if (d + DAYSPERWEEK >= |
881 mon_lengths[leapyear][rulep->r_mon - 1]) | 893 mon_lengths[leapyear][rulep->r_mon - 1]) |
882 break; | 894 break; |
883 d += DAYSPERWEEK; | 895 d += DAYSPERWEEK; |
884 } | 896 } |
885 | 897 |
886 /* | 898 /* |
887 ** "d" is the day-of-month (zero-origin) of the day we want. | 899 ** "d" is the day-of-month (zero-origin) of the day we want. |
888 */ | 900 */ |
889 » » value += d * SECSPERDAY; | 901 » » value = d * SECSPERDAY; |
| 902 » » for (i = 0; i < rulep->r_mon - 1; ++i) |
| 903 » » » value += mon_lengths[leapyear][i] * SECSPERDAY; |
890 break; | 904 break; |
891 } | 905 } |
892 | 906 |
893 /* | 907 /* |
894 » ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in | 908 » ** "value" is the year-relative time of 00:00:00 UT on the day in |
895 » ** question. To get the Epoch-relative time of the specified local | 909 » ** question. To get the year-relative time of the specified local |
896 ** time on that day, add the transition time and the current offset | 910 ** time on that day, add the transition time and the current offset |
897 » ** from UTC. | 911 » ** from UT. |
898 */ | 912 */ |
899 return value + rulep->r_time + offset; | 913 return value + rulep->r_time + offset; |
900 } | 914 } |
901 | 915 |
902 /* | 916 /* |
903 ** Given a POSIX section 8-style TZ string, fill in the rule tables as | 917 ** Given a POSIX section 8-style TZ string, fill in the rule tables as |
904 ** appropriate. | 918 ** appropriate. |
905 */ | 919 */ |
906 | 920 |
907 static int | 921 static int |
908 tzparse(name, sp, lastditch) | 922 tzparse(const char *name, register struct state *const sp, |
909 const char *» » » name; | 923 » const int lastditch) |
910 register struct state * const» sp; | |
911 const int» » » lastditch; | |
912 { | 924 { |
913 const char * stdname; | 925 const char * stdname; |
914 const char * dstname; | 926 const char * dstname; |
915 size_t stdlen; | 927 size_t stdlen; |
916 size_t dstlen; | 928 size_t dstlen; |
917 » long» » » » stdoffset; | 929 » int_fast32_t» » » stdoffset; |
918 » long» » » » dstoffset; | 930 » int_fast32_t» » » dstoffset; |
919 » register time_t *» » atp; | |
920 » register unsigned char *» typep; | |
921 register char * cp; | 931 register char * cp; |
922 register int load_result; | 932 register int load_result; |
| 933 static struct ttinfo zttinfo; |
923 | 934 |
924 INITIALIZE(dstname); | 935 INITIALIZE(dstname); |
925 stdname = name; | 936 stdname = name; |
926 if (lastditch) { | 937 if (lastditch) { |
927 stdlen = strlen(name); /* length of standard zone name */ | 938 stdlen = strlen(name); /* length of standard zone name */ |
928 name += stdlen; | 939 name += stdlen; |
929 if (stdlen >= sizeof sp->chars) | 940 if (stdlen >= sizeof sp->chars) |
930 stdlen = (sizeof sp->chars) - 1; | 941 stdlen = (sizeof sp->chars) - 1; |
931 stdoffset = 0; | 942 stdoffset = 0; |
932 } else { | 943 } else { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
968 name = getoffset(name, &dstoffset); | 979 name = getoffset(name, &dstoffset); |
969 if (name == NULL) | 980 if (name == NULL) |
970 return -1; | 981 return -1; |
971 } else dstoffset = stdoffset - SECSPERHOUR; | 982 } else dstoffset = stdoffset - SECSPERHOUR; |
972 if (*name == '\0' && load_result != 0) | 983 if (*name == '\0' && load_result != 0) |
973 name = TZDEFRULESTRING; | 984 name = TZDEFRULESTRING; |
974 if (*name == ',' || *name == ';') { | 985 if (*name == ',' || *name == ';') { |
975 struct rule start; | 986 struct rule start; |
976 struct rule end; | 987 struct rule end; |
977 register int year; | 988 register int year; |
978 » » » register time_t»janfirst; | 989 » » » register int» yearlim; |
979 » » » time_t» » starttime; | 990 » » » register int» timecnt; |
980 » » » time_t» » endtime; | 991 » » » time_t» » janfirst; |
981 | 992 |
982 ++name; | 993 ++name; |
983 if ((name = getrule(name, &start)) == NULL) | 994 if ((name = getrule(name, &start)) == NULL) |
984 return -1; | 995 return -1; |
985 if (*name++ != ',') | 996 if (*name++ != ',') |
986 return -1; | 997 return -1; |
987 if ((name = getrule(name, &end)) == NULL) | 998 if ((name = getrule(name, &end)) == NULL) |
988 return -1; | 999 return -1; |
989 if (*name != '\0') | 1000 if (*name != '\0') |
990 return -1; | 1001 return -1; |
991 sp->typecnt = 2; /* standard time and DST */ | 1002 sp->typecnt = 2; /* standard time and DST */ |
992 /* | 1003 /* |
993 ** Two transitions per year, from EPOCH_YEAR forward. | 1004 ** Two transitions per year, from EPOCH_YEAR forward. |
994 */ | 1005 */ |
| 1006 sp->ttis[0] = sp->ttis[1] = zttinfo; |
995 sp->ttis[0].tt_gmtoff = -dstoffset; | 1007 sp->ttis[0].tt_gmtoff = -dstoffset; |
996 sp->ttis[0].tt_isdst = 1; | 1008 sp->ttis[0].tt_isdst = 1; |
997 sp->ttis[0].tt_abbrind = stdlen + 1; | 1009 sp->ttis[0].tt_abbrind = stdlen + 1; |
998 sp->ttis[1].tt_gmtoff = -stdoffset; | 1010 sp->ttis[1].tt_gmtoff = -stdoffset; |
999 sp->ttis[1].tt_isdst = 0; | 1011 sp->ttis[1].tt_isdst = 0; |
1000 sp->ttis[1].tt_abbrind = 0; | 1012 sp->ttis[1].tt_abbrind = 0; |
1001 » » » atp = sp->ats; | 1013 » » » sp->defaulttype = 0; |
1002 » » » typep = sp->types; | 1014 » » » timecnt = 0; |
1003 janfirst = 0; | 1015 janfirst = 0; |
1004 » » » sp->timecnt = 0; | 1016 » » » yearlim = EPOCH_YEAR + YEARSPERREPEAT; |
1005 » » » for (year = EPOCH_YEAR; | 1017 » » » for (year = EPOCH_YEAR; year < yearlim; year++) { |
1006 » » » sp->timecnt + 2 <= TZ_MAX_TIMES; | 1018 » » » » int_fast32_t |
1007 » » » ++year) { | 1019 » » » » starttime = transtime(year, &start, stdoffset)
, |
1008 » » » » time_t» newfirst; | 1020 » » » » endtime = transtime(year, &end, dstoffset); |
1009 | 1021 » » » » int_fast32_t |
1010 » » » » starttime = transtime(janfirst, year, &start, | 1022 » » » » yearsecs = (year_lengths[isleap(year)] |
1011 » » » » » stdoffset); | 1023 » » » » » * SECSPERDAY); |
1012 » » » » endtime = transtime(janfirst, year, &end, | 1024 » » » » int reversed = endtime < starttime; |
1013 » » » » » dstoffset); | 1025 » » » » if (reversed) { |
1014 » » » » if (starttime > endtime) { | 1026 » » » » » int_fast32_t swap = starttime; |
1015 » » » » » *atp++ = endtime; | 1027 » » » » » starttime = endtime; |
1016 » » » » » *typep++ = 1;» /* DST ends */ | 1028 » » » » » endtime = swap; |
1017 » » » » » *atp++ = starttime; | |
1018 » » » » » *typep++ = 0;» /* DST begins */ | |
1019 » » » » } else { | |
1020 » » » » » *atp++ = starttime; | |
1021 » » » » » *typep++ = 0;» /* DST begins */ | |
1022 » » » » » *atp++ = endtime; | |
1023 » » » » » *typep++ = 1;» /* DST ends */ | |
1024 } | 1029 } |
1025 » » » » sp->timecnt += 2; | 1030 » » » » if (reversed |
1026 » » » » newfirst = janfirst; | 1031 » » » » || (starttime < endtime |
1027 » » » » newfirst += year_lengths[isleap(year)] * | 1032 » » » » » && (endtime - starttime |
1028 » » » » » SECSPERDAY; | 1033 » » » » » < (yearsecs |
1029 » » » » if (newfirst <= janfirst) | 1034 » » » » » + (stdoffset - dstoffset))))) { |
| 1035 » » » » » if (TZ_MAX_TIMES - 2 < timecnt) |
| 1036 » » » » » » break; |
| 1037 » » » » » yearlim = year + YEARSPERREPEAT + 1; |
| 1038 » » » » » sp->ats[timecnt] = janfirst; |
| 1039 » » » » » if (increment_overflow_time |
| 1040 » » » » » (&sp->ats[timecnt], starttime)) |
| 1041 » » » » » » break; |
| 1042 » » » » » sp->types[timecnt++] = reversed; |
| 1043 » » » » » sp->ats[timecnt] = janfirst; |
| 1044 » » » » » if (increment_overflow_time |
| 1045 » » » » » (&sp->ats[timecnt], endtime)) |
| 1046 » » » » » » break; |
| 1047 » » » » » sp->types[timecnt++] = !reversed; |
| 1048 » » » » } |
| 1049 » » » » if (increment_overflow_time(&janfirst, yearsecs)
) |
1030 break; | 1050 break; |
1031 janfirst = newfirst; | |
1032 } | 1051 } |
| 1052 sp->timecnt = timecnt; |
| 1053 if (!timecnt) |
| 1054 sp->typecnt = 1; /* Perpetual DST. */ |
1033 } else { | 1055 } else { |
1034 » » » register long» theirstdoffset; | 1056 » » » register int_fast32_t» theirstdoffset; |
1035 » » » register long» theirdstoffset; | 1057 » » » register int_fast32_t» theirdstoffset; |
1036 » » » register long» theiroffset; | 1058 » » » register int_fast32_t» theiroffset; |
1037 » » » register int» isdst; | 1059 » » » register int» » isdst; |
1038 » » » register int» i; | 1060 » » » register int» » i; |
1039 » » » register int» j; | 1061 » » » register int» » j; |
1040 | 1062 |
1041 if (*name != '\0') | 1063 if (*name != '\0') |
1042 return -1; | 1064 return -1; |
1043 /* | 1065 /* |
1044 ** Initial values of theirstdoffset and theirdstoffset. | 1066 ** Initial values of theirstdoffset and theirdstoffset. |
1045 */ | 1067 */ |
1046 theirstdoffset = 0; | 1068 theirstdoffset = 0; |
1047 for (i = 0; i < sp->timecnt; ++i) { | 1069 for (i = 0; i < sp->timecnt; ++i) { |
1048 j = sp->types[i]; | 1070 j = sp->types[i]; |
1049 if (!sp->ttis[j].tt_isdst) { | 1071 if (!sp->ttis[j].tt_isdst) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1098 theirstdoffset; | 1120 theirstdoffset; |
1099 } | 1121 } |
1100 } | 1122 } |
1101 theiroffset = -sp->ttis[j].tt_gmtoff; | 1123 theiroffset = -sp->ttis[j].tt_gmtoff; |
1102 if (sp->ttis[j].tt_isdst) | 1124 if (sp->ttis[j].tt_isdst) |
1103 theirdstoffset = theiroffset; | 1125 theirdstoffset = theiroffset; |
1104 else theirstdoffset = theiroffset; | 1126 else theirstdoffset = theiroffset; |
1105 } | 1127 } |
1106 /* | 1128 /* |
1107 ** Finally, fill in ttis. | 1129 ** Finally, fill in ttis. |
1108 ** ttisstd and ttisgmt need not be handled. | |
1109 */ | 1130 */ |
| 1131 sp->ttis[0] = sp->ttis[1] = zttinfo; |
1110 sp->ttis[0].tt_gmtoff = -stdoffset; | 1132 sp->ttis[0].tt_gmtoff = -stdoffset; |
1111 sp->ttis[0].tt_isdst = FALSE; | 1133 sp->ttis[0].tt_isdst = FALSE; |
1112 sp->ttis[0].tt_abbrind = 0; | 1134 sp->ttis[0].tt_abbrind = 0; |
1113 sp->ttis[1].tt_gmtoff = -dstoffset; | 1135 sp->ttis[1].tt_gmtoff = -dstoffset; |
1114 sp->ttis[1].tt_isdst = TRUE; | 1136 sp->ttis[1].tt_isdst = TRUE; |
1115 sp->ttis[1].tt_abbrind = stdlen + 1; | 1137 sp->ttis[1].tt_abbrind = stdlen + 1; |
1116 sp->typecnt = 2; | 1138 sp->typecnt = 2; |
| 1139 sp->defaulttype = 0; |
1117 } | 1140 } |
1118 } else { | 1141 } else { |
1119 dstlen = 0; | 1142 dstlen = 0; |
1120 sp->typecnt = 1; /* only standard time */ | 1143 sp->typecnt = 1; /* only standard time */ |
1121 sp->timecnt = 0; | 1144 sp->timecnt = 0; |
| 1145 sp->ttis[0] = zttinfo; |
1122 sp->ttis[0].tt_gmtoff = -stdoffset; | 1146 sp->ttis[0].tt_gmtoff = -stdoffset; |
1123 sp->ttis[0].tt_isdst = 0; | 1147 sp->ttis[0].tt_isdst = 0; |
1124 sp->ttis[0].tt_abbrind = 0; | 1148 sp->ttis[0].tt_abbrind = 0; |
| 1149 sp->defaulttype = 0; |
1125 } | 1150 } |
1126 sp->charcnt = stdlen + 1; | 1151 sp->charcnt = stdlen + 1; |
1127 if (dstlen != 0) | 1152 if (dstlen != 0) |
1128 sp->charcnt += dstlen + 1; | 1153 sp->charcnt += dstlen + 1; |
1129 if ((size_t) sp->charcnt > sizeof sp->chars) | 1154 if ((size_t) sp->charcnt > sizeof sp->chars) |
1130 return -1; | 1155 return -1; |
1131 cp = sp->chars; | 1156 cp = sp->chars; |
1132 (void) strncpy(cp, stdname, stdlen); | 1157 (void) strncpy(cp, stdname, stdlen); |
1133 cp += stdlen; | 1158 cp += stdlen; |
1134 *cp++ = '\0'; | 1159 *cp++ = '\0'; |
1135 if (dstlen != 0) { | 1160 if (dstlen != 0) { |
1136 (void) strncpy(cp, dstname, dstlen); | 1161 (void) strncpy(cp, dstname, dstlen); |
1137 *(cp + dstlen) = '\0'; | 1162 *(cp + dstlen) = '\0'; |
1138 } | 1163 } |
1139 return 0; | 1164 return 0; |
1140 } | 1165 } |
1141 | 1166 |
1142 static void | 1167 static void |
1143 gmtload(sp) | 1168 gmtload(struct state *const sp) |
1144 struct state * const» sp; | |
1145 { | 1169 { |
1146 if (tzload(gmt, sp, TRUE) != 0) | 1170 if (tzload(gmt, sp, TRUE) != 0) |
1147 (void) tzparse(gmt, sp, TRUE); | 1171 (void) tzparse(gmt, sp, TRUE); |
1148 } | 1172 } |
1149 | 1173 |
1150 #ifndef STD_INSPIRED | 1174 #ifndef STD_INSPIRED |
1151 /* | 1175 /* |
1152 ** A non-static declaration of tzsetwall in a system header file | 1176 ** A non-static declaration of tzsetwall in a system header file |
1153 ** may cause a warning about this upcoming static declaration... | 1177 ** may cause a warning about this upcoming static declaration... |
1154 */ | 1178 */ |
1155 static | 1179 static |
1156 #endif /* !defined STD_INSPIRED */ | 1180 #endif /* !defined STD_INSPIRED */ |
1157 void | 1181 void |
1158 tzsetwall(void) | 1182 tzsetwall(void) |
1159 { | 1183 { |
1160 if (lcl_is_set < 0) | 1184 if (lcl_is_set < 0) |
1161 return; | 1185 return; |
1162 lcl_is_set = -1; | 1186 lcl_is_set = -1; |
1163 | 1187 |
1164 #ifdef ALL_STATE | 1188 #ifdef ALL_STATE |
1165 if (lclptr == NULL) { | 1189 if (lclptr == NULL) { |
1166 » » lclptr = (struct state *) malloc(sizeof *lclptr); | 1190 » » lclptr = malloc(sizeof *lclptr); |
1167 if (lclptr == NULL) { | 1191 if (lclptr == NULL) { |
1168 settzname(); /* all we can do */ | 1192 settzname(); /* all we can do */ |
1169 return; | 1193 return; |
1170 } | 1194 } |
1171 } | 1195 } |
1172 #endif /* defined ALL_STATE */ | 1196 #endif /* defined ALL_STATE */ |
1173 » if (tzload((char *) NULL, lclptr, TRUE) != 0) | 1197 » if (tzload(NULL, lclptr, TRUE) != 0) |
1174 gmtload(lclptr); | 1198 gmtload(lclptr); |
1175 settzname(); | 1199 settzname(); |
1176 } | 1200 } |
1177 | 1201 |
1178 void | 1202 void |
1179 tzset(void) | 1203 tzset(void) |
1180 { | 1204 { |
1181 register const char * name; | 1205 register const char * name; |
1182 | 1206 |
1183 name = getenv("TZ"); | 1207 name = getenv("TZ"); |
1184 if (name == NULL) { | 1208 if (name == NULL) { |
1185 tzsetwall(); | 1209 tzsetwall(); |
1186 return; | 1210 return; |
1187 } | 1211 } |
1188 | 1212 |
1189 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) | 1213 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) |
1190 return; | 1214 return; |
1191 lcl_is_set = strlen(name) < sizeof lcl_TZname; | 1215 lcl_is_set = strlen(name) < sizeof lcl_TZname; |
1192 if (lcl_is_set) | 1216 if (lcl_is_set) |
1193 (void) strcpy(lcl_TZname, name); | 1217 (void) strcpy(lcl_TZname, name); |
1194 | 1218 |
1195 #ifdef ALL_STATE | 1219 #ifdef ALL_STATE |
1196 if (lclptr == NULL) { | 1220 if (lclptr == NULL) { |
1197 » » lclptr = (struct state *) malloc(sizeof *lclptr); | 1221 » » lclptr = malloc(sizeof *lclptr); |
1198 if (lclptr == NULL) { | 1222 if (lclptr == NULL) { |
1199 settzname(); /* all we can do */ | 1223 settzname(); /* all we can do */ |
1200 return; | 1224 return; |
1201 } | 1225 } |
1202 } | 1226 } |
1203 #endif /* defined ALL_STATE */ | 1227 #endif /* defined ALL_STATE */ |
1204 if (*name == '\0') { | 1228 if (*name == '\0') { |
1205 /* | 1229 /* |
1206 ** User wants it fast rather than right. | 1230 ** User wants it fast rather than right. |
1207 */ | 1231 */ |
(...skipping 14 matching lines...) Expand all Loading... |
1222 ** The easy way to behave "as if no library function calls" localtime | 1246 ** The easy way to behave "as if no library function calls" localtime |
1223 ** is to not call it--so we drop its guts into "localsub", which can be | 1247 ** is to not call it--so we drop its guts into "localsub", which can be |
1224 ** freely called. (And no, the PANS doesn't require the above behavior-- | 1248 ** freely called. (And no, the PANS doesn't require the above behavior-- |
1225 ** but it *is* desirable.) | 1249 ** but it *is* desirable.) |
1226 ** | 1250 ** |
1227 ** The unused offset argument is for the benefit of mktime variants. | 1251 ** The unused offset argument is for the benefit of mktime variants. |
1228 */ | 1252 */ |
1229 | 1253 |
1230 /*ARGSUSED*/ | 1254 /*ARGSUSED*/ |
1231 static struct tm * | 1255 static struct tm * |
1232 localsub(timep, offset, tmp) | 1256 localsub(const time_t *const timep, const int_fast32_t offset, |
1233 const time_t * const» timep; | 1257 » struct tm *const tmp) |
1234 const long» » offset; | |
1235 struct tm * const» tmp; | |
1236 { | 1258 { |
1237 register struct state * sp; | 1259 register struct state * sp; |
1238 register const struct ttinfo * ttisp; | 1260 register const struct ttinfo * ttisp; |
1239 register int i; | 1261 register int i; |
1240 register struct tm * result; | 1262 register struct tm * result; |
1241 const time_t t = *timep; | 1263 const time_t t = *timep; |
1242 | 1264 |
1243 sp = lclptr; | 1265 sp = lclptr; |
1244 #ifdef ALL_STATE | |
1245 if (sp == NULL) | 1266 if (sp == NULL) |
1246 return gmtsub(timep, offset, tmp); | 1267 return gmtsub(timep, offset, tmp); |
1247 #endif /* defined ALL_STATE */ | |
1248 if ((sp->goback && t < sp->ats[0]) || | 1268 if ((sp->goback && t < sp->ats[0]) || |
1249 (sp->goahead && t > sp->ats[sp->timecnt - 1])) { | 1269 (sp->goahead && t > sp->ats[sp->timecnt - 1])) { |
1250 time_t newt = t; | 1270 time_t newt = t; |
1251 register time_t seconds; | 1271 register time_t seconds; |
1252 » » » register time_t»» tcycles; | 1272 » » » register time_t»» years; |
1253 » » » register int_fast64_t» icycles; | |
1254 | 1273 |
1255 if (t < sp->ats[0]) | 1274 if (t < sp->ats[0]) |
1256 seconds = sp->ats[0] - t; | 1275 seconds = sp->ats[0] - t; |
1257 else seconds = t - sp->ats[sp->timecnt - 1]; | 1276 else seconds = t - sp->ats[sp->timecnt - 1]; |
1258 --seconds; | 1277 --seconds; |
1259 » » » tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR; | 1278 » » » years = (seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT; |
1260 » » » ++tcycles; | 1279 » » » seconds = years * AVGSECSPERYEAR; |
1261 » » » icycles = tcycles; | |
1262 » » » if (tcycles - icycles >= 1 || icycles - tcycles >= 1) | |
1263 » » » » return NULL; | |
1264 » » » seconds = icycles; | |
1265 » » » seconds *= YEARSPERREPEAT; | |
1266 » » » seconds *= AVGSECSPERYEAR; | |
1267 if (t < sp->ats[0]) | 1280 if (t < sp->ats[0]) |
1268 newt += seconds; | 1281 newt += seconds; |
1269 else newt -= seconds; | 1282 else newt -= seconds; |
1270 if (newt < sp->ats[0] || | 1283 if (newt < sp->ats[0] || |
1271 newt > sp->ats[sp->timecnt - 1]) | 1284 newt > sp->ats[sp->timecnt - 1]) |
1272 return NULL; /* "cannot happen" */ | 1285 return NULL; /* "cannot happen" */ |
1273 result = localsub(&newt, offset, tmp); | 1286 result = localsub(&newt, offset, tmp); |
1274 if (result == tmp) { | 1287 if (result == tmp) { |
1275 register time_t newy; | 1288 register time_t newy; |
1276 | 1289 |
1277 newy = tmp->tm_year; | 1290 newy = tmp->tm_year; |
1278 if (t < sp->ats[0]) | 1291 if (t < sp->ats[0]) |
1279 » » » » » newy -= icycles * YEARSPERREPEAT; | 1292 » » » » » newy -= years; |
1280 » » » » else» newy += icycles * YEARSPERREPEAT; | 1293 » » » » else» newy += years; |
1281 tmp->tm_year = newy; | 1294 tmp->tm_year = newy; |
1282 if (tmp->tm_year != newy) | 1295 if (tmp->tm_year != newy) |
1283 return NULL; | 1296 return NULL; |
1284 } | 1297 } |
1285 return result; | 1298 return result; |
1286 } | 1299 } |
1287 if (sp->timecnt == 0 || t < sp->ats[0]) { | 1300 if (sp->timecnt == 0 || t < sp->ats[0]) { |
1288 » » i = 0; | 1301 » » i = sp->defaulttype; |
1289 » » while (sp->ttis[i].tt_isdst) | |
1290 » » » if (++i >= sp->typecnt) { | |
1291 » » » » i = 0; | |
1292 » » » » break; | |
1293 » » » } | |
1294 } else { | 1302 } else { |
1295 register int lo = 1; | 1303 register int lo = 1; |
1296 register int hi = sp->timecnt; | 1304 register int hi = sp->timecnt; |
1297 | 1305 |
1298 while (lo < hi) { | 1306 while (lo < hi) { |
1299 register int mid = (lo + hi) >> 1; | 1307 register int mid = (lo + hi) >> 1; |
1300 | 1308 |
1301 if (t < sp->ats[mid]) | 1309 if (t < sp->ats[mid]) |
1302 hi = mid; | 1310 hi = mid; |
1303 else lo = mid + 1; | 1311 else lo = mid + 1; |
(...skipping 10 matching lines...) Expand all Loading... |
1314 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp); | 1322 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp); |
1315 tmp->tm_isdst = ttisp->tt_isdst; | 1323 tmp->tm_isdst = ttisp->tt_isdst; |
1316 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; | 1324 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind]; |
1317 #ifdef TM_ZONE | 1325 #ifdef TM_ZONE |
1318 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; | 1326 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind]; |
1319 #endif /* defined TM_ZONE */ | 1327 #endif /* defined TM_ZONE */ |
1320 return result; | 1328 return result; |
1321 } | 1329 } |
1322 | 1330 |
1323 struct tm * | 1331 struct tm * |
1324 localtime(timep) | 1332 localtime(const time_t *const timep) |
1325 const time_t * const» timep; | |
1326 { | 1333 { |
1327 tzset(); | 1334 tzset(); |
1328 return localsub(timep, 0L, &tm); | 1335 return localsub(timep, 0L, &tm); |
1329 } | 1336 } |
1330 | 1337 |
1331 /* | 1338 /* |
1332 ** Re-entrant version of localtime. | 1339 ** Re-entrant version of localtime. |
1333 */ | 1340 */ |
1334 | 1341 |
1335 struct tm * | 1342 struct tm * |
1336 localtime_r(timep, tmp) | 1343 localtime_r(const time_t *const timep, struct tm *tmp) |
1337 const time_t * const» timep; | |
1338 struct tm *» » tmp; | |
1339 { | 1344 { |
1340 return localsub(timep, 0L, tmp); | 1345 return localsub(timep, 0L, tmp); |
1341 } | 1346 } |
1342 | 1347 |
1343 /* | 1348 /* |
1344 ** gmtsub is to gmtime as localsub is to localtime. | 1349 ** gmtsub is to gmtime as localsub is to localtime. |
1345 */ | 1350 */ |
1346 | 1351 |
1347 static struct tm * | 1352 static struct tm * |
1348 gmtsub(timep, offset, tmp) | 1353 gmtsub(const time_t *const timep, const int_fast32_t offset, |
1349 const time_t * const» timep; | 1354 struct tm *const tmp) |
1350 const long» » offset; | |
1351 struct tm * const» tmp; | |
1352 { | 1355 { |
1353 register struct tm * result; | 1356 register struct tm * result; |
1354 | 1357 |
1355 if (!gmt_is_set) { | 1358 if (!gmt_is_set) { |
1356 gmt_is_set = TRUE; | 1359 gmt_is_set = TRUE; |
1357 #ifdef ALL_STATE | 1360 #ifdef ALL_STATE |
1358 » » gmtptr = (struct state *) malloc(sizeof *gmtptr); | 1361 » » gmtptr = malloc(sizeof *gmtptr); |
| 1362 #endif /* defined ALL_STATE */ |
1359 if (gmtptr != NULL) | 1363 if (gmtptr != NULL) |
1360 #endif /* defined ALL_STATE */ | |
1361 gmtload(gmtptr); | 1364 gmtload(gmtptr); |
1362 } | 1365 } |
1363 result = timesub(timep, offset, gmtptr, tmp); | 1366 result = timesub(timep, offset, gmtptr, tmp); |
1364 #ifdef TM_ZONE | 1367 #ifdef TM_ZONE |
1365 /* | 1368 /* |
1366 ** Could get fancy here and deliver something such as | 1369 ** Could get fancy here and deliver something such as |
1367 » ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero, | 1370 » ** "UT+xxxx" or "UT-xxxx" if offset is non-zero, |
1368 ** but this is no time for a treasure hunt. | 1371 ** but this is no time for a treasure hunt. |
1369 */ | 1372 */ |
1370 » if (offset != 0) | 1373 » tmp->TM_ZONE = offset ? wildabbr : gmtptr ? gmtptr->chars : gmt; |
1371 » » tmp->TM_ZONE = wildabbr; | |
1372 » else { | |
1373 #ifdef ALL_STATE | |
1374 » » if (gmtptr == NULL) | |
1375 » » » tmp->TM_ZONE = gmt; | |
1376 » » else» tmp->TM_ZONE = gmtptr->chars; | |
1377 #endif /* defined ALL_STATE */ | |
1378 #ifndef ALL_STATE | |
1379 » » tmp->TM_ZONE = gmtptr->chars; | |
1380 #endif /* State Farm */ | |
1381 » } | |
1382 #endif /* defined TM_ZONE */ | 1374 #endif /* defined TM_ZONE */ |
1383 return result; | 1375 return result; |
1384 } | 1376 } |
1385 | 1377 |
1386 struct tm * | 1378 struct tm * |
1387 gmtime(timep) | 1379 gmtime(const time_t *const timep) |
1388 const time_t * const» timep; | |
1389 { | 1380 { |
1390 return gmtsub(timep, 0L, &tm); | 1381 return gmtsub(timep, 0L, &tm); |
1391 } | 1382 } |
1392 | 1383 |
1393 /* | 1384 /* |
1394 * Re-entrant version of gmtime. | 1385 * Re-entrant version of gmtime. |
1395 */ | 1386 */ |
1396 | 1387 |
1397 struct tm * | 1388 struct tm * |
1398 gmtime_r(timep, tmp) | 1389 gmtime_r(const time_t *const timep, struct tm *tmp) |
1399 const time_t * const» timep; | |
1400 struct tm *» » tmp; | |
1401 { | 1390 { |
1402 return gmtsub(timep, 0L, tmp); | 1391 return gmtsub(timep, 0L, tmp); |
1403 } | 1392 } |
1404 | 1393 |
1405 #ifdef STD_INSPIRED | 1394 #ifdef STD_INSPIRED |
1406 | 1395 |
1407 struct tm * | 1396 struct tm * |
1408 offtime(timep, offset) | 1397 offtime(const time_t *const timep, const long offset) |
1409 const time_t * const» timep; | |
1410 const long» » offset; | |
1411 { | 1398 { |
1412 return gmtsub(timep, offset, &tm); | 1399 return gmtsub(timep, offset, &tm); |
1413 } | 1400 } |
1414 | 1401 |
1415 #endif /* defined STD_INSPIRED */ | 1402 #endif /* defined STD_INSPIRED */ |
1416 | 1403 |
1417 /* | 1404 /* |
1418 ** Return the number of leap years through the end of the given year | 1405 ** Return the number of leap years through the end of the given year |
1419 ** where, to make the math easy, the answer for year zero is defined as zero. | 1406 ** where, to make the math easy, the answer for year zero is defined as zero. |
1420 */ | 1407 */ |
1421 | 1408 |
1422 static int | 1409 static int |
1423 leaps_thru_end_of(y) | 1410 leaps_thru_end_of(register const int y) |
1424 register const int» y; | |
1425 { | 1411 { |
1426 return (y >= 0) ? (y / 4 - y / 100 + y / 400) : | 1412 return (y >= 0) ? (y / 4 - y / 100 + y / 400) : |
1427 -(leaps_thru_end_of(-(y + 1)) + 1); | 1413 -(leaps_thru_end_of(-(y + 1)) + 1); |
1428 } | 1414 } |
1429 | 1415 |
1430 static struct tm * | 1416 static struct tm * |
1431 timesub(timep, offset, sp, tmp) | 1417 timesub(const time_t *const timep, const int_fast32_t offset, |
1432 const time_t * const» » » timep; | 1418 » register const struct state *const sp, |
1433 const long» » » » offset; | 1419 » register struct tm *const tmp) |
1434 register const struct state * const» sp; | |
1435 register struct tm * const» » tmp; | |
1436 { | 1420 { |
1437 register const struct lsinfo * lp; | 1421 register const struct lsinfo * lp; |
1438 register time_t tdays; | 1422 register time_t tdays; |
1439 register int idays; /* unsigned would be so 2003 */ | 1423 register int idays; /* unsigned would be so 2003 */ |
1440 » register long» » » rem; | 1424 » register int_fast64_t» » rem; |
1441 int y; | 1425 int y; |
1442 register const int * ip; | 1426 register const int * ip; |
1443 » register long» » » corr; | 1427 » register int_fast64_t» » corr; |
1444 register int hit; | 1428 register int hit; |
1445 register int i; | 1429 register int i; |
1446 | 1430 |
1447 corr = 0; | 1431 corr = 0; |
1448 hit = 0; | 1432 hit = 0; |
1449 #ifdef ALL_STATE | |
1450 i = (sp == NULL) ? 0 : sp->leapcnt; | 1433 i = (sp == NULL) ? 0 : sp->leapcnt; |
1451 #endif /* defined ALL_STATE */ | |
1452 #ifndef ALL_STATE | |
1453 i = sp->leapcnt; | |
1454 #endif /* State Farm */ | |
1455 while (--i >= 0) { | 1434 while (--i >= 0) { |
1456 lp = &sp->lsis[i]; | 1435 lp = &sp->lsis[i]; |
1457 if (*timep >= lp->ls_trans) { | 1436 if (*timep >= lp->ls_trans) { |
1458 if (*timep == lp->ls_trans) { | 1437 if (*timep == lp->ls_trans) { |
1459 hit = ((i == 0 && lp->ls_corr > 0) || | 1438 hit = ((i == 0 && lp->ls_corr > 0) || |
1460 lp->ls_corr > sp->lsis[i - 1].ls_corr); | 1439 lp->ls_corr > sp->lsis[i - 1].ls_corr); |
1461 if (hit) | 1440 if (hit) |
1462 while (i > 0 && | 1441 while (i > 0 && |
1463 sp->lsis[i].ls_trans == | 1442 sp->lsis[i].ls_trans == |
1464 sp->lsis[i - 1].ls_trans + 1 && | 1443 sp->lsis[i - 1].ls_trans + 1 && |
(...skipping 10 matching lines...) Expand all Loading... |
1475 y = EPOCH_YEAR; | 1454 y = EPOCH_YEAR; |
1476 tdays = *timep / SECSPERDAY; | 1455 tdays = *timep / SECSPERDAY; |
1477 rem = *timep - tdays * SECSPERDAY; | 1456 rem = *timep - tdays * SECSPERDAY; |
1478 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) { | 1457 while (tdays < 0 || tdays >= year_lengths[isleap(y)]) { |
1479 int newy; | 1458 int newy; |
1480 register time_t tdelta; | 1459 register time_t tdelta; |
1481 register int idelta; | 1460 register int idelta; |
1482 register int leapdays; | 1461 register int leapdays; |
1483 | 1462 |
1484 tdelta = tdays / DAYSPERLYEAR; | 1463 tdelta = tdays / DAYSPERLYEAR; |
| 1464 if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta) |
| 1465 && tdelta <= INT_MAX)) |
| 1466 return NULL; |
1485 idelta = tdelta; | 1467 idelta = tdelta; |
1486 if (tdelta - idelta >= 1 || idelta - tdelta >= 1) | |
1487 return NULL; | |
1488 if (idelta == 0) | 1468 if (idelta == 0) |
1489 idelta = (tdays < 0) ? -1 : 1; | 1469 idelta = (tdays < 0) ? -1 : 1; |
1490 newy = y; | 1470 newy = y; |
1491 if (increment_overflow(&newy, idelta)) | 1471 if (increment_overflow(&newy, idelta)) |
1492 return NULL; | 1472 return NULL; |
1493 leapdays = leaps_thru_end_of(newy - 1) - | 1473 leapdays = leaps_thru_end_of(newy - 1) - |
1494 leaps_thru_end_of(y - 1); | 1474 leaps_thru_end_of(y - 1); |
1495 tdays -= ((time_t) newy - y) * DAYSPERNYEAR; | 1475 tdays -= ((time_t) newy - y) * DAYSPERNYEAR; |
1496 tdays -= leapdays; | 1476 tdays -= leapdays; |
1497 y = newy; | 1477 y = newy; |
1498 } | 1478 } |
1499 { | 1479 { |
1500 » » register long» seconds; | 1480 » » register int_fast32_t» seconds; |
1501 | 1481 |
1502 » » seconds = tdays * SECSPERDAY + 0.5; | 1482 » » seconds = tdays * SECSPERDAY; |
1503 tdays = seconds / SECSPERDAY; | 1483 tdays = seconds / SECSPERDAY; |
1504 rem += seconds - tdays * SECSPERDAY; | 1484 rem += seconds - tdays * SECSPERDAY; |
1505 } | 1485 } |
1506 /* | 1486 /* |
1507 ** Given the range, we can now fearlessly cast... | 1487 ** Given the range, we can now fearlessly cast... |
1508 */ | 1488 */ |
1509 idays = tdays; | 1489 idays = tdays; |
1510 rem += offset - corr; | 1490 rem += offset - corr; |
1511 while (rem < 0) { | 1491 while (rem < 0) { |
1512 rem += SECSPERDAY; | 1492 rem += SECSPERDAY; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1555 idays -= ip[tmp->tm_mon]; | 1535 idays -= ip[tmp->tm_mon]; |
1556 tmp->tm_mday = (int) (idays + 1); | 1536 tmp->tm_mday = (int) (idays + 1); |
1557 tmp->tm_isdst = 0; | 1537 tmp->tm_isdst = 0; |
1558 #ifdef TM_GMTOFF | 1538 #ifdef TM_GMTOFF |
1559 tmp->TM_GMTOFF = offset; | 1539 tmp->TM_GMTOFF = offset; |
1560 #endif /* defined TM_GMTOFF */ | 1540 #endif /* defined TM_GMTOFF */ |
1561 return tmp; | 1541 return tmp; |
1562 } | 1542 } |
1563 | 1543 |
1564 char * | 1544 char * |
1565 ctime(timep) | 1545 ctime(const time_t *const timep) |
1566 const time_t * const» timep; | |
1567 { | 1546 { |
1568 /* | 1547 /* |
1569 ** Section 4.12.3.2 of X3.159-1989 requires that | 1548 ** Section 4.12.3.2 of X3.159-1989 requires that |
1570 ** The ctime function converts the calendar time pointed to by timer | 1549 ** The ctime function converts the calendar time pointed to by timer |
1571 ** to local time in the form of a string. It is equivalent to | 1550 ** to local time in the form of a string. It is equivalent to |
1572 ** asctime(localtime(timer)) | 1551 ** asctime(localtime(timer)) |
1573 */ | 1552 */ |
1574 return asctime(localtime(timep)); | 1553 return asctime(localtime(timep)); |
1575 } | 1554 } |
1576 | 1555 |
1577 char * | 1556 char * |
1578 ctime_r(timep, buf) | 1557 ctime_r(const time_t *const timep, char *buf) |
1579 const time_t * const» timep; | |
1580 char *» » » buf; | |
1581 { | 1558 { |
1582 struct tm mytm; | 1559 struct tm mytm; |
1583 | 1560 |
1584 return asctime_r(localtime_r(timep, &mytm), buf); | 1561 return asctime_r(localtime_r(timep, &mytm), buf); |
1585 } | 1562 } |
1586 | 1563 |
1587 /* | 1564 /* |
1588 ** Adapted from code provided by Robert Elz, who writes: | 1565 ** Adapted from code provided by Robert Elz, who writes: |
1589 ** The "best" way to do mktime I think is based on an idea of Bob | 1566 ** The "best" way to do mktime I think is based on an idea of Bob |
1590 ** Kridle's (so its said...) from a long time ago. | 1567 ** Kridle's (so its said...) from a long time ago. |
1591 ** It does a binary search of the time_t space. Since time_t's are | 1568 ** It does a binary search of the time_t space. Since time_t's are |
1592 ** just 32 bits, its a max of 32 iterations (even at 64 bits it | 1569 ** just 32 bits, its a max of 32 iterations (even at 64 bits it |
1593 ** would still be very reasonable). | 1570 ** would still be very reasonable). |
1594 */ | 1571 */ |
1595 | 1572 |
1596 #ifndef WRONG | 1573 #ifndef WRONG |
1597 #define WRONG (-1) | 1574 #define WRONG (-1) |
1598 #endif /* !defined WRONG */ | 1575 #endif /* !defined WRONG */ |
1599 | 1576 |
1600 /* | 1577 /* |
1601 ** Simplified normalize logic courtesy Paul Eggert. | 1578 ** Normalize logic courtesy Paul Eggert. |
1602 */ | 1579 */ |
1603 | 1580 |
1604 static int | 1581 static int |
1605 increment_overflow(number, delta) | 1582 increment_overflow(int *const ip, int j) |
1606 int *» number; | |
1607 int» delta; | |
1608 { | 1583 { |
1609 » int» number0; | 1584 » register int const» i = *ip; |
1610 | 1585 |
1611 » number0 = *number; | 1586 » /* |
1612 » *number += delta; | 1587 » ** If i >= 0 there can only be overflow if i + j > INT_MAX |
1613 » return (*number < number0) != (delta < 0); | 1588 » ** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow. |
| 1589 » ** If i < 0 there can only be overflow if i + j < INT_MIN |
| 1590 » ** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow. |
| 1591 » */ |
| 1592 » if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i)) |
| 1593 » » return TRUE; |
| 1594 » *ip += j; |
| 1595 » return FALSE; |
1614 } | 1596 } |
1615 | 1597 |
1616 static int | 1598 static int |
1617 long_increment_overflow(number, delta) | 1599 increment_overflow32(int_fast32_t *const lp, int const m) |
1618 long *» number; | |
1619 int» delta; | |
1620 { | 1600 { |
1621 » long» number0; | 1601 » register int_fast32_t const» l = *lp; |
1622 | 1602 |
1623 » number0 = *number; | 1603 » if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l)) |
1624 » *number += delta; | 1604 » » return TRUE; |
1625 » return (*number < number0) != (delta < 0); | 1605 » *lp += m; |
| 1606 » return FALSE; |
1626 } | 1607 } |
1627 | 1608 |
1628 static int | 1609 static int |
1629 normalize_overflow(tensptr, unitsptr, base) | 1610 increment_overflow_time(time_t *tp, int_fast32_t j) |
1630 int * const» tensptr; | 1611 { |
1631 int * const» unitsptr; | 1612 » /* |
1632 const int» base; | 1613 » ** This is like |
| 1614 » ** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...', |
| 1615 » ** except that it does the right thing even if *tp + j would overflow. |
| 1616 » */ |
| 1617 » if (! (j < 0 |
| 1618 » ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp) |
| 1619 » : *tp <= time_t_max - j)) |
| 1620 » » return TRUE; |
| 1621 » *tp += j; |
| 1622 » return FALSE; |
| 1623 } |
| 1624 |
| 1625 static int |
| 1626 normalize_overflow(int *const tensptr, int *const unitsptr, const int base) |
1633 { | 1627 { |
1634 register int tensdelta; | 1628 register int tensdelta; |
1635 | 1629 |
1636 tensdelta = (*unitsptr >= 0) ? | 1630 tensdelta = (*unitsptr >= 0) ? |
1637 (*unitsptr / base) : | 1631 (*unitsptr / base) : |
1638 (-1 - (-1 - *unitsptr) / base); | 1632 (-1 - (-1 - *unitsptr) / base); |
1639 *unitsptr -= tensdelta * base; | 1633 *unitsptr -= tensdelta * base; |
1640 return increment_overflow(tensptr, tensdelta); | 1634 return increment_overflow(tensptr, tensdelta); |
1641 } | 1635 } |
1642 | 1636 |
1643 static int | 1637 static int |
1644 long_normalize_overflow(tensptr, unitsptr, base) | 1638 normalize_overflow32(int_fast32_t *const tensptr, int *const unitsptr, |
1645 long * const» tensptr; | 1639 » » const int base) |
1646 int * const» unitsptr; | |
1647 const int» base; | |
1648 { | 1640 { |
1649 register int tensdelta; | 1641 register int tensdelta; |
1650 | 1642 |
1651 tensdelta = (*unitsptr >= 0) ? | 1643 tensdelta = (*unitsptr >= 0) ? |
1652 (*unitsptr / base) : | 1644 (*unitsptr / base) : |
1653 (-1 - (-1 - *unitsptr) / base); | 1645 (-1 - (-1 - *unitsptr) / base); |
1654 *unitsptr -= tensdelta * base; | 1646 *unitsptr -= tensdelta * base; |
1655 » return long_increment_overflow(tensptr, tensdelta); | 1647 » return increment_overflow32(tensptr, tensdelta); |
1656 } | 1648 } |
1657 | 1649 |
1658 static int | 1650 static int |
1659 tmcomp(atmp, btmp) | 1651 tmcomp(register const struct tm *const atmp, |
1660 register const struct tm * const atmp; | 1652 register const struct tm *const btmp) |
1661 register const struct tm * const btmp; | |
1662 { | 1653 { |
1663 register int result; | 1654 register int result; |
1664 | 1655 |
1665 » if ((result = (atmp->tm_year - btmp->tm_year)) == 0 && | 1656 » if (atmp->tm_year != btmp->tm_year) |
1666 » » (result = (atmp->tm_mon - btmp->tm_mon)) == 0 && | 1657 » » return atmp->tm_year < btmp->tm_year ? -1 : 1; |
| 1658 » if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 && |
1667 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && | 1659 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 && |
1668 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && | 1660 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 && |
1669 (result = (atmp->tm_min - btmp->tm_min)) == 0) | 1661 (result = (atmp->tm_min - btmp->tm_min)) == 0) |
1670 result = atmp->tm_sec - btmp->tm_sec; | 1662 result = atmp->tm_sec - btmp->tm_sec; |
1671 return result; | 1663 return result; |
1672 } | 1664 } |
1673 | 1665 |
1674 static time_t | 1666 static time_t |
1675 time2sub(tmp, funcp, offset, okayp, do_norm_secs) | 1667 time2sub(struct tm *const tmp, |
1676 struct tm * const» tmp; | 1668 » struct tm *(*const funcp)(const time_t *, int_fast32_t, struct tm *), |
1677 struct tm * (* const» funcp)(const time_t*, long, struct tm*); | 1669 » const int_fast32_t offset, |
1678 const long» » offset; | 1670 » int *const okayp, |
1679 int * const» » okayp; | 1671 » const int do_norm_secs) |
1680 const int» » do_norm_secs; | |
1681 { | 1672 { |
1682 register const struct state * sp; | 1673 register const struct state * sp; |
1683 register int dir; | 1674 register int dir; |
1684 register int i, j; | 1675 register int i, j; |
1685 register int saved_seconds; | 1676 register int saved_seconds; |
1686 » register long» » » li; | 1677 » register int_fast32_t» » » li; |
1687 register time_t lo; | 1678 register time_t lo; |
1688 register time_t hi; | 1679 register time_t hi; |
1689 » long» » » » y; | 1680 » int_fast32_t» » » » y; |
1690 time_t newt; | 1681 time_t newt; |
1691 time_t t; | 1682 time_t t; |
1692 struct tm yourtm, mytm; | 1683 struct tm yourtm, mytm; |
1693 | 1684 |
1694 *okayp = FALSE; | 1685 *okayp = FALSE; |
1695 yourtm = *tmp; | 1686 yourtm = *tmp; |
1696 if (do_norm_secs) { | 1687 if (do_norm_secs) { |
1697 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec, | 1688 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec, |
1698 SECSPERMIN)) | 1689 SECSPERMIN)) |
1699 return WRONG; | 1690 return WRONG; |
1700 } | 1691 } |
1701 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR)) | 1692 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR)) |
1702 return WRONG; | 1693 return WRONG; |
1703 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY)) | 1694 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY)) |
1704 return WRONG; | 1695 return WRONG; |
1705 y = yourtm.tm_year; | 1696 y = yourtm.tm_year; |
1706 » if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR)) | 1697 » if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR)) |
1707 return WRONG; | 1698 return WRONG; |
1708 /* | 1699 /* |
1709 ** Turn y into an actual year number for now. | 1700 ** Turn y into an actual year number for now. |
1710 ** It is converted back to an offset from TM_YEAR_BASE later. | 1701 ** It is converted back to an offset from TM_YEAR_BASE later. |
1711 */ | 1702 */ |
1712 » if (long_increment_overflow(&y, TM_YEAR_BASE)) | 1703 » if (increment_overflow32(&y, TM_YEAR_BASE)) |
1713 return WRONG; | 1704 return WRONG; |
1714 while (yourtm.tm_mday <= 0) { | 1705 while (yourtm.tm_mday <= 0) { |
1715 » » if (long_increment_overflow(&y, -1)) | 1706 » » if (increment_overflow32(&y, -1)) |
1716 return WRONG; | 1707 return WRONG; |
1717 li = y + (1 < yourtm.tm_mon); | 1708 li = y + (1 < yourtm.tm_mon); |
1718 yourtm.tm_mday += year_lengths[isleap(li)]; | 1709 yourtm.tm_mday += year_lengths[isleap(li)]; |
1719 } | 1710 } |
1720 while (yourtm.tm_mday > DAYSPERLYEAR) { | 1711 while (yourtm.tm_mday > DAYSPERLYEAR) { |
1721 li = y + (1 < yourtm.tm_mon); | 1712 li = y + (1 < yourtm.tm_mon); |
1722 yourtm.tm_mday -= year_lengths[isleap(li)]; | 1713 yourtm.tm_mday -= year_lengths[isleap(li)]; |
1723 » » if (long_increment_overflow(&y, 1)) | 1714 » » if (increment_overflow32(&y, 1)) |
1724 return WRONG; | 1715 return WRONG; |
1725 } | 1716 } |
1726 for ( ; ; ) { | 1717 for ( ; ; ) { |
1727 i = mon_lengths[isleap(y)][yourtm.tm_mon]; | 1718 i = mon_lengths[isleap(y)][yourtm.tm_mon]; |
1728 if (yourtm.tm_mday <= i) | 1719 if (yourtm.tm_mday <= i) |
1729 break; | 1720 break; |
1730 yourtm.tm_mday -= i; | 1721 yourtm.tm_mday -= i; |
1731 if (++yourtm.tm_mon >= MONSPERYEAR) { | 1722 if (++yourtm.tm_mon >= MONSPERYEAR) { |
1732 yourtm.tm_mon = 0; | 1723 yourtm.tm_mon = 0; |
1733 » » » if (long_increment_overflow(&y, 1)) | 1724 » » » if (increment_overflow32(&y, 1)) |
1734 return WRONG; | 1725 return WRONG; |
1735 } | 1726 } |
1736 } | 1727 } |
1737 » if (long_increment_overflow(&y, -TM_YEAR_BASE)) | 1728 » if (increment_overflow32(&y, -TM_YEAR_BASE)) |
1738 return WRONG; | 1729 return WRONG; |
1739 yourtm.tm_year = y; | 1730 yourtm.tm_year = y; |
1740 if (yourtm.tm_year != y) | 1731 if (yourtm.tm_year != y) |
1741 return WRONG; | 1732 return WRONG; |
1742 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN) | 1733 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN) |
1743 saved_seconds = 0; | 1734 saved_seconds = 0; |
1744 else if (y + TM_YEAR_BASE < EPOCH_YEAR) { | 1735 else if (y + TM_YEAR_BASE < EPOCH_YEAR) { |
1745 /* | 1736 /* |
1746 ** We can't set tm_sec to 0, because that might push the | 1737 ** We can't set tm_sec to 0, because that might push the |
1747 ** time below the minimum representable time. | 1738 ** time below the minimum representable time. |
1748 ** Set tm_sec to 59 instead. | 1739 ** Set tm_sec to 59 instead. |
1749 ** This assumes that the minimum representable time is | 1740 ** This assumes that the minimum representable time is |
1750 ** not in the same minute that a leap second was deleted from, | 1741 ** not in the same minute that a leap second was deleted from, |
1751 ** which is a safer assumption than using 58 would be. | 1742 ** which is a safer assumption than using 58 would be. |
1752 */ | 1743 */ |
1753 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN)) | 1744 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN)) |
1754 return WRONG; | 1745 return WRONG; |
1755 saved_seconds = yourtm.tm_sec; | 1746 saved_seconds = yourtm.tm_sec; |
1756 yourtm.tm_sec = SECSPERMIN - 1; | 1747 yourtm.tm_sec = SECSPERMIN - 1; |
1757 } else { | 1748 } else { |
1758 saved_seconds = yourtm.tm_sec; | 1749 saved_seconds = yourtm.tm_sec; |
1759 yourtm.tm_sec = 0; | 1750 yourtm.tm_sec = 0; |
1760 } | 1751 } |
1761 /* | 1752 /* |
1762 ** Do a binary search (this works whatever time_t's type is). | 1753 ** Do a binary search (this works whatever time_t's type is). |
1763 */ | 1754 */ |
1764 if (!TYPE_SIGNED(time_t)) { | 1755 if (!TYPE_SIGNED(time_t)) { |
1765 lo = 0; | 1756 lo = 0; |
1766 hi = lo - 1; | 1757 hi = lo - 1; |
1767 } else if (!TYPE_INTEGRAL(time_t)) { | |
1768 if (sizeof(time_t) > sizeof(float)) | |
1769 hi = (time_t) DBL_MAX; | |
1770 else hi = (time_t) FLT_MAX; | |
1771 lo = -hi; | |
1772 } else { | 1758 } else { |
1773 lo = 1; | 1759 lo = 1; |
1774 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i) | 1760 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i) |
1775 lo *= 2; | 1761 lo *= 2; |
1776 hi = -(lo + 1); | 1762 hi = -(lo + 1); |
1777 } | 1763 } |
1778 for ( ; ; ) { | 1764 for ( ; ; ) { |
1779 t = lo / 2 + hi / 2; | 1765 t = lo / 2 + hi / 2; |
1780 if (t < lo) | 1766 if (t < lo) |
1781 t = lo; | 1767 t = lo; |
1782 else if (t > hi) | 1768 else if (t > hi) |
1783 t = hi; | 1769 t = hi; |
1784 if ((*funcp)(&t, offset, &mytm) == NULL) { | 1770 if ((*funcp)(&t, offset, &mytm) == NULL) { |
1785 /* | 1771 /* |
1786 ** Assume that t is too extreme to be represented in | 1772 ** Assume that t is too extreme to be represented in |
1787 ** a struct tm; arrange things so that it is less | 1773 ** a struct tm; arrange things so that it is less |
1788 ** extreme on the next pass. | 1774 ** extreme on the next pass. |
1789 */ | 1775 */ |
1790 dir = (t > 0) ? 1 : -1; | 1776 dir = (t > 0) ? 1 : -1; |
1791 } else dir = tmcomp(&mytm, &yourtm); | 1777 } else dir = tmcomp(&mytm, &yourtm); |
1792 if (dir != 0) { | 1778 if (dir != 0) { |
1793 if (t == lo) { | 1779 if (t == lo) { |
| 1780 if (t == time_t_max) |
| 1781 return WRONG; |
1794 ++t; | 1782 ++t; |
1795 if (t <= lo) | |
1796 return WRONG; | |
1797 ++lo; | 1783 ++lo; |
1798 } else if (t == hi) { | 1784 } else if (t == hi) { |
| 1785 if (t == time_t_min) |
| 1786 return WRONG; |
1799 --t; | 1787 --t; |
1800 if (t >= hi) | |
1801 return WRONG; | |
1802 --hi; | 1788 --hi; |
1803 } | 1789 } |
1804 if (lo > hi) | 1790 if (lo > hi) |
1805 return WRONG; | 1791 return WRONG; |
1806 if (dir > 0) | 1792 if (dir > 0) |
1807 hi = t; | 1793 hi = t; |
1808 else lo = t; | 1794 else lo = t; |
1809 continue; | 1795 continue; |
1810 } | 1796 } |
1811 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) | 1797 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst) |
1812 break; | 1798 break; |
1813 /* | 1799 /* |
1814 ** Right time, wrong type. | 1800 ** Right time, wrong type. |
1815 ** Hunt for right time, right type. | 1801 ** Hunt for right time, right type. |
1816 ** It's okay to guess wrong since the guess | 1802 ** It's okay to guess wrong since the guess |
1817 ** gets checked. | 1803 ** gets checked. |
1818 */ | 1804 */ |
1819 sp = (const struct state *) | 1805 sp = (const struct state *) |
1820 ((funcp == localsub) ? lclptr : gmtptr); | 1806 ((funcp == localsub) ? lclptr : gmtptr); |
1821 #ifdef ALL_STATE | |
1822 if (sp == NULL) | 1807 if (sp == NULL) |
1823 return WRONG; | 1808 return WRONG; |
1824 #endif /* defined ALL_STATE */ | |
1825 for (i = sp->typecnt - 1; i >= 0; --i) { | 1809 for (i = sp->typecnt - 1; i >= 0; --i) { |
1826 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) | 1810 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst) |
1827 continue; | 1811 continue; |
1828 for (j = sp->typecnt - 1; j >= 0; --j) { | 1812 for (j = sp->typecnt - 1; j >= 0; --j) { |
1829 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) | 1813 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst) |
1830 continue; | 1814 continue; |
1831 newt = t + sp->ttis[j].tt_gmtoff - | 1815 newt = t + sp->ttis[j].tt_gmtoff - |
1832 sp->ttis[i].tt_gmtoff; | 1816 sp->ttis[i].tt_gmtoff; |
1833 if ((*funcp)(&newt, offset, &mytm) == NULL) | 1817 if ((*funcp)(&newt, offset, &mytm) == NULL) |
1834 continue; | 1818 continue; |
(...skipping 14 matching lines...) Expand all Loading... |
1849 newt = t + saved_seconds; | 1833 newt = t + saved_seconds; |
1850 if ((newt < t) != (saved_seconds < 0)) | 1834 if ((newt < t) != (saved_seconds < 0)) |
1851 return WRONG; | 1835 return WRONG; |
1852 t = newt; | 1836 t = newt; |
1853 if ((*funcp)(&t, offset, tmp)) | 1837 if ((*funcp)(&t, offset, tmp)) |
1854 *okayp = TRUE; | 1838 *okayp = TRUE; |
1855 return t; | 1839 return t; |
1856 } | 1840 } |
1857 | 1841 |
1858 static time_t | 1842 static time_t |
1859 time2(tmp, funcp, offset, okayp) | 1843 time2(struct tm * const»tmp, |
1860 struct tm * const» tmp; | 1844 struct tm * (*const funcp)(const time_t *, int_fast32_t, struct tm *), |
1861 struct tm * (* const» funcp)(const time_t*, long, struct tm*); | 1845 const int_fast32_t offset, |
1862 const long» » offset; | 1846 int *const okayp) |
1863 int * const» » okayp; | |
1864 { | 1847 { |
1865 time_t t; | 1848 time_t t; |
1866 | 1849 |
1867 /* | 1850 /* |
1868 ** First try without normalization of seconds | 1851 ** First try without normalization of seconds |
1869 ** (in case tm_sec contains a value associated with a leap second). | 1852 ** (in case tm_sec contains a value associated with a leap second). |
1870 ** If that fails, try with normalization of seconds. | 1853 ** If that fails, try with normalization of seconds. |
1871 */ | 1854 */ |
1872 t = time2sub(tmp, funcp, offset, okayp, FALSE); | 1855 t = time2sub(tmp, funcp, offset, okayp, FALSE); |
1873 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE); | 1856 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE); |
1874 } | 1857 } |
1875 | 1858 |
1876 static time_t | 1859 static time_t |
1877 time1(tmp, funcp, offset) | 1860 time1(struct tm *const tmp, |
1878 struct tm * const» tmp; | 1861 struct tm *(*const funcp) (const time_t *, int_fast32_t, struct tm *), |
1879 struct tm * (* const» funcp)(const time_t *, long, struct tm *); | 1862 const int_fast32_t offset) |
1880 const long» » offset; | |
1881 { | 1863 { |
1882 register time_t t; | 1864 register time_t t; |
1883 register const struct state * sp; | 1865 register const struct state * sp; |
1884 register int samei, otheri; | 1866 register int samei, otheri; |
1885 register int sameind, otherind; | 1867 register int sameind, otherind; |
1886 register int i; | 1868 register int i; |
1887 register int nseen; | 1869 register int nseen; |
1888 int seen[TZ_MAX_TYPES]; | 1870 int seen[TZ_MAX_TYPES]; |
1889 int types[TZ_MAX_TYPES]; | 1871 int types[TZ_MAX_TYPES]; |
1890 int okay; | 1872 int okay; |
1891 | 1873 |
| 1874 if (tmp == NULL) { |
| 1875 errno = EINVAL; |
| 1876 return WRONG; |
| 1877 } |
1892 if (tmp->tm_isdst > 1) | 1878 if (tmp->tm_isdst > 1) |
1893 tmp->tm_isdst = 1; | 1879 tmp->tm_isdst = 1; |
1894 t = time2(tmp, funcp, offset, &okay); | 1880 t = time2(tmp, funcp, offset, &okay); |
1895 #ifdef PCTS | |
1896 /* | |
1897 ** PCTS code courtesy Grant Sullivan. | |
1898 */ | |
1899 if (okay) | 1881 if (okay) |
1900 return t; | 1882 return t; |
1901 if (tmp->tm_isdst < 0) | 1883 if (tmp->tm_isdst < 0) |
| 1884 #ifdef PCTS |
| 1885 /* |
| 1886 ** POSIX Conformance Test Suite code courtesy Grant Sullivan. |
| 1887 */ |
1902 tmp->tm_isdst = 0; /* reset to std and try again */ | 1888 tmp->tm_isdst = 0; /* reset to std and try again */ |
1903 #endif /* defined PCTS */ | 1889 #else |
1904 #ifndef PCTS | |
1905 » if (okay || tmp->tm_isdst < 0) | |
1906 return t; | 1890 return t; |
1907 #endif /* !defined PCTS */ | 1891 #endif /* !defined PCTS */ |
1908 /* | 1892 /* |
1909 ** We're supposed to assume that somebody took a time of one type | 1893 ** We're supposed to assume that somebody took a time of one type |
1910 ** and did some math on it that yielded a "struct tm" that's bad. | 1894 ** and did some math on it that yielded a "struct tm" that's bad. |
1911 ** We try to divine the type they started from and adjust to the | 1895 ** We try to divine the type they started from and adjust to the |
1912 ** type they need. | 1896 ** type they need. |
1913 */ | 1897 */ |
1914 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr); | 1898 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr); |
1915 #ifdef ALL_STATE | |
1916 if (sp == NULL) | 1899 if (sp == NULL) |
1917 return WRONG; | 1900 return WRONG; |
1918 #endif /* defined ALL_STATE */ | |
1919 for (i = 0; i < sp->typecnt; ++i) | 1901 for (i = 0; i < sp->typecnt; ++i) |
1920 seen[i] = FALSE; | 1902 seen[i] = FALSE; |
1921 nseen = 0; | 1903 nseen = 0; |
1922 for (i = sp->timecnt - 1; i >= 0; --i) | 1904 for (i = sp->timecnt - 1; i >= 0; --i) |
1923 if (!seen[sp->types[i]]) { | 1905 if (!seen[sp->types[i]]) { |
1924 seen[sp->types[i]] = TRUE; | 1906 seen[sp->types[i]] = TRUE; |
1925 types[nseen++] = sp->types[i]; | 1907 types[nseen++] = sp->types[i]; |
1926 } | 1908 } |
1927 for (sameind = 0; sameind < nseen; ++sameind) { | 1909 for (sameind = 0; sameind < nseen; ++sameind) { |
1928 samei = types[sameind]; | 1910 samei = types[sameind]; |
(...skipping 11 matching lines...) Expand all Loading... |
1940 return t; | 1922 return t; |
1941 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - | 1923 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff - |
1942 sp->ttis[samei].tt_gmtoff; | 1924 sp->ttis[samei].tt_gmtoff; |
1943 tmp->tm_isdst = !tmp->tm_isdst; | 1925 tmp->tm_isdst = !tmp->tm_isdst; |
1944 } | 1926 } |
1945 } | 1927 } |
1946 return WRONG; | 1928 return WRONG; |
1947 } | 1929 } |
1948 | 1930 |
1949 time_t | 1931 time_t |
1950 mktime(tmp) | 1932 mktime(struct tm *const tmp) |
1951 struct tm * const» tmp; | |
1952 { | 1933 { |
1953 tzset(); | 1934 tzset(); |
1954 return time1(tmp, localsub, 0L); | 1935 return time1(tmp, localsub, 0L); |
1955 } | 1936 } |
1956 | 1937 |
1957 #ifdef STD_INSPIRED | 1938 #ifdef STD_INSPIRED |
1958 | 1939 |
1959 time_t | 1940 time_t |
1960 timelocal(tmp) | 1941 timelocal(struct tm *const tmp) |
1961 struct tm * const» tmp; | |
1962 { | 1942 { |
1963 » tmp->tm_isdst = -1;» /* in case it wasn't initialized */ | 1943 » if (tmp != NULL) |
| 1944 » » tmp->tm_isdst = -1;» /* in case it wasn't initialized */ |
1964 return mktime(tmp); | 1945 return mktime(tmp); |
1965 } | 1946 } |
1966 | 1947 |
1967 time_t | 1948 time_t |
1968 timegm(tmp) | 1949 timegm(struct tm *const tmp) |
1969 struct tm * const» tmp; | |
1970 { | 1950 { |
1971 » tmp->tm_isdst = 0; | 1951 » if (tmp != NULL) |
| 1952 » » tmp->tm_isdst = 0; |
1972 return time1(tmp, gmtsub, 0L); | 1953 return time1(tmp, gmtsub, 0L); |
1973 } | 1954 } |
1974 | 1955 |
1975 time_t | 1956 time_t |
1976 timeoff(tmp, offset) | 1957 timeoff(struct tm *const tmp, const long offset) |
1977 struct tm * const» tmp; | |
1978 const long» » offset; | |
1979 { | 1958 { |
1980 » tmp->tm_isdst = 0; | 1959 » if (tmp != NULL) |
| 1960 » » tmp->tm_isdst = 0; |
1981 return time1(tmp, gmtsub, offset); | 1961 return time1(tmp, gmtsub, offset); |
1982 } | 1962 } |
1983 | 1963 |
1984 #endif /* defined STD_INSPIRED */ | 1964 #endif /* defined STD_INSPIRED */ |
1985 | 1965 |
1986 #ifdef CMUCS | 1966 #ifdef CMUCS |
1987 | 1967 |
1988 /* | 1968 /* |
1989 ** The following is supplied for compatibility with | 1969 ** The following is supplied for compatibility with |
1990 ** previous versions of the CMUCS runtime library. | 1970 ** previous versions of the CMUCS runtime library. |
1991 */ | 1971 */ |
1992 | 1972 |
1993 long | 1973 long |
1994 gtime(tmp) | 1974 gtime(struct tm *const tmp) |
1995 struct tm * const» tmp; | |
1996 { | 1975 { |
1997 const time_t t = mktime(tmp); | 1976 const time_t t = mktime(tmp); |
1998 | 1977 |
1999 if (t == WRONG) | 1978 if (t == WRONG) |
2000 return -1; | 1979 return -1; |
2001 return t; | 1980 return t; |
2002 } | 1981 } |
2003 | 1982 |
2004 #endif /* defined CMUCS */ | 1983 #endif /* defined CMUCS */ |
2005 | 1984 |
2006 /* | 1985 /* |
2007 ** XXX--is the below the right way to conditionalize?? | 1986 ** XXX--is the below the right way to conditionalize?? |
2008 */ | 1987 */ |
2009 | 1988 |
2010 #ifdef STD_INSPIRED | 1989 #ifdef STD_INSPIRED |
2011 | 1990 |
2012 /* | 1991 /* |
2013 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599 | 1992 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599 |
2014 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which | 1993 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which |
2015 ** is not the case if we are accounting for leap seconds. | 1994 ** is not the case if we are accounting for leap seconds. |
2016 ** So, we provide the following conversion routines for use | 1995 ** So, we provide the following conversion routines for use |
2017 ** when exchanging timestamps with POSIX conforming systems. | 1996 ** when exchanging timestamps with POSIX conforming systems. |
2018 */ | 1997 */ |
2019 | 1998 |
2020 static long | 1999 static int_fast64_t |
2021 leapcorr(timep) | 2000 leapcorr(time_t *timep) |
2022 time_t *» timep; | |
2023 { | 2001 { |
2024 register struct state * sp; | 2002 register struct state * sp; |
2025 register struct lsinfo * lp; | 2003 register struct lsinfo * lp; |
2026 register int i; | 2004 register int i; |
2027 | 2005 |
2028 sp = lclptr; | 2006 sp = lclptr; |
2029 i = sp->leapcnt; | 2007 i = sp->leapcnt; |
2030 while (--i >= 0) { | 2008 while (--i >= 0) { |
2031 lp = &sp->lsis[i]; | 2009 lp = &sp->lsis[i]; |
2032 if (*timep >= lp->ls_trans) | 2010 if (*timep >= lp->ls_trans) |
2033 return lp->ls_corr; | 2011 return lp->ls_corr; |
2034 } | 2012 } |
2035 return 0; | 2013 return 0; |
2036 } | 2014 } |
2037 | 2015 |
2038 time_t | 2016 time_t |
2039 time2posix(t) | 2017 time2posix(time_t t) |
2040 time_t» t; | |
2041 { | 2018 { |
2042 tzset(); | 2019 tzset(); |
2043 return t - leapcorr(&t); | 2020 return t - leapcorr(&t); |
2044 } | 2021 } |
2045 | 2022 |
2046 time_t | 2023 time_t |
2047 posix2time(t) | 2024 posix2time(time_t t) |
2048 time_t» t; | |
2049 { | 2025 { |
2050 time_t x; | 2026 time_t x; |
2051 time_t y; | 2027 time_t y; |
2052 | 2028 |
2053 tzset(); | 2029 tzset(); |
2054 /* | 2030 /* |
2055 ** For a positive leap second hit, the result | 2031 ** For a positive leap second hit, the result |
2056 ** is not unique. For a negative leap second | 2032 ** is not unique. For a negative leap second |
2057 ** hit, the corresponding time doesn't exist, | 2033 ** hit, the corresponding time doesn't exist, |
2058 ** so we return an adjacent second. | 2034 ** so we return an adjacent second. |
(...skipping 12 matching lines...) Expand all Loading... |
2071 --x; | 2047 --x; |
2072 y = x - leapcorr(&x); | 2048 y = x - leapcorr(&x); |
2073 } while (y > t); | 2049 } while (y > t); |
2074 if (t != y) | 2050 if (t != y) |
2075 return x + 1; | 2051 return x + 1; |
2076 } | 2052 } |
2077 return x; | 2053 return x; |
2078 } | 2054 } |
2079 | 2055 |
2080 #endif /* defined STD_INSPIRED */ | 2056 #endif /* defined STD_INSPIRED */ |
OLD | NEW |