1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | * prtime.c --
|
---|
40 | *
|
---|
41 | * NSPR date and time functions
|
---|
42 | *
|
---|
43 | */
|
---|
44 |
|
---|
45 | #include "prinit.h"
|
---|
46 | #include "prtime.h"
|
---|
47 | #include "prlock.h"
|
---|
48 | #include "prprf.h"
|
---|
49 | #include "prlog.h"
|
---|
50 |
|
---|
51 | #include <string.h>
|
---|
52 | #include <ctype.h>
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * Static variables used by functions in this file
|
---|
56 | */
|
---|
57 |
|
---|
58 | /*
|
---|
59 | * The following array contains the day of year for the last day of
|
---|
60 | * each month, where index 1 is January, and day 0 is January 1.
|
---|
61 | */
|
---|
62 |
|
---|
63 | static const int lastDayOfMonth[2][13] = {
|
---|
64 | {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
|
---|
65 | {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
|
---|
66 | };
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * The number of days in a month
|
---|
70 | */
|
---|
71 |
|
---|
72 | static const PRInt8 nDays[2][12] = {
|
---|
73 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
---|
74 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
---|
75 | };
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * Declarations for internal functions defined later in this file.
|
---|
79 | */
|
---|
80 |
|
---|
81 | static void ComputeGMT(PRTime time, PRExplodedTime *gmt);
|
---|
82 | static int IsLeapYear(PRInt16 year);
|
---|
83 | static void ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset);
|
---|
84 |
|
---|
85 | /*
|
---|
86 | *------------------------------------------------------------------------
|
---|
87 | *
|
---|
88 | * ComputeGMT --
|
---|
89 | *
|
---|
90 | * Caveats:
|
---|
91 | * - we ignore leap seconds
|
---|
92 | * - our leap-year calculation is only correct for years 1901-2099
|
---|
93 | *
|
---|
94 | *------------------------------------------------------------------------
|
---|
95 | */
|
---|
96 |
|
---|
97 | static void
|
---|
98 | ComputeGMT(PRTime time, PRExplodedTime *gmt)
|
---|
99 | {
|
---|
100 | PRInt32 tmp, rem;
|
---|
101 | PRInt32 numDays;
|
---|
102 | PRInt64 numDays64, rem64;
|
---|
103 | int isLeap;
|
---|
104 | PRInt64 sec;
|
---|
105 | PRInt64 usec;
|
---|
106 | PRInt64 usecPerSec;
|
---|
107 | PRInt64 secPerDay;
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * We first do the usec, sec, min, hour thing so that we do not
|
---|
111 | * have to do LL arithmetic.
|
---|
112 | */
|
---|
113 |
|
---|
114 | LL_I2L(usecPerSec, 1000000L);
|
---|
115 | LL_DIV(sec, time, usecPerSec);
|
---|
116 | LL_MOD(usec, time, usecPerSec);
|
---|
117 | LL_L2I(gmt->tm_usec, usec);
|
---|
118 | /* Correct for weird mod semantics so the remainder is always positive */
|
---|
119 | if (gmt->tm_usec < 0) {
|
---|
120 | PRInt64 one;
|
---|
121 |
|
---|
122 | LL_I2L(one, 1L);
|
---|
123 | LL_SUB(sec, sec, one);
|
---|
124 | gmt->tm_usec += 1000000L;
|
---|
125 | }
|
---|
126 |
|
---|
127 | LL_I2L(secPerDay, 86400L);
|
---|
128 | LL_DIV(numDays64, sec, secPerDay);
|
---|
129 | LL_MOD(rem64, sec, secPerDay);
|
---|
130 | /* We are sure both of these numbers can fit into PRInt32 */
|
---|
131 | LL_L2I(numDays, numDays64);
|
---|
132 | LL_L2I(rem, rem64);
|
---|
133 | if (rem < 0) {
|
---|
134 | numDays--;
|
---|
135 | rem += 86400L;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* Compute day of week. Epoch started on a Thursday. */
|
---|
139 |
|
---|
140 | gmt->tm_wday = (numDays + 4) % 7;
|
---|
141 | if (gmt->tm_wday < 0) {
|
---|
142 | gmt->tm_wday += 7;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /* Compute the time of day. */
|
---|
146 |
|
---|
147 | gmt->tm_hour = rem / 3600;
|
---|
148 | rem %= 3600;
|
---|
149 | gmt->tm_min = rem / 60;
|
---|
150 | gmt->tm_sec = rem % 60;
|
---|
151 |
|
---|
152 | /* Compute the four-year span containing the specified time */
|
---|
153 |
|
---|
154 | tmp = numDays / (4 * 365 + 1);
|
---|
155 | rem = numDays % (4 * 365 + 1);
|
---|
156 |
|
---|
157 | if (rem < 0) {
|
---|
158 | tmp--;
|
---|
159 | rem += (4 * 365 + 1);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Compute the year after 1900 by taking the four-year span and
|
---|
164 | * adjusting for the remainder. This works because 2000 is a
|
---|
165 | * leap year, and 1900 and 2100 are out of the range.
|
---|
166 | */
|
---|
167 |
|
---|
168 | tmp = (tmp * 4) + 1970;
|
---|
169 | isLeap = 0;
|
---|
170 |
|
---|
171 | /*
|
---|
172 | * 1970 has 365 days
|
---|
173 | * 1971 has 365 days
|
---|
174 | * 1972 has 366 days (leap year)
|
---|
175 | * 1973 has 365 days
|
---|
176 | */
|
---|
177 |
|
---|
178 | if (rem >= 365) { /* 1971, etc. */
|
---|
179 | tmp++;
|
---|
180 | rem -= 365;
|
---|
181 | if (rem >= 365) { /* 1972, etc. */
|
---|
182 | tmp++;
|
---|
183 | rem -= 365;
|
---|
184 | if (rem >= 366) { /* 1973, etc. */
|
---|
185 | tmp++;
|
---|
186 | rem -= 366;
|
---|
187 | } else {
|
---|
188 | isLeap = 1;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | gmt->tm_year = tmp;
|
---|
194 | gmt->tm_yday = rem;
|
---|
195 |
|
---|
196 | /* Compute the month and day of month. */
|
---|
197 |
|
---|
198 | for (tmp = 1; lastDayOfMonth[isLeap][tmp] < gmt->tm_yday; tmp++) {
|
---|
199 | }
|
---|
200 | gmt->tm_month = --tmp;
|
---|
201 | gmt->tm_mday = gmt->tm_yday - lastDayOfMonth[isLeap][tmp];
|
---|
202 |
|
---|
203 | gmt->tm_params.tp_gmt_offset = 0;
|
---|
204 | gmt->tm_params.tp_dst_offset = 0;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /*
|
---|
209 | *------------------------------------------------------------------------
|
---|
210 | *
|
---|
211 | * PR_ExplodeTime --
|
---|
212 | *
|
---|
213 | * Cf. struct tm *gmtime(const time_t *tp) and
|
---|
214 | * struct tm *localtime(const time_t *tp)
|
---|
215 | *
|
---|
216 | *------------------------------------------------------------------------
|
---|
217 | */
|
---|
218 |
|
---|
219 | PR_IMPLEMENT(void)
|
---|
220 | PR_ExplodeTime(
|
---|
221 | PRTime usecs,
|
---|
222 | PRTimeParamFn params,
|
---|
223 | PRExplodedTime *exploded)
|
---|
224 | {
|
---|
225 | ComputeGMT(usecs, exploded);
|
---|
226 | exploded->tm_params = params(exploded);
|
---|
227 | ApplySecOffset(exploded, exploded->tm_params.tp_gmt_offset
|
---|
228 | + exploded->tm_params.tp_dst_offset);
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | /*
|
---|
233 | *------------------------------------------------------------------------
|
---|
234 | *
|
---|
235 | * PR_ImplodeTime --
|
---|
236 | *
|
---|
237 | * Cf. time_t mktime(struct tm *tp)
|
---|
238 | * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough.
|
---|
239 | *
|
---|
240 | *------------------------------------------------------------------------
|
---|
241 | */
|
---|
242 | PR_IMPLEMENT(PRTime)
|
---|
243 | PR_ImplodeTime(const PRExplodedTime *exploded)
|
---|
244 | {
|
---|
245 | PRExplodedTime copy;
|
---|
246 | PRTime retVal;
|
---|
247 | PRInt64 secPerDay, usecPerSec;
|
---|
248 | PRInt64 temp;
|
---|
249 | PRInt64 numSecs64;
|
---|
250 | PRInt32 fourYears;
|
---|
251 | PRInt32 remainder;
|
---|
252 | PRInt32 numDays;
|
---|
253 | PRInt32 numSecs;
|
---|
254 |
|
---|
255 | /* Normalize first. Do this on our copy */
|
---|
256 | copy = *exploded;
|
---|
257 | PR_NormalizeTime(©, PR_GMTParameters);
|
---|
258 |
|
---|
259 | fourYears = (copy.tm_year - 1970) / 4;
|
---|
260 | remainder = (copy.tm_year - 1970) % 4;
|
---|
261 | if (remainder < 0) {
|
---|
262 | remainder += 4;
|
---|
263 | fourYears--;
|
---|
264 | }
|
---|
265 | numDays = fourYears * (4 * 365 + 1);
|
---|
266 | switch (remainder) {
|
---|
267 | case 0:
|
---|
268 | break;
|
---|
269 | case 1: /* 1970 */
|
---|
270 | numDays += 365;
|
---|
271 | break;
|
---|
272 | case 2: /* 1970-1 */
|
---|
273 | numDays += 365 * 2;
|
---|
274 | break;
|
---|
275 | case 3: /* 1970-2 */
|
---|
276 | numDays += 365 * 3 + 1;
|
---|
277 | break;
|
---|
278 | }
|
---|
279 |
|
---|
280 | numSecs = copy.tm_yday * 86400 + copy.tm_hour * 3600
|
---|
281 | + copy.tm_min * 60 + copy.tm_sec;
|
---|
282 |
|
---|
283 | LL_I2L(temp, numDays);
|
---|
284 | LL_I2L(secPerDay, 86400);
|
---|
285 | LL_MUL(temp, temp, secPerDay);
|
---|
286 | LL_I2L(numSecs64, numSecs);
|
---|
287 | LL_ADD(numSecs64, numSecs64, temp);
|
---|
288 |
|
---|
289 | /* apply the GMT and DST offsets */
|
---|
290 | LL_I2L(temp, copy.tm_params.tp_gmt_offset);
|
---|
291 | LL_SUB(numSecs64, numSecs64, temp);
|
---|
292 | LL_I2L(temp, copy.tm_params.tp_dst_offset);
|
---|
293 | LL_SUB(numSecs64, numSecs64, temp);
|
---|
294 |
|
---|
295 | LL_I2L(usecPerSec, 1000000L);
|
---|
296 | LL_MUL(temp, numSecs64, usecPerSec);
|
---|
297 | LL_I2L(retVal, copy.tm_usec);
|
---|
298 | LL_ADD(retVal, retVal, temp);
|
---|
299 |
|
---|
300 | return retVal;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /*
|
---|
304 | *-------------------------------------------------------------------------
|
---|
305 | *
|
---|
306 | * IsLeapYear --
|
---|
307 | *
|
---|
308 | * Returns 1 if the year is a leap year, 0 otherwise.
|
---|
309 | *
|
---|
310 | *-------------------------------------------------------------------------
|
---|
311 | */
|
---|
312 |
|
---|
313 | static int IsLeapYear(PRInt16 year)
|
---|
314 | {
|
---|
315 | if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
|
---|
316 | return 1;
|
---|
317 | else
|
---|
318 | return 0;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /*
|
---|
322 | * 'secOffset' should be less than 86400 (i.e., a day).
|
---|
323 | * 'time' should point to a normalized PRExplodedTime.
|
---|
324 | */
|
---|
325 |
|
---|
326 | static void
|
---|
327 | ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset)
|
---|
328 | {
|
---|
329 | time->tm_sec += secOffset;
|
---|
330 |
|
---|
331 | /* Note that in this implementation we do not count leap seconds */
|
---|
332 | if (time->tm_sec < 0 || time->tm_sec >= 60) {
|
---|
333 | time->tm_min += time->tm_sec / 60;
|
---|
334 | time->tm_sec %= 60;
|
---|
335 | if (time->tm_sec < 0) {
|
---|
336 | time->tm_sec += 60;
|
---|
337 | time->tm_min--;
|
---|
338 | }
|
---|
339 | }
|
---|
340 |
|
---|
341 | if (time->tm_min < 0 || time->tm_min >= 60) {
|
---|
342 | time->tm_hour += time->tm_min / 60;
|
---|
343 | time->tm_min %= 60;
|
---|
344 | if (time->tm_min < 0) {
|
---|
345 | time->tm_min += 60;
|
---|
346 | time->tm_hour--;
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | if (time->tm_hour < 0) {
|
---|
351 | /* Decrement mday, yday, and wday */
|
---|
352 | time->tm_hour += 24;
|
---|
353 | time->tm_mday--;
|
---|
354 | time->tm_yday--;
|
---|
355 | if (time->tm_mday < 1) {
|
---|
356 | time->tm_month--;
|
---|
357 | if (time->tm_month < 0) {
|
---|
358 | time->tm_month = 11;
|
---|
359 | time->tm_year--;
|
---|
360 | if (IsLeapYear(time->tm_year))
|
---|
361 | time->tm_yday = 365;
|
---|
362 | else
|
---|
363 | time->tm_yday = 364;
|
---|
364 | }
|
---|
365 | time->tm_mday = nDays[IsLeapYear(time->tm_year)][time->tm_month];
|
---|
366 | }
|
---|
367 | time->tm_wday--;
|
---|
368 | if (time->tm_wday < 0)
|
---|
369 | time->tm_wday = 6;
|
---|
370 | } else if (time->tm_hour > 23) {
|
---|
371 | /* Increment mday, yday, and wday */
|
---|
372 | time->tm_hour -= 24;
|
---|
373 | time->tm_mday++;
|
---|
374 | time->tm_yday++;
|
---|
375 | if (time->tm_mday >
|
---|
376 | nDays[IsLeapYear(time->tm_year)][time->tm_month]) {
|
---|
377 | time->tm_mday = 1;
|
---|
378 | time->tm_month++;
|
---|
379 | if (time->tm_month > 11) {
|
---|
380 | time->tm_month = 0;
|
---|
381 | time->tm_year++;
|
---|
382 | time->tm_yday = 0;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | time->tm_wday++;
|
---|
386 | if (time->tm_wday > 6)
|
---|
387 | time->tm_wday = 0;
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | PR_IMPLEMENT(void)
|
---|
392 | PR_NormalizeTime(PRExplodedTime *time, PRTimeParamFn params)
|
---|
393 | {
|
---|
394 | int daysInMonth;
|
---|
395 | PRInt32 fourYears;
|
---|
396 | PRInt32 remainder;
|
---|
397 | PRInt32 numDays;
|
---|
398 |
|
---|
399 | /* Get back to GMT */
|
---|
400 | time->tm_sec -= time->tm_params.tp_gmt_offset
|
---|
401 | + time->tm_params.tp_dst_offset;
|
---|
402 | time->tm_params.tp_gmt_offset = 0;
|
---|
403 | time->tm_params.tp_dst_offset = 0;
|
---|
404 |
|
---|
405 | /* Now normalize GMT */
|
---|
406 |
|
---|
407 | if (time->tm_usec < 0 || time->tm_usec >= 1000000) {
|
---|
408 | time->tm_sec += time->tm_usec / 1000000;
|
---|
409 | time->tm_usec %= 1000000;
|
---|
410 | if (time->tm_usec < 0) {
|
---|
411 | time->tm_usec += 1000000;
|
---|
412 | time->tm_sec--;
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | /* Note that we do not count leap seconds in this implementation */
|
---|
417 | if (time->tm_sec < 0 || time->tm_sec >= 60) {
|
---|
418 | time->tm_min += time->tm_sec / 60;
|
---|
419 | time->tm_sec %= 60;
|
---|
420 | if (time->tm_sec < 0) {
|
---|
421 | time->tm_sec += 60;
|
---|
422 | time->tm_min--;
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | if (time->tm_min < 0 || time->tm_min >= 60) {
|
---|
427 | time->tm_hour += time->tm_min / 60;
|
---|
428 | time->tm_min %= 60;
|
---|
429 | if (time->tm_min < 0) {
|
---|
430 | time->tm_min += 60;
|
---|
431 | time->tm_hour--;
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | if (time->tm_hour < 0 || time->tm_hour >= 24) {
|
---|
436 | time->tm_mday += time->tm_hour / 24;
|
---|
437 | time->tm_hour %= 24;
|
---|
438 | if (time->tm_hour < 0) {
|
---|
439 | time->tm_hour += 24;
|
---|
440 | time->tm_mday--;
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | /* Normalize month and year before mday */
|
---|
445 | if (time->tm_month < 0 || time->tm_month >= 12) {
|
---|
446 | time->tm_year += time->tm_month / 12;
|
---|
447 | time->tm_month %= 12;
|
---|
448 | if (time->tm_month < 0) {
|
---|
449 | time->tm_month += 12;
|
---|
450 | time->tm_year--;
|
---|
451 | }
|
---|
452 | }
|
---|
453 |
|
---|
454 | /* Now that month and year are in proper range, normalize mday */
|
---|
455 |
|
---|
456 | if (time->tm_mday < 1) {
|
---|
457 | /* mday too small */
|
---|
458 | do {
|
---|
459 | /* the previous month */
|
---|
460 | time->tm_month--;
|
---|
461 | if (time->tm_month < 0) {
|
---|
462 | time->tm_month = 11;
|
---|
463 | time->tm_year--;
|
---|
464 | }
|
---|
465 | time->tm_mday += nDays[IsLeapYear(time->tm_year)][time->tm_month];
|
---|
466 | } while (time->tm_mday < 1);
|
---|
467 | } else {
|
---|
468 | daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month];
|
---|
469 | while (time->tm_mday > daysInMonth) {
|
---|
470 | /* mday too large */
|
---|
471 | time->tm_mday -= daysInMonth;
|
---|
472 | time->tm_month++;
|
---|
473 | if (time->tm_month > 11) {
|
---|
474 | time->tm_month = 0;
|
---|
475 | time->tm_year++;
|
---|
476 | }
|
---|
477 | daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month];
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | /* Recompute yday and wday */
|
---|
482 | time->tm_yday = time->tm_mday +
|
---|
483 | lastDayOfMonth[IsLeapYear(time->tm_year)][time->tm_month];
|
---|
484 | fourYears = (time->tm_year - 1970) / 4;
|
---|
485 | remainder = (time->tm_year - 1970) % 4;
|
---|
486 | if (remainder < 0) {
|
---|
487 | remainder += 4;
|
---|
488 | fourYears--;
|
---|
489 | }
|
---|
490 | numDays = fourYears * (4 * 365 + 1);
|
---|
491 | switch (remainder) {
|
---|
492 | case 0:
|
---|
493 | break;
|
---|
494 | case 1:
|
---|
495 | numDays += 365; /* 1970 */
|
---|
496 | break;
|
---|
497 | case 2:
|
---|
498 | numDays += 365 + 365; /* 1970 and 1971 */
|
---|
499 | break;
|
---|
500 | case 3:
|
---|
501 | numDays += 365 + 365 + 366; /* 1970-2 */
|
---|
502 | }
|
---|
503 | numDays += time->tm_yday;
|
---|
504 | time->tm_wday = (numDays + 4) % 7;
|
---|
505 | if (time->tm_wday < 0) {
|
---|
506 | time->tm_wday += 7;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /* Recompute time parameters */
|
---|
510 |
|
---|
511 | time->tm_params = params(time);
|
---|
512 |
|
---|
513 | ApplySecOffset(time, time->tm_params.tp_gmt_offset
|
---|
514 | + time->tm_params.tp_dst_offset);
|
---|
515 | }
|
---|
516 |
|
---|
517 |
|
---|
518 | /*
|
---|
519 | *-------------------------------------------------------------------------
|
---|
520 | *
|
---|
521 | * PR_LocalTimeParameters --
|
---|
522 | *
|
---|
523 | * returns the time parameters for the local time zone
|
---|
524 | *
|
---|
525 | * The following uses localtime() from the standard C library.
|
---|
526 | * (time.h) This is our fallback implementation. Unix and PC
|
---|
527 | * use this version. Mac has its own machine-dependent
|
---|
528 | * implementation of this function.
|
---|
529 | *
|
---|
530 | *-------------------------------------------------------------------------
|
---|
531 | */
|
---|
532 |
|
---|
533 | #include <time.h>
|
---|
534 |
|
---|
535 | #if defined(HAVE_INT_LOCALTIME_R)
|
---|
536 |
|
---|
537 | /*
|
---|
538 | * In this case we could define the macro as
|
---|
539 | * #define MT_safe_localtime(timer, result) \
|
---|
540 | * (localtime_r(timer, result) == 0 ? result : NULL)
|
---|
541 | * I chose to compare the return value of localtime_r with -1 so
|
---|
542 | * that I can catch the cases where localtime_r returns a pointer
|
---|
543 | * to struct tm. The macro definition above would not be able to
|
---|
544 | * detect such mistakes because it is legal to compare a pointer
|
---|
545 | * with 0.
|
---|
546 | */
|
---|
547 |
|
---|
548 | #define MT_safe_localtime(timer, result) \
|
---|
549 | (localtime_r(timer, result) == -1 ? NULL: result)
|
---|
550 |
|
---|
551 | #elif defined(HAVE_POINTER_LOCALTIME_R)
|
---|
552 |
|
---|
553 | #define MT_safe_localtime localtime_r
|
---|
554 |
|
---|
555 | #else
|
---|
556 |
|
---|
557 | static PRLock *monitor = NULL;
|
---|
558 |
|
---|
559 | static struct tm *MT_safe_localtime(const time_t *clock, struct tm *result)
|
---|
560 | {
|
---|
561 | struct tm *tmPtr;
|
---|
562 | int needLock = PR_Initialized(); /* We need to use a lock to protect
|
---|
563 | * against NSPR threads only when the
|
---|
564 | * NSPR thread system is activated. */
|
---|
565 |
|
---|
566 | if (needLock) {
|
---|
567 | if (monitor == NULL) {
|
---|
568 | monitor = PR_NewLock();
|
---|
569 | }
|
---|
570 | PR_Lock(monitor);
|
---|
571 | }
|
---|
572 |
|
---|
573 | /*
|
---|
574 | * Microsoft (all flavors) localtime() returns a NULL pointer if 'clock'
|
---|
575 | * represents a time before midnight January 1, 1970. In
|
---|
576 | * that case, we also return a NULL pointer and the struct tm
|
---|
577 | * object pointed to by 'result' is not modified.
|
---|
578 | *
|
---|
579 | * Watcom C/C++ 11.0 localtime() treats time_t as unsigned long
|
---|
580 | * hence, does not recognize negative values of clock as pre-1/1/70.
|
---|
581 | * We have to manually check (WIN16 only) for negative value of
|
---|
582 | * clock and return NULL.
|
---|
583 | *
|
---|
584 | * With negative values of clock, emx returns the struct tm for
|
---|
585 | * clock plus ULONG_MAX. So we also have to check for the invalid
|
---|
586 | * structs returned for timezones west of Greenwich when clock == 0.
|
---|
587 | */
|
---|
588 |
|
---|
589 | tmPtr = localtime(clock);
|
---|
590 | if (tmPtr) {
|
---|
591 | *result = *tmPtr;
|
---|
592 | } else {
|
---|
593 | result = NULL;
|
---|
594 | }
|
---|
595 | if (needLock) PR_Unlock(monitor);
|
---|
596 |
|
---|
597 | return result;
|
---|
598 | }
|
---|
599 |
|
---|
600 | #endif /* definition of MT_safe_localtime() */
|
---|
601 |
|
---|
602 | #if defined(XP_UNIX)
|
---|
603 |
|
---|
604 | PR_IMPLEMENT(PRTimeParameters)
|
---|
605 | PR_LocalTimeParameters(const PRExplodedTime *gmt)
|
---|
606 | {
|
---|
607 |
|
---|
608 | PRTimeParameters retVal;
|
---|
609 | struct tm localTime;
|
---|
610 | time_t secs;
|
---|
611 | PRTime secs64;
|
---|
612 | PRInt64 usecPerSec;
|
---|
613 | PRInt64 maxInt32;
|
---|
614 | PRInt64 minInt32;
|
---|
615 | PRInt32 dayOffset;
|
---|
616 | PRInt32 offset2Jan1970;
|
---|
617 | PRInt32 offsetNew;
|
---|
618 | int isdst2Jan1970;
|
---|
619 |
|
---|
620 | /*
|
---|
621 | * Calculate the GMT offset. First, figure out what is
|
---|
622 | * 00:00:00 Jan. 2, 1970 GMT (which is exactly a day, or 86400
|
---|
623 | * seconds, since the epoch) in local time. Then we calculate
|
---|
624 | * the difference between local time and GMT in seconds:
|
---|
625 | * gmt_offset = local_time - GMT
|
---|
626 | *
|
---|
627 | * Caveat: the validity of this calculation depends on two
|
---|
628 | * assumptions:
|
---|
629 | * 1. Daylight saving time was not in effect on Jan. 2, 1970.
|
---|
630 | * 2. The time zone of the geographic location has not changed
|
---|
631 | * since Jan. 2, 1970.
|
---|
632 | */
|
---|
633 |
|
---|
634 | secs = 86400L;
|
---|
635 | (void) MT_safe_localtime(&secs, &localTime);
|
---|
636 |
|
---|
637 | /* GMT is 00:00:00, 2nd of Jan. */
|
---|
638 |
|
---|
639 | offset2Jan1970 = (PRInt32)localTime.tm_sec
|
---|
640 | + 60L * (PRInt32)localTime.tm_min
|
---|
641 | + 3600L * (PRInt32)localTime.tm_hour
|
---|
642 | + 86400L * (PRInt32)((PRInt32)localTime.tm_mday - 2L);
|
---|
643 |
|
---|
644 | isdst2Jan1970 = localTime.tm_isdst;
|
---|
645 |
|
---|
646 | /*
|
---|
647 | * Now compute DST offset. We calculate the overall offset
|
---|
648 | * of local time from GMT, similar to above. The overall
|
---|
649 | * offset has two components: gmt offset and dst offset.
|
---|
650 | * We subtract gmt offset from the overall offset to get
|
---|
651 | * the dst offset.
|
---|
652 | * overall_offset = local_time - GMT
|
---|
653 | * overall_offset = gmt_offset + dst_offset
|
---|
654 | * ==> dst_offset = local_time - GMT - gmt_offset
|
---|
655 | */
|
---|
656 |
|
---|
657 | secs64 = PR_ImplodeTime(gmt); /* This is still in microseconds */
|
---|
658 | LL_I2L(usecPerSec, PR_USEC_PER_SEC);
|
---|
659 | LL_DIV(secs64, secs64, usecPerSec); /* Convert to seconds */
|
---|
660 | LL_I2L(maxInt32, PR_INT32_MAX);
|
---|
661 | LL_I2L(minInt32, PR_INT32_MIN);
|
---|
662 | if (LL_CMP(secs64, >, maxInt32) || LL_CMP(secs64, <, minInt32)) {
|
---|
663 | /* secs64 is too large or too small for time_t (32-bit integer) */
|
---|
664 | retVal.tp_gmt_offset = offset2Jan1970;
|
---|
665 | retVal.tp_dst_offset = 0;
|
---|
666 | return retVal;
|
---|
667 | }
|
---|
668 | LL_L2I(secs, secs64);
|
---|
669 |
|
---|
670 | /*
|
---|
671 | * On Windows, localtime() (and our MT_safe_localtime() too)
|
---|
672 | * returns a NULL pointer for time before midnight January 1,
|
---|
673 | * 1970 GMT. In that case, we just use the GMT offset for
|
---|
674 | * Jan 2, 1970 and assume that DST was not in effect.
|
---|
675 | */
|
---|
676 |
|
---|
677 | if (MT_safe_localtime(&secs, &localTime) == NULL) {
|
---|
678 | retVal.tp_gmt_offset = offset2Jan1970;
|
---|
679 | retVal.tp_dst_offset = 0;
|
---|
680 | return retVal;
|
---|
681 | }
|
---|
682 |
|
---|
683 | /*
|
---|
684 | * dayOffset is the offset between local time and GMT in
|
---|
685 | * the day component, which can only be -1, 0, or 1. We
|
---|
686 | * use the day of the week to compute dayOffset.
|
---|
687 | */
|
---|
688 |
|
---|
689 | dayOffset = (PRInt32) localTime.tm_wday - gmt->tm_wday;
|
---|
690 |
|
---|
691 | /*
|
---|
692 | * Need to adjust for wrapping around of day of the week from
|
---|
693 | * 6 back to 0.
|
---|
694 | */
|
---|
695 |
|
---|
696 | if (dayOffset == -6) {
|
---|
697 | /* Local time is Sunday (0) and GMT is Saturday (6) */
|
---|
698 | dayOffset = 1;
|
---|
699 | } else if (dayOffset == 6) {
|
---|
700 | /* Local time is Saturday (6) and GMT is Sunday (0) */
|
---|
701 | dayOffset = -1;
|
---|
702 | }
|
---|
703 |
|
---|
704 | offsetNew = (PRInt32)localTime.tm_sec - gmt->tm_sec
|
---|
705 | + 60L * ((PRInt32)localTime.tm_min - gmt->tm_min)
|
---|
706 | + 3600L * ((PRInt32)localTime.tm_hour - gmt->tm_hour)
|
---|
707 | + 86400L * (PRInt32)dayOffset;
|
---|
708 |
|
---|
709 | if (localTime.tm_isdst <= 0) {
|
---|
710 | /* DST is not in effect */
|
---|
711 | retVal.tp_gmt_offset = offsetNew;
|
---|
712 | retVal.tp_dst_offset = 0;
|
---|
713 | } else {
|
---|
714 | /* DST is in effect */
|
---|
715 | if (isdst2Jan1970 <=0) {
|
---|
716 | /*
|
---|
717 | * DST was not in effect back in 2 Jan. 1970.
|
---|
718 | * Use the offset back then as the GMT offset,
|
---|
719 | * assuming the time zone has not changed since then.
|
---|
720 | */
|
---|
721 | retVal.tp_gmt_offset = offset2Jan1970;
|
---|
722 | retVal.tp_dst_offset = offsetNew - offset2Jan1970;
|
---|
723 | } else {
|
---|
724 | /*
|
---|
725 | * DST was also in effect back in 2 Jan. 1970.
|
---|
726 | * Then our clever trick (or rather, ugly hack) fails.
|
---|
727 | * We will just assume DST offset is an hour.
|
---|
728 | */
|
---|
729 | retVal.tp_gmt_offset = offsetNew - 3600;
|
---|
730 | retVal.tp_dst_offset = 3600;
|
---|
731 | }
|
---|
732 | }
|
---|
733 |
|
---|
734 | return retVal;
|
---|
735 | }
|
---|
736 |
|
---|
737 | #endif /* defined(XP_UNIX) */
|
---|
738 |
|
---|
739 | /*
|
---|
740 | *------------------------------------------------------------------------
|
---|
741 | *
|
---|
742 | * PR_USPacificTimeParameters --
|
---|
743 | *
|
---|
744 | * The time parameters function for the US Pacific Time Zone.
|
---|
745 | *
|
---|
746 | *------------------------------------------------------------------------
|
---|
747 | */
|
---|
748 |
|
---|
749 | PR_IMPLEMENT(PRTimeParameters)
|
---|
750 | PR_USPacificTimeParameters(const PRExplodedTime *gmt)
|
---|
751 | {
|
---|
752 | PRTimeParameters retVal;
|
---|
753 | PRExplodedTime st;
|
---|
754 |
|
---|
755 | /*
|
---|
756 | * Based on geographic location and GMT, figure out offset of
|
---|
757 | * standard time from GMT. In this example implementation, we
|
---|
758 | * assume the local time zone is US Pacific Time.
|
---|
759 | */
|
---|
760 |
|
---|
761 | retVal.tp_gmt_offset = -8L * 3600L;
|
---|
762 |
|
---|
763 | /*
|
---|
764 | * Make a copy of GMT. Note that the tm_params field of this copy
|
---|
765 | * is ignored.
|
---|
766 | */
|
---|
767 |
|
---|
768 | st.tm_usec = gmt->tm_usec;
|
---|
769 | st.tm_sec = gmt->tm_sec;
|
---|
770 | st.tm_min = gmt->tm_min;
|
---|
771 | st.tm_hour = gmt->tm_hour;
|
---|
772 | st.tm_mday = gmt->tm_mday;
|
---|
773 | st.tm_month = gmt->tm_month;
|
---|
774 | st.tm_year = gmt->tm_year;
|
---|
775 | st.tm_wday = gmt->tm_wday;
|
---|
776 | st.tm_yday = gmt->tm_yday;
|
---|
777 |
|
---|
778 | /* Apply the offset to GMT to obtain the local standard time */
|
---|
779 | ApplySecOffset(&st, retVal.tp_gmt_offset);
|
---|
780 |
|
---|
781 | /*
|
---|
782 | * Apply the rules on standard time or GMT to obtain daylight saving
|
---|
783 | * time offset. In this implementation, we use the US DST rule.
|
---|
784 | */
|
---|
785 | if (st.tm_month < 3) {
|
---|
786 | retVal.tp_dst_offset = 0L;
|
---|
787 | } else if (st.tm_month == 3) {
|
---|
788 | if (st.tm_wday == 0) {
|
---|
789 | /* A Sunday */
|
---|
790 | if (st.tm_mday <= 7) {
|
---|
791 | /* First Sunday */
|
---|
792 | /* 01:59:59 PST -> 03:00:00 PDT */
|
---|
793 | if (st.tm_hour < 2) {
|
---|
794 | retVal.tp_dst_offset = 0L;
|
---|
795 | } else {
|
---|
796 | retVal.tp_dst_offset = 3600L;
|
---|
797 | }
|
---|
798 | } else {
|
---|
799 | /* Not first Sunday */
|
---|
800 | retVal.tp_dst_offset = 3600L;
|
---|
801 | }
|
---|
802 | } else {
|
---|
803 | /* Not a Sunday. See if before first Sunday or after */
|
---|
804 | if (st.tm_wday + 1 <= st.tm_mday) {
|
---|
805 | /* After first Sunday */
|
---|
806 | retVal.tp_dst_offset = 3600L;
|
---|
807 | } else {
|
---|
808 | /* Before first Sunday */
|
---|
809 | retVal.tp_dst_offset = 0L;
|
---|
810 | }
|
---|
811 | }
|
---|
812 | } else if (st.tm_month < 9) {
|
---|
813 | retVal.tp_dst_offset = 3600L;
|
---|
814 | } else if (st.tm_month == 9) {
|
---|
815 | if (st.tm_wday == 0) {
|
---|
816 | if (31 - st.tm_mday < 7) {
|
---|
817 | /* Last Sunday */
|
---|
818 | /* 01:59:59 PDT -> 01:00:00 PST */
|
---|
819 | if (st.tm_hour < 1) {
|
---|
820 | retVal.tp_dst_offset = 3600L;
|
---|
821 | } else {
|
---|
822 | retVal.tp_dst_offset = 0L;
|
---|
823 | }
|
---|
824 | } else {
|
---|
825 | /* Not last Sunday */
|
---|
826 | retVal.tp_dst_offset = 3600L;
|
---|
827 | }
|
---|
828 | } else {
|
---|
829 | /* See if before or after last Sunday */
|
---|
830 | if (7 - st.tm_wday <= 31 - st.tm_mday) {
|
---|
831 | /* before last Sunday */
|
---|
832 | retVal.tp_dst_offset = 3600L;
|
---|
833 | } else {
|
---|
834 | retVal.tp_dst_offset = 0L;
|
---|
835 | }
|
---|
836 | }
|
---|
837 | } else {
|
---|
838 | retVal.tp_dst_offset = 0L;
|
---|
839 | }
|
---|
840 | return retVal;
|
---|
841 | }
|
---|
842 |
|
---|
843 | /*
|
---|
844 | *------------------------------------------------------------------------
|
---|
845 | *
|
---|
846 | * PR_GMTParameters --
|
---|
847 | *
|
---|
848 | * Returns the PRTimeParameters for Greenwich Mean Time.
|
---|
849 | * Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0.
|
---|
850 | *
|
---|
851 | *------------------------------------------------------------------------
|
---|
852 | */
|
---|
853 |
|
---|
854 | PR_IMPLEMENT(PRTimeParameters)
|
---|
855 | PR_GMTParameters(const PRExplodedTime *gmt)
|
---|
856 | {
|
---|
857 | PRTimeParameters retVal = { 0, 0 };
|
---|
858 | return retVal;
|
---|
859 | }
|
---|
860 |
|
---|
861 | /*
|
---|
862 | * The following code implements PR_ParseTimeString(). It is based on
|
---|
863 | * ns/lib/xp/xp_time.c, revision 1.25, by Jamie Zawinski <[email protected]>.
|
---|
864 | */
|
---|
865 |
|
---|
866 | /*
|
---|
867 | * We only recognize the abbreviations of a small subset of time zones
|
---|
868 | * in North America, Europe, and Japan.
|
---|
869 | *
|
---|
870 | * PST/PDT: Pacific Standard/Daylight Time
|
---|
871 | * MST/MDT: Mountain Standard/Daylight Time
|
---|
872 | * CST/CDT: Central Standard/Daylight Time
|
---|
873 | * EST/EDT: Eastern Standard/Daylight Time
|
---|
874 | * AST: Atlantic Standard Time
|
---|
875 | * NST: Newfoundland Standard Time
|
---|
876 | * GMT: Greenwich Mean Time
|
---|
877 | * BST: British Summer Time
|
---|
878 | * MET: Middle Europe Time
|
---|
879 | * EET: Eastern Europe Time
|
---|
880 | * JST: Japan Standard Time
|
---|
881 | */
|
---|
882 |
|
---|
883 | typedef enum
|
---|
884 | {
|
---|
885 | TT_UNKNOWN,
|
---|
886 |
|
---|
887 | TT_SUN, TT_MON, TT_TUE, TT_WED, TT_THU, TT_FRI, TT_SAT,
|
---|
888 |
|
---|
889 | TT_JAN, TT_FEB, TT_MAR, TT_APR, TT_MAY, TT_JUN,
|
---|
890 | TT_JUL, TT_AUG, TT_SEP, TT_OCT, TT_NOV, TT_DEC,
|
---|
891 |
|
---|
892 | TT_PST, TT_PDT, TT_MST, TT_MDT, TT_CST, TT_CDT, TT_EST, TT_EDT,
|
---|
893 | TT_AST, TT_NST, TT_GMT, TT_BST, TT_MET, TT_EET, TT_JST
|
---|
894 | } TIME_TOKEN;
|
---|
895 |
|
---|
896 | /*
|
---|
897 | * This parses a time/date string into a PRTime
|
---|
898 | * (microseconds after "1-Jan-1970 00:00:00 GMT").
|
---|
899 | * It returns PR_SUCCESS on success, and PR_FAILURE
|
---|
900 | * if the time/date string can't be parsed.
|
---|
901 | *
|
---|
902 | * Many formats are handled, including:
|
---|
903 | *
|
---|
904 | * 14 Apr 89 03:20:12
|
---|
905 | * 14 Apr 89 03:20 GMT
|
---|
906 | * Fri, 17 Mar 89 4:01:33
|
---|
907 | * Fri, 17 Mar 89 4:01 GMT
|
---|
908 | * Mon Jan 16 16:12 PDT 1989
|
---|
909 | * Mon Jan 16 16:12 +0130 1989
|
---|
910 | * 6 May 1992 16:41-JST (Wednesday)
|
---|
911 | * 22-AUG-1993 10:59:12.82
|
---|
912 | * 22-AUG-1993 10:59pm
|
---|
913 | * 22-AUG-1993 12:59am
|
---|
914 | * 22-AUG-1993 12:59 PM
|
---|
915 | * Friday, August 04, 1995 3:54 PM
|
---|
916 | * 06/21/95 04:24:34 PM
|
---|
917 | * 20/06/95 21:07
|
---|
918 | * 95-06-08 19:32:48 EDT
|
---|
919 | *
|
---|
920 | * If the input string doesn't contain a description of the timezone,
|
---|
921 | * we consult the `default_to_gmt' to decide whether the string should
|
---|
922 | * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
|
---|
923 | * The correct value for this argument depends on what standard specified
|
---|
924 | * the time string which you are parsing.
|
---|
925 | */
|
---|
926 |
|
---|
927 | PR_IMPLEMENT(PRStatus)
|
---|
928 | PR_ParseTimeString(
|
---|
929 | const char *string,
|
---|
930 | PRBool default_to_gmt,
|
---|
931 | PRTime *result)
|
---|
932 | {
|
---|
933 | PRExplodedTime tm;
|
---|
934 | TIME_TOKEN dotw = TT_UNKNOWN;
|
---|
935 | TIME_TOKEN month = TT_UNKNOWN;
|
---|
936 | TIME_TOKEN zone = TT_UNKNOWN;
|
---|
937 | int zone_offset = -1;
|
---|
938 | int date = -1;
|
---|
939 | PRInt32 year = -1;
|
---|
940 | int hour = -1;
|
---|
941 | int min = -1;
|
---|
942 | int sec = -1;
|
---|
943 |
|
---|
944 | const char *rest = string;
|
---|
945 |
|
---|
946 | #ifdef DEBUG
|
---|
947 | int iterations = 0;
|
---|
948 | #endif
|
---|
949 |
|
---|
950 | PR_ASSERT(string && result);
|
---|
951 | if (!string || !result) return PR_FAILURE;
|
---|
952 |
|
---|
953 | while (*rest)
|
---|
954 | {
|
---|
955 |
|
---|
956 | #ifdef DEBUG
|
---|
957 | if (iterations++ > 1000)
|
---|
958 | {
|
---|
959 | PR_ASSERT(0);
|
---|
960 | return PR_FAILURE;
|
---|
961 | }
|
---|
962 | #endif
|
---|
963 |
|
---|
964 | switch (*rest)
|
---|
965 | {
|
---|
966 | case 'a': case 'A':
|
---|
967 | if (month == TT_UNKNOWN &&
|
---|
968 | (rest[1] == 'p' || rest[1] == 'P') &&
|
---|
969 | (rest[2] == 'r' || rest[2] == 'R'))
|
---|
970 | month = TT_APR;
|
---|
971 | else if (zone == TT_UNKNOWN &&
|
---|
972 | (rest[1] == 's' || rest[1] == 'S') &&
|
---|
973 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
974 | zone = TT_AST;
|
---|
975 | else if (month == TT_UNKNOWN &&
|
---|
976 | (rest[1] == 'u' || rest[1] == 'U') &&
|
---|
977 | (rest[2] == 'g' || rest[2] == 'G'))
|
---|
978 | month = TT_AUG;
|
---|
979 | break;
|
---|
980 | case 'b': case 'B':
|
---|
981 | if (zone == TT_UNKNOWN &&
|
---|
982 | (rest[1] == 's' || rest[1] == 'S') &&
|
---|
983 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
984 | zone = TT_BST;
|
---|
985 | break;
|
---|
986 | case 'c': case 'C':
|
---|
987 | if (zone == TT_UNKNOWN &&
|
---|
988 | (rest[1] == 'd' || rest[1] == 'D') &&
|
---|
989 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
990 | zone = TT_CDT;
|
---|
991 | else if (zone == TT_UNKNOWN &&
|
---|
992 | (rest[1] == 's' || rest[1] == 'S') &&
|
---|
993 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
994 | zone = TT_CST;
|
---|
995 | break;
|
---|
996 | case 'd': case 'D':
|
---|
997 | if (month == TT_UNKNOWN &&
|
---|
998 | (rest[1] == 'e' || rest[1] == 'E') &&
|
---|
999 | (rest[2] == 'c' || rest[2] == 'C'))
|
---|
1000 | month = TT_DEC;
|
---|
1001 | break;
|
---|
1002 | case 'e': case 'E':
|
---|
1003 | if (zone == TT_UNKNOWN &&
|
---|
1004 | (rest[1] == 'd' || rest[1] == 'D') &&
|
---|
1005 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1006 | zone = TT_EDT;
|
---|
1007 | else if (zone == TT_UNKNOWN &&
|
---|
1008 | (rest[1] == 'e' || rest[1] == 'E') &&
|
---|
1009 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1010 | zone = TT_EET;
|
---|
1011 | else if (zone == TT_UNKNOWN &&
|
---|
1012 | (rest[1] == 's' || rest[1] == 'S') &&
|
---|
1013 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1014 | zone = TT_EST;
|
---|
1015 | break;
|
---|
1016 | case 'f': case 'F':
|
---|
1017 | if (month == TT_UNKNOWN &&
|
---|
1018 | (rest[1] == 'e' || rest[1] == 'E') &&
|
---|
1019 | (rest[2] == 'b' || rest[2] == 'B'))
|
---|
1020 | month = TT_FEB;
|
---|
1021 | else if (dotw == TT_UNKNOWN &&
|
---|
1022 | (rest[1] == 'r' || rest[1] == 'R') &&
|
---|
1023 | (rest[2] == 'i' || rest[2] == 'I'))
|
---|
1024 | dotw = TT_FRI;
|
---|
1025 | break;
|
---|
1026 | case 'g': case 'G':
|
---|
1027 | if (zone == TT_UNKNOWN &&
|
---|
1028 | (rest[1] == 'm' || rest[1] == 'M') &&
|
---|
1029 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1030 | zone = TT_GMT;
|
---|
1031 | break;
|
---|
1032 | case 'j': case 'J':
|
---|
1033 | if (month == TT_UNKNOWN &&
|
---|
1034 | (rest[1] == 'a' || rest[1] == 'A') &&
|
---|
1035 | (rest[2] == 'n' || rest[2] == 'N'))
|
---|
1036 | month = TT_JAN;
|
---|
1037 | else if (zone == TT_UNKNOWN &&
|
---|
1038 | (rest[1] == 's' || rest[1] == 'S') &&
|
---|
1039 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1040 | zone = TT_JST;
|
---|
1041 | else if (month == TT_UNKNOWN &&
|
---|
1042 | (rest[1] == 'u' || rest[1] == 'U') &&
|
---|
1043 | (rest[2] == 'l' || rest[2] == 'L'))
|
---|
1044 | month = TT_JUL;
|
---|
1045 | else if (month == TT_UNKNOWN &&
|
---|
1046 | (rest[1] == 'u' || rest[1] == 'U') &&
|
---|
1047 | (rest[2] == 'n' || rest[2] == 'N'))
|
---|
1048 | month = TT_JUN;
|
---|
1049 | break;
|
---|
1050 | case 'm': case 'M':
|
---|
1051 | if (month == TT_UNKNOWN &&
|
---|
1052 | (rest[1] == 'a' || rest[1] == 'A') &&
|
---|
1053 | (rest[2] == 'r' || rest[2] == 'R'))
|
---|
1054 | month = TT_MAR;
|
---|
1055 | else if (month == TT_UNKNOWN &&
|
---|
1056 | (rest[1] == 'a' || rest[1] == 'A') &&
|
---|
1057 | (rest[2] == 'y' || rest[2] == 'Y'))
|
---|
1058 | month = TT_MAY;
|
---|
1059 | else if (zone == TT_UNKNOWN &&
|
---|
1060 | (rest[1] == 'd' || rest[1] == 'D') &&
|
---|
1061 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1062 | zone = TT_MDT;
|
---|
1063 | else if (zone == TT_UNKNOWN &&
|
---|
1064 | (rest[1] == 'e' || rest[1] == 'E') &&
|
---|
1065 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1066 | zone = TT_MET;
|
---|
1067 | else if (dotw == TT_UNKNOWN &&
|
---|
1068 | (rest[1] == 'o' || rest[1] == 'O') &&
|
---|
1069 | (rest[2] == 'n' || rest[2] == 'N'))
|
---|
1070 | dotw = TT_MON;
|
---|
1071 | else if (zone == TT_UNKNOWN &&
|
---|
1072 | (rest[1] == 's' || rest[1] == 'S') &&
|
---|
1073 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1074 | zone = TT_MST;
|
---|
1075 | break;
|
---|
1076 | case 'n': case 'N':
|
---|
1077 | if (month == TT_UNKNOWN &&
|
---|
1078 | (rest[1] == 'o' || rest[1] == 'O') &&
|
---|
1079 | (rest[2] == 'v' || rest[2] == 'V'))
|
---|
1080 | month = TT_NOV;
|
---|
1081 | else if (zone == TT_UNKNOWN &&
|
---|
1082 | (rest[1] == 's' || rest[1] == 'S') &&
|
---|
1083 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1084 | zone = TT_NST;
|
---|
1085 | break;
|
---|
1086 | case 'o': case 'O':
|
---|
1087 | if (month == TT_UNKNOWN &&
|
---|
1088 | (rest[1] == 'c' || rest[1] == 'C') &&
|
---|
1089 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1090 | month = TT_OCT;
|
---|
1091 | break;
|
---|
1092 | case 'p': case 'P':
|
---|
1093 | if (zone == TT_UNKNOWN &&
|
---|
1094 | (rest[1] == 'd' || rest[1] == 'D') &&
|
---|
1095 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1096 | zone = TT_PDT;
|
---|
1097 | else if (zone == TT_UNKNOWN &&
|
---|
1098 | (rest[1] == 's' || rest[1] == 'S') &&
|
---|
1099 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1100 | zone = TT_PST;
|
---|
1101 | break;
|
---|
1102 | case 's': case 'S':
|
---|
1103 | if (dotw == TT_UNKNOWN &&
|
---|
1104 | (rest[1] == 'a' || rest[1] == 'A') &&
|
---|
1105 | (rest[2] == 't' || rest[2] == 'T'))
|
---|
1106 | dotw = TT_SAT;
|
---|
1107 | else if (month == TT_UNKNOWN &&
|
---|
1108 | (rest[1] == 'e' || rest[1] == 'E') &&
|
---|
1109 | (rest[2] == 'p' || rest[2] == 'P'))
|
---|
1110 | month = TT_SEP;
|
---|
1111 | else if (dotw == TT_UNKNOWN &&
|
---|
1112 | (rest[1] == 'u' || rest[1] == 'U') &&
|
---|
1113 | (rest[2] == 'n' || rest[2] == 'N'))
|
---|
1114 | dotw = TT_SUN;
|
---|
1115 | break;
|
---|
1116 | case 't': case 'T':
|
---|
1117 | if (dotw == TT_UNKNOWN &&
|
---|
1118 | (rest[1] == 'h' || rest[1] == 'H') &&
|
---|
1119 | (rest[2] == 'u' || rest[2] == 'U'))
|
---|
1120 | dotw = TT_THU;
|
---|
1121 | else if (dotw == TT_UNKNOWN &&
|
---|
1122 | (rest[1] == 'u' || rest[1] == 'U') &&
|
---|
1123 | (rest[2] == 'e' || rest[2] == 'E'))
|
---|
1124 | dotw = TT_TUE;
|
---|
1125 | break;
|
---|
1126 | case 'u': case 'U':
|
---|
1127 | if (zone == TT_UNKNOWN &&
|
---|
1128 | (rest[1] == 't' || rest[1] == 'T') &&
|
---|
1129 | !(rest[2] >= 'A' && rest[2] <= 'Z') &&
|
---|
1130 | !(rest[2] >= 'a' && rest[2] <= 'z'))
|
---|
1131 | /* UT is the same as GMT but UTx is not. */
|
---|
1132 | zone = TT_GMT;
|
---|
1133 | break;
|
---|
1134 | case 'w': case 'W':
|
---|
1135 | if (dotw == TT_UNKNOWN &&
|
---|
1136 | (rest[1] == 'e' || rest[1] == 'E') &&
|
---|
1137 | (rest[2] == 'd' || rest[2] == 'D'))
|
---|
1138 | dotw = TT_WED;
|
---|
1139 | break;
|
---|
1140 |
|
---|
1141 | case '+': case '-':
|
---|
1142 | {
|
---|
1143 | const char *end;
|
---|
1144 | int sign;
|
---|
1145 | if (zone_offset != -1)
|
---|
1146 | {
|
---|
1147 | /* already got one... */
|
---|
1148 | rest++;
|
---|
1149 | break;
|
---|
1150 | }
|
---|
1151 | if (zone != TT_UNKNOWN && zone != TT_GMT)
|
---|
1152 | {
|
---|
1153 | /* GMT+0300 is legal, but PST+0300 is not. */
|
---|
1154 | rest++;
|
---|
1155 | break;
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | sign = ((*rest == '+') ? 1 : -1);
|
---|
1159 | rest++; /* move over sign */
|
---|
1160 | end = rest;
|
---|
1161 | while (*end >= '0' && *end <= '9')
|
---|
1162 | end++;
|
---|
1163 | if (rest == end) /* no digits here */
|
---|
1164 | break;
|
---|
1165 |
|
---|
1166 | if ((end - rest) == 4)
|
---|
1167 | /* offset in HHMM */
|
---|
1168 | zone_offset = (((((rest[0]-'0')*10) + (rest[1]-'0')) * 60) +
|
---|
1169 | (((rest[2]-'0')*10) + (rest[3]-'0')));
|
---|
1170 | else if ((end - rest) == 2)
|
---|
1171 | /* offset in hours */
|
---|
1172 | zone_offset = (((rest[0]-'0')*10) + (rest[1]-'0')) * 60;
|
---|
1173 | else if ((end - rest) == 1)
|
---|
1174 | /* offset in hours */
|
---|
1175 | zone_offset = (rest[0]-'0') * 60;
|
---|
1176 | else
|
---|
1177 | /* 3 or >4 */
|
---|
1178 | break;
|
---|
1179 |
|
---|
1180 | zone_offset *= sign;
|
---|
1181 | zone = TT_GMT;
|
---|
1182 | break;
|
---|
1183 | }
|
---|
1184 |
|
---|
1185 | case '0': case '1': case '2': case '3': case '4':
|
---|
1186 | case '5': case '6': case '7': case '8': case '9':
|
---|
1187 | {
|
---|
1188 | int tmp_hour = -1;
|
---|
1189 | int tmp_min = -1;
|
---|
1190 | int tmp_sec = -1;
|
---|
1191 | const char *end = rest + 1;
|
---|
1192 | while (*end >= '0' && *end <= '9')
|
---|
1193 | end++;
|
---|
1194 |
|
---|
1195 | /* end is now the first character after a range of digits. */
|
---|
1196 |
|
---|
1197 | if (*end == ':')
|
---|
1198 | {
|
---|
1199 | if (hour >= 0 && min >= 0) /* already got it */
|
---|
1200 | break;
|
---|
1201 |
|
---|
1202 | /* We have seen "[0-9]+:", so this is probably HH:MM[:SS] */
|
---|
1203 | if ((end - rest) > 2)
|
---|
1204 | /* it is [0-9][0-9][0-9]+: */
|
---|
1205 | break;
|
---|
1206 | else if ((end - rest) == 2)
|
---|
1207 | tmp_hour = ((rest[0]-'0')*10 +
|
---|
1208 | (rest[1]-'0'));
|
---|
1209 | else
|
---|
1210 | tmp_hour = (rest[0]-'0');
|
---|
1211 |
|
---|
1212 | while (*rest && *rest != ':')
|
---|
1213 | rest++;
|
---|
1214 | rest++;
|
---|
1215 |
|
---|
1216 | /* move over the colon, and parse minutes */
|
---|
1217 |
|
---|
1218 | end = rest + 1;
|
---|
1219 | while (*end >= '0' && *end <= '9')
|
---|
1220 | end++;
|
---|
1221 |
|
---|
1222 | if (end == rest)
|
---|
1223 | /* no digits after first colon? */
|
---|
1224 | break;
|
---|
1225 | else if ((end - rest) > 2)
|
---|
1226 | /* it is [0-9][0-9][0-9]+: */
|
---|
1227 | break;
|
---|
1228 | else if ((end - rest) == 2)
|
---|
1229 | tmp_min = ((rest[0]-'0')*10 +
|
---|
1230 | (rest[1]-'0'));
|
---|
1231 | else
|
---|
1232 | tmp_min = (rest[0]-'0');
|
---|
1233 |
|
---|
1234 | /* now go for seconds */
|
---|
1235 | rest = end;
|
---|
1236 | if (*rest == ':')
|
---|
1237 | rest++;
|
---|
1238 | end = rest;
|
---|
1239 | while (*end >= '0' && *end <= '9')
|
---|
1240 | end++;
|
---|
1241 |
|
---|
1242 | if (end == rest)
|
---|
1243 | /* no digits after second colon - that's ok. */
|
---|
1244 | ;
|
---|
1245 | else if ((end - rest) > 2)
|
---|
1246 | /* it is [0-9][0-9][0-9]+: */
|
---|
1247 | break;
|
---|
1248 | else if ((end - rest) == 2)
|
---|
1249 | tmp_sec = ((rest[0]-'0')*10 +
|
---|
1250 | (rest[1]-'0'));
|
---|
1251 | else
|
---|
1252 | tmp_sec = (rest[0]-'0');
|
---|
1253 |
|
---|
1254 | /* If we made it here, we've parsed hour and min,
|
---|
1255 | and possibly sec, so it worked as a unit. */
|
---|
1256 |
|
---|
1257 | /* skip over whitespace and see if there's an AM or PM
|
---|
1258 | directly following the time.
|
---|
1259 | */
|
---|
1260 | if (tmp_hour <= 12)
|
---|
1261 | {
|
---|
1262 | const char *s = end;
|
---|
1263 | while (*s && (*s == ' ' || *s == '\t'))
|
---|
1264 | s++;
|
---|
1265 | if ((s[0] == 'p' || s[0] == 'P') &&
|
---|
1266 | (s[1] == 'm' || s[1] == 'M'))
|
---|
1267 | /* 10:05pm == 22:05, and 12:05pm == 12:05 */
|
---|
1268 | tmp_hour = (tmp_hour == 12 ? 12 : tmp_hour + 12);
|
---|
1269 | else if (tmp_hour == 12 &&
|
---|
1270 | (s[0] == 'a' || s[0] == 'A') &&
|
---|
1271 | (s[1] == 'm' || s[1] == 'M'))
|
---|
1272 | /* 12:05am == 00:05 */
|
---|
1273 | tmp_hour = 0;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | hour = tmp_hour;
|
---|
1277 | min = tmp_min;
|
---|
1278 | sec = tmp_sec;
|
---|
1279 | rest = end;
|
---|
1280 | break;
|
---|
1281 | }
|
---|
1282 | else if ((*end == '/' || *end == '-') &&
|
---|
1283 | end[1] >= '0' && end[1] <= '9')
|
---|
1284 | {
|
---|
1285 | /* Perhaps this is 6/16/95, 16/6/95, 6-16-95, or 16-6-95
|
---|
1286 | or even 95-06-05...
|
---|
1287 | #### But it doesn't handle 1995-06-22.
|
---|
1288 | */
|
---|
1289 | int n1, n2, n3;
|
---|
1290 | const char *s;
|
---|
1291 |
|
---|
1292 | if (month != TT_UNKNOWN)
|
---|
1293 | /* if we saw a month name, this can't be. */
|
---|
1294 | break;
|
---|
1295 |
|
---|
1296 | s = rest;
|
---|
1297 |
|
---|
1298 | n1 = (*s++ - '0'); /* first 1 or 2 digits */
|
---|
1299 | if (*s >= '0' && *s <= '9')
|
---|
1300 | n1 = n1*10 + (*s++ - '0');
|
---|
1301 |
|
---|
1302 | if (*s != '/' && *s != '-') /* slash */
|
---|
1303 | break;
|
---|
1304 | s++;
|
---|
1305 |
|
---|
1306 | if (*s < '0' || *s > '9') /* second 1 or 2 digits */
|
---|
1307 | break;
|
---|
1308 | n2 = (*s++ - '0');
|
---|
1309 | if (*s >= '0' && *s <= '9')
|
---|
1310 | n2 = n2*10 + (*s++ - '0');
|
---|
1311 |
|
---|
1312 | if (*s != '/' && *s != '-') /* slash */
|
---|
1313 | break;
|
---|
1314 | s++;
|
---|
1315 |
|
---|
1316 | if (*s < '0' || *s > '9') /* third 1, 2, or 4 digits */
|
---|
1317 | break;
|
---|
1318 | n3 = (*s++ - '0');
|
---|
1319 | if (*s >= '0' && *s <= '9')
|
---|
1320 | n3 = n3*10 + (*s++ - '0');
|
---|
1321 |
|
---|
1322 | if (*s >= '0' && *s <= '9') /* optional digits 3 and 4 */
|
---|
1323 | {
|
---|
1324 | n3 = n3*10 + (*s++ - '0');
|
---|
1325 | if (*s < '0' || *s > '9')
|
---|
1326 | break;
|
---|
1327 | n3 = n3*10 + (*s++ - '0');
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | if ((*s >= '0' && *s <= '9') || /* followed by non-alphanum */
|
---|
1331 | (*s >= 'A' && *s <= 'Z') ||
|
---|
1332 | (*s >= 'a' && *s <= 'z'))
|
---|
1333 | break;
|
---|
1334 |
|
---|
1335 | /* Ok, we parsed three 1-2 digit numbers, with / or -
|
---|
1336 | between them. Now decide what the hell they are
|
---|
1337 | (DD/MM/YY or MM/DD/YY or YY/MM/DD.)
|
---|
1338 | */
|
---|
1339 |
|
---|
1340 | if (n1 > 31 || n1 == 0) /* must be YY/MM/DD */
|
---|
1341 | {
|
---|
1342 | if (n2 > 12) break;
|
---|
1343 | if (n3 > 31) break;
|
---|
1344 | year = n1;
|
---|
1345 | if (year < 70)
|
---|
1346 | year += 2000;
|
---|
1347 | else if (year < 100)
|
---|
1348 | year += 1900;
|
---|
1349 | month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1);
|
---|
1350 | date = n3;
|
---|
1351 | rest = s;
|
---|
1352 | break;
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | if (n1 > 12 && n2 > 12) /* illegal */
|
---|
1356 | {
|
---|
1357 | rest = s;
|
---|
1358 | break;
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | if (n3 < 70)
|
---|
1362 | n3 += 2000;
|
---|
1363 | else if (n3 < 100)
|
---|
1364 | n3 += 1900;
|
---|
1365 |
|
---|
1366 | if (n1 > 12) /* must be DD/MM/YY */
|
---|
1367 | {
|
---|
1368 | date = n1;
|
---|
1369 | month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1);
|
---|
1370 | year = n3;
|
---|
1371 | }
|
---|
1372 | else /* assume MM/DD/YY */
|
---|
1373 | {
|
---|
1374 | /* #### In the ambiguous case, should we consult the
|
---|
1375 | locale to find out the local default? */
|
---|
1376 | month = (TIME_TOKEN)(n1 + ((int)TT_JAN) - 1);
|
---|
1377 | date = n2;
|
---|
1378 | year = n3;
|
---|
1379 | }
|
---|
1380 | rest = s;
|
---|
1381 | }
|
---|
1382 | else if ((*end >= 'A' && *end <= 'Z') ||
|
---|
1383 | (*end >= 'a' && *end <= 'z'))
|
---|
1384 | /* Digits followed by non-punctuation - what's that? */
|
---|
1385 | ;
|
---|
1386 | else if ((end - rest) == 4) /* four digits is a year */
|
---|
1387 | year = (year < 0
|
---|
1388 | ? ((rest[0]-'0')*1000L +
|
---|
1389 | (rest[1]-'0')*100L +
|
---|
1390 | (rest[2]-'0')*10L +
|
---|
1391 | (rest[3]-'0'))
|
---|
1392 | : year);
|
---|
1393 | else if ((end - rest) == 2) /* two digits - date or year */
|
---|
1394 | {
|
---|
1395 | int n = ((rest[0]-'0')*10 +
|
---|
1396 | (rest[1]-'0'));
|
---|
1397 | /* If we don't have a date (day of the month) and we see a number
|
---|
1398 | less than 32, then assume that is the date.
|
---|
1399 |
|
---|
1400 | Otherwise, if we have a date and not a year, assume this is the
|
---|
1401 | year. If it is less than 70, then assume it refers to the 21st
|
---|
1402 | century. If it is two digits (>= 70), assume it refers to this
|
---|
1403 | century. Otherwise, assume it refers to an unambiguous year.
|
---|
1404 |
|
---|
1405 | The world will surely end soon.
|
---|
1406 | */
|
---|
1407 | if (date < 0 && n < 32)
|
---|
1408 | date = n;
|
---|
1409 | else if (year < 0)
|
---|
1410 | {
|
---|
1411 | if (n < 70)
|
---|
1412 | year = 2000 + n;
|
---|
1413 | else if (n < 100)
|
---|
1414 | year = 1900 + n;
|
---|
1415 | else
|
---|
1416 | year = n;
|
---|
1417 | }
|
---|
1418 | /* else what the hell is this. */
|
---|
1419 | }
|
---|
1420 | else if ((end - rest) == 1) /* one digit - date */
|
---|
1421 | date = (date < 0 ? (rest[0]-'0') : date);
|
---|
1422 | /* else, three or more than four digits - what's that? */
|
---|
1423 |
|
---|
1424 | break;
|
---|
1425 | }
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | /* Skip to the end of this token, whether we parsed it or not.
|
---|
1429 | Tokens are delimited by whitespace, or ,;-/
|
---|
1430 | But explicitly not :+-.
|
---|
1431 | */
|
---|
1432 | while (*rest &&
|
---|
1433 | *rest != ' ' && *rest != '\t' &&
|
---|
1434 | *rest != ',' && *rest != ';' &&
|
---|
1435 | *rest != '-' && *rest != '+' &&
|
---|
1436 | *rest != '/' &&
|
---|
1437 | *rest != '(' && *rest != ')' && *rest != '[' && *rest != ']')
|
---|
1438 | rest++;
|
---|
1439 | /* skip over uninteresting chars. */
|
---|
1440 | SKIP_MORE:
|
---|
1441 | while (*rest &&
|
---|
1442 | (*rest == ' ' || *rest == '\t' ||
|
---|
1443 | *rest == ',' || *rest == ';' || *rest == '/' ||
|
---|
1444 | *rest == '(' || *rest == ')' || *rest == '[' || *rest == ']'))
|
---|
1445 | rest++;
|
---|
1446 |
|
---|
1447 | /* "-" is ignored at the beginning of a token if we have not yet
|
---|
1448 | parsed a year (e.g., the second "-" in "30-AUG-1966"), or if
|
---|
1449 | the character after the dash is not a digit. */
|
---|
1450 | if (*rest == '-' && ((rest > string && isalpha(rest[-1]) && year < 0)
|
---|
1451 | || rest[1] < '0' || rest[1] > '9'))
|
---|
1452 | {
|
---|
1453 | rest++;
|
---|
1454 | goto SKIP_MORE;
|
---|
1455 | }
|
---|
1456 |
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | if (zone != TT_UNKNOWN && zone_offset == -1)
|
---|
1460 | {
|
---|
1461 | switch (zone)
|
---|
1462 | {
|
---|
1463 | case TT_PST: zone_offset = -8 * 60; break;
|
---|
1464 | case TT_PDT: zone_offset = -7 * 60; break;
|
---|
1465 | case TT_MST: zone_offset = -7 * 60; break;
|
---|
1466 | case TT_MDT: zone_offset = -6 * 60; break;
|
---|
1467 | case TT_CST: zone_offset = -6 * 60; break;
|
---|
1468 | case TT_CDT: zone_offset = -5 * 60; break;
|
---|
1469 | case TT_EST: zone_offset = -5 * 60; break;
|
---|
1470 | case TT_EDT: zone_offset = -4 * 60; break;
|
---|
1471 | case TT_AST: zone_offset = -4 * 60; break;
|
---|
1472 | case TT_NST: zone_offset = -3 * 60 - 30; break;
|
---|
1473 | case TT_GMT: zone_offset = 0 * 60; break;
|
---|
1474 | case TT_BST: zone_offset = 1 * 60; break;
|
---|
1475 | case TT_MET: zone_offset = 1 * 60; break;
|
---|
1476 | case TT_EET: zone_offset = 2 * 60; break;
|
---|
1477 | case TT_JST: zone_offset = 9 * 60; break;
|
---|
1478 | default:
|
---|
1479 | PR_ASSERT (0);
|
---|
1480 | break;
|
---|
1481 | }
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | /* If we didn't find a year, month, or day-of-the-month, we can't
|
---|
1485 | possibly parse this, and in fact, mktime() will do something random
|
---|
1486 | (I'm seeing it return "Tue Feb 5 06:28:16 2036", which is no doubt
|
---|
1487 | a numerologically significant date... */
|
---|
1488 | if (month == TT_UNKNOWN || date == -1 || year == -1)
|
---|
1489 | return PR_FAILURE;
|
---|
1490 |
|
---|
1491 | memset(&tm, 0, sizeof(tm));
|
---|
1492 | if (sec != -1)
|
---|
1493 | tm.tm_sec = sec;
|
---|
1494 | if (min != -1)
|
---|
1495 | tm.tm_min = min;
|
---|
1496 | if (hour != -1)
|
---|
1497 | tm.tm_hour = hour;
|
---|
1498 | if (date != -1)
|
---|
1499 | tm.tm_mday = date;
|
---|
1500 | if (month != TT_UNKNOWN)
|
---|
1501 | tm.tm_month = (((int)month) - ((int)TT_JAN));
|
---|
1502 | if (year != -1)
|
---|
1503 | tm.tm_year = year;
|
---|
1504 | if (dotw != TT_UNKNOWN)
|
---|
1505 | tm.tm_wday = (((int)dotw) - ((int)TT_SUN));
|
---|
1506 |
|
---|
1507 | if (zone == TT_UNKNOWN && default_to_gmt)
|
---|
1508 | {
|
---|
1509 | /* No zone was specified, so pretend the zone was GMT. */
|
---|
1510 | zone = TT_GMT;
|
---|
1511 | zone_offset = 0;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | if (zone_offset == -1)
|
---|
1515 | {
|
---|
1516 | /* no zone was specified, and we're to assume that everything
|
---|
1517 | is local. */
|
---|
1518 | struct tm localTime;
|
---|
1519 | time_t secs;
|
---|
1520 |
|
---|
1521 | PR_ASSERT(tm.tm_month > -1
|
---|
1522 | && tm.tm_mday > 0
|
---|
1523 | && tm.tm_hour > -1
|
---|
1524 | && tm.tm_min > -1
|
---|
1525 | && tm.tm_sec > -1);
|
---|
1526 |
|
---|
1527 | /*
|
---|
1528 | * To obtain time_t from a tm structure representing the local
|
---|
1529 | * time, we call mktime(). However, we need to see if we are
|
---|
1530 | * on 1-Jan-1970 or before. If we are, we can't call mktime()
|
---|
1531 | * because mktime() will crash on win16. In that case, we
|
---|
1532 | * calculate zone_offset based on the zone offset at
|
---|
1533 | * 00:00:00, 2 Jan 1970 GMT, and subtract zone_offset from the
|
---|
1534 | * date we are parsing to transform the date to GMT. We also
|
---|
1535 | * do so if mktime() returns (time_t) -1 (time out of range).
|
---|
1536 | */
|
---|
1537 |
|
---|
1538 | /* month, day, hours, mins and secs are always non-negative
|
---|
1539 | so we dont need to worry about them. */
|
---|
1540 | if(tm.tm_year >= 1970)
|
---|
1541 | {
|
---|
1542 | PRInt64 usec_per_sec;
|
---|
1543 |
|
---|
1544 | localTime.tm_sec = tm.tm_sec;
|
---|
1545 | localTime.tm_min = tm.tm_min;
|
---|
1546 | localTime.tm_hour = tm.tm_hour;
|
---|
1547 | localTime.tm_mday = tm.tm_mday;
|
---|
1548 | localTime.tm_mon = tm.tm_month;
|
---|
1549 | localTime.tm_year = tm.tm_year - 1900;
|
---|
1550 | /* Set this to -1 to tell mktime "I don't care". If you set
|
---|
1551 | it to 0 or 1, you are making assertions about whether the
|
---|
1552 | date you are handing it is in daylight savings mode or not;
|
---|
1553 | and if you're wrong, it will "fix" it for you. */
|
---|
1554 | localTime.tm_isdst = -1;
|
---|
1555 | secs = mktime(&localTime);
|
---|
1556 | if (secs != (time_t) -1)
|
---|
1557 | {
|
---|
1558 | LL_I2L(*result, secs);
|
---|
1559 | LL_I2L(usec_per_sec, PR_USEC_PER_SEC);
|
---|
1560 | LL_MUL(*result, *result, usec_per_sec);
|
---|
1561 | return PR_SUCCESS;
|
---|
1562 | }
|
---|
1563 | }
|
---|
1564 |
|
---|
1565 | /* So mktime() can't handle this case. We assume the
|
---|
1566 | zone_offset for the date we are parsing is the same as
|
---|
1567 | the zone offset on 00:00:00 2 Jan 1970 GMT. */
|
---|
1568 | secs = 86400;
|
---|
1569 | (void) MT_safe_localtime(&secs, &localTime);
|
---|
1570 | zone_offset = localTime.tm_min
|
---|
1571 | + 60 * localTime.tm_hour
|
---|
1572 | + 1440 * (localTime.tm_mday - 2);
|
---|
1573 | }
|
---|
1574 |
|
---|
1575 | /* Adjust the hours and minutes before handing them to
|
---|
1576 | PR_ImplodeTime(). Note that it's ok for them to be <0 or >24/60
|
---|
1577 |
|
---|
1578 | We adjust the time to GMT before going into PR_ImplodeTime().
|
---|
1579 | The zone_offset represents the difference between the time
|
---|
1580 | zone parsed and GMT
|
---|
1581 | */
|
---|
1582 | tm.tm_hour -= (zone_offset / 60);
|
---|
1583 | tm.tm_min -= (zone_offset % 60);
|
---|
1584 |
|
---|
1585 | *result = PR_ImplodeTime(&tm);
|
---|
1586 |
|
---|
1587 | return PR_SUCCESS;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | /*
|
---|
1591 | *******************************************************************
|
---|
1592 | *******************************************************************
|
---|
1593 | **
|
---|
1594 | ** OLD COMPATIBILITY FUNCTIONS
|
---|
1595 | **
|
---|
1596 | *******************************************************************
|
---|
1597 | *******************************************************************
|
---|
1598 | */
|
---|
1599 |
|
---|
1600 |
|
---|
1601 | /*
|
---|
1602 | *-----------------------------------------------------------------------
|
---|
1603 | *
|
---|
1604 | * PR_FormatTime --
|
---|
1605 | *
|
---|
1606 | * Format a time value into a buffer. Same semantics as strftime().
|
---|
1607 | *
|
---|
1608 | *-----------------------------------------------------------------------
|
---|
1609 | */
|
---|
1610 |
|
---|
1611 | PR_IMPLEMENT(PRUint32)
|
---|
1612 | PR_FormatTime(char *buf, int buflen, const char *fmt, const PRExplodedTime *tm)
|
---|
1613 | {
|
---|
1614 | struct tm a;
|
---|
1615 | a.tm_sec = tm->tm_sec;
|
---|
1616 | a.tm_min = tm->tm_min;
|
---|
1617 | a.tm_hour = tm->tm_hour;
|
---|
1618 | a.tm_mday = tm->tm_mday;
|
---|
1619 | a.tm_mon = tm->tm_month;
|
---|
1620 | a.tm_wday = tm->tm_wday;
|
---|
1621 | a.tm_year = tm->tm_year - 1900;
|
---|
1622 | a.tm_yday = tm->tm_yday;
|
---|
1623 | a.tm_isdst = tm->tm_params.tp_dst_offset ? 1 : 0;
|
---|
1624 |
|
---|
1625 | /*
|
---|
1626 | * On some platforms, for example SunOS 4, struct tm has two additional
|
---|
1627 | * fields: tm_zone and tm_gmtoff.
|
---|
1628 | */
|
---|
1629 |
|
---|
1630 | #if defined(SUNOS4) || (__GLIBC__ >= 2) \
|
---|
1631 | || defined(NETBSD) || defined(OPENBSD) || defined(FREEBSD) \
|
---|
1632 | || defined(DARWIN)
|
---|
1633 | a.tm_zone = NULL;
|
---|
1634 | a.tm_gmtoff = tm->tm_params.tp_gmt_offset + tm->tm_params.tp_dst_offset;
|
---|
1635 | #endif
|
---|
1636 |
|
---|
1637 | return strftime(buf, buflen, fmt, &a);
|
---|
1638 | }
|
---|
1639 |
|
---|
1640 |
|
---|
1641 | /*
|
---|
1642 | * The following string arrays and macros are used by PR_FormatTimeUSEnglish().
|
---|
1643 | */
|
---|
1644 |
|
---|
1645 | static const char* abbrevDays[] =
|
---|
1646 | {
|
---|
1647 | "Sun","Mon","Tue","Wed","Thu","Fri","Sat"
|
---|
1648 | };
|
---|
1649 |
|
---|
1650 | static const char* days[] =
|
---|
1651 | {
|
---|
1652 | "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"
|
---|
1653 | };
|
---|
1654 |
|
---|
1655 | static const char* abbrevMonths[] =
|
---|
1656 | {
|
---|
1657 | "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
---|
1658 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
---|
1659 | };
|
---|
1660 |
|
---|
1661 | static const char* months[] =
|
---|
1662 | {
|
---|
1663 | "January", "February", "March", "April", "May", "June",
|
---|
1664 | "July", "August", "September", "October", "November", "December"
|
---|
1665 | };
|
---|
1666 |
|
---|
1667 |
|
---|
1668 | /*
|
---|
1669 | * Add a single character to the given buffer, incrementing the buffer pointer
|
---|
1670 | * and decrementing the buffer size. Return 0 on error.
|
---|
1671 | */
|
---|
1672 | #define ADDCHAR( buf, bufSize, ch ) \
|
---|
1673 | do \
|
---|
1674 | { \
|
---|
1675 | if( bufSize < 1 ) \
|
---|
1676 | { \
|
---|
1677 | *(--buf) = '\0'; \
|
---|
1678 | return 0; \
|
---|
1679 | } \
|
---|
1680 | *buf++ = ch; \
|
---|
1681 | bufSize--; \
|
---|
1682 | } \
|
---|
1683 | while(0)
|
---|
1684 |
|
---|
1685 |
|
---|
1686 | /*
|
---|
1687 | * Add a string to the given buffer, incrementing the buffer pointer
|
---|
1688 | * and decrementing the buffer size appropriately. Return 0 on error.
|
---|
1689 | */
|
---|
1690 | #define ADDSTR( buf, bufSize, str ) \
|
---|
1691 | do \
|
---|
1692 | { \
|
---|
1693 | PRUint32 strSize = strlen( str ); \
|
---|
1694 | if( strSize > bufSize ) \
|
---|
1695 | { \
|
---|
1696 | if( bufSize==0 ) \
|
---|
1697 | *(--buf) = '\0'; \
|
---|
1698 | else \
|
---|
1699 | *buf = '\0'; \
|
---|
1700 | return 0; \
|
---|
1701 | } \
|
---|
1702 | memcpy(buf, str, strSize); \
|
---|
1703 | buf += strSize; \
|
---|
1704 | bufSize -= strSize; \
|
---|
1705 | } \
|
---|
1706 | while(0)
|
---|
1707 |
|
---|
1708 | /* Needed by PR_FormatTimeUSEnglish() */
|
---|
1709 | static unsigned int pr_WeekOfYear(const PRExplodedTime* time,
|
---|
1710 | unsigned int firstDayOfWeek);
|
---|
1711 |
|
---|
1712 |
|
---|
1713 | /***********************************************************************************
|
---|
1714 | *
|
---|
1715 | * Description:
|
---|
1716 | * This is a dumbed down version of strftime that will format the date in US
|
---|
1717 | * English regardless of the setting of the global locale. This functionality is
|
---|
1718 | * needed to write things like MIME headers which must always be in US English.
|
---|
1719 | *
|
---|
1720 | **********************************************************************************/
|
---|
1721 |
|
---|
1722 | PR_IMPLEMENT(PRUint32)
|
---|
1723 | PR_FormatTimeUSEnglish( char* buf, PRUint32 bufSize,
|
---|
1724 | const char* format, const PRExplodedTime* time )
|
---|
1725 | {
|
---|
1726 | char* bufPtr = buf;
|
---|
1727 | const char* fmtPtr;
|
---|
1728 | char tmpBuf[ 40 ];
|
---|
1729 | const int tmpBufSize = sizeof( tmpBuf );
|
---|
1730 |
|
---|
1731 |
|
---|
1732 | for( fmtPtr=format; *fmtPtr != '\0'; fmtPtr++ )
|
---|
1733 | {
|
---|
1734 | if( *fmtPtr != '%' )
|
---|
1735 | {
|
---|
1736 | ADDCHAR( bufPtr, bufSize, *fmtPtr );
|
---|
1737 | }
|
---|
1738 | else
|
---|
1739 | {
|
---|
1740 | switch( *(++fmtPtr) )
|
---|
1741 | {
|
---|
1742 | case '%':
|
---|
1743 | /* escaped '%' character */
|
---|
1744 | ADDCHAR( bufPtr, bufSize, '%' );
|
---|
1745 | break;
|
---|
1746 |
|
---|
1747 | case 'a':
|
---|
1748 | /* abbreviated weekday name */
|
---|
1749 | ADDSTR( bufPtr, bufSize, abbrevDays[ time->tm_wday ] );
|
---|
1750 | break;
|
---|
1751 |
|
---|
1752 | case 'A':
|
---|
1753 | /* full weekday name */
|
---|
1754 | ADDSTR( bufPtr, bufSize, days[ time->tm_wday ] );
|
---|
1755 | break;
|
---|
1756 |
|
---|
1757 | case 'b':
|
---|
1758 | /* abbreviated month name */
|
---|
1759 | ADDSTR( bufPtr, bufSize, abbrevMonths[ time->tm_month ] );
|
---|
1760 | break;
|
---|
1761 |
|
---|
1762 | case 'B':
|
---|
1763 | /* full month name */
|
---|
1764 | ADDSTR(bufPtr, bufSize, months[ time->tm_month ] );
|
---|
1765 | break;
|
---|
1766 |
|
---|
1767 | case 'c':
|
---|
1768 | /* Date and time. */
|
---|
1769 | PR_FormatTimeUSEnglish( tmpBuf, tmpBufSize, "%a %b %d %H:%M:%S %Y", time );
|
---|
1770 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1771 | break;
|
---|
1772 |
|
---|
1773 | case 'd':
|
---|
1774 | /* day of month ( 01 - 31 ) */
|
---|
1775 | PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_mday );
|
---|
1776 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1777 | break;
|
---|
1778 |
|
---|
1779 | case 'H':
|
---|
1780 | /* hour ( 00 - 23 ) */
|
---|
1781 | PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_hour );
|
---|
1782 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1783 | break;
|
---|
1784 |
|
---|
1785 | case 'I':
|
---|
1786 | /* hour ( 01 - 12 ) */
|
---|
1787 | PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",
|
---|
1788 | (time->tm_hour%12) ? time->tm_hour%12 : (PRInt32) 12 );
|
---|
1789 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1790 | break;
|
---|
1791 |
|
---|
1792 | case 'j':
|
---|
1793 | /* day number of year ( 001 - 366 ) */
|
---|
1794 | PR_snprintf(tmpBuf,tmpBufSize,"%.3d",time->tm_yday + 1);
|
---|
1795 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1796 | break;
|
---|
1797 |
|
---|
1798 | case 'm':
|
---|
1799 | /* month number ( 01 - 12 ) */
|
---|
1800 | PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_month+1);
|
---|
1801 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1802 | break;
|
---|
1803 |
|
---|
1804 | case 'M':
|
---|
1805 | /* minute ( 00 - 59 ) */
|
---|
1806 | PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_min );
|
---|
1807 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1808 | break;
|
---|
1809 |
|
---|
1810 | case 'p':
|
---|
1811 | /* locale's equivalent of either AM or PM */
|
---|
1812 | ADDSTR( bufPtr, bufSize, (time->tm_hour<12)?"AM":"PM" );
|
---|
1813 | break;
|
---|
1814 |
|
---|
1815 | case 'S':
|
---|
1816 | /* seconds ( 00 - 61 ), allows for leap seconds */
|
---|
1817 | PR_snprintf(tmpBuf,tmpBufSize,"%.2ld",time->tm_sec );
|
---|
1818 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1819 | break;
|
---|
1820 |
|
---|
1821 | case 'U':
|
---|
1822 | /* week number of year ( 00 - 53 ), Sunday is the first day of week 1 */
|
---|
1823 | PR_snprintf(tmpBuf,tmpBufSize,"%.2d", pr_WeekOfYear( time, 0 ) );
|
---|
1824 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1825 | break;
|
---|
1826 |
|
---|
1827 | case 'w':
|
---|
1828 | /* weekday number ( 0 - 6 ), Sunday = 0 */
|
---|
1829 | PR_snprintf(tmpBuf,tmpBufSize,"%d",time->tm_wday );
|
---|
1830 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1831 | break;
|
---|
1832 |
|
---|
1833 | case 'W':
|
---|
1834 | /* Week number of year ( 00 - 53 ), Monday is the first day of week 1 */
|
---|
1835 | PR_snprintf(tmpBuf,tmpBufSize,"%.2d", pr_WeekOfYear( time, 1 ) );
|
---|
1836 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1837 | break;
|
---|
1838 |
|
---|
1839 | case 'x':
|
---|
1840 | /* Date representation */
|
---|
1841 | PR_FormatTimeUSEnglish( tmpBuf, tmpBufSize, "%m/%d/%y", time );
|
---|
1842 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1843 | break;
|
---|
1844 |
|
---|
1845 | case 'X':
|
---|
1846 | /* Time representation. */
|
---|
1847 | PR_FormatTimeUSEnglish( tmpBuf, tmpBufSize, "%H:%M:%S", time );
|
---|
1848 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1849 | break;
|
---|
1850 |
|
---|
1851 | case 'y':
|
---|
1852 | /* year within century ( 00 - 99 ) */
|
---|
1853 | PR_snprintf(tmpBuf,tmpBufSize,"%.2d",time->tm_year % 100 );
|
---|
1854 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1855 | break;
|
---|
1856 |
|
---|
1857 | case 'Y':
|
---|
1858 | /* year as ccyy ( for example 1986 ) */
|
---|
1859 | PR_snprintf(tmpBuf,tmpBufSize,"%.4d",time->tm_year );
|
---|
1860 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1861 | break;
|
---|
1862 |
|
---|
1863 | case 'Z':
|
---|
1864 | /* Time zone name or no characters if no time zone exists.
|
---|
1865 | * Since time zone name is supposed to be independant of locale, we
|
---|
1866 | * defer to PR_FormatTime() for this option.
|
---|
1867 | */
|
---|
1868 | PR_FormatTime( tmpBuf, tmpBufSize, "%Z", time );
|
---|
1869 | ADDSTR( bufPtr, bufSize, tmpBuf );
|
---|
1870 | break;
|
---|
1871 |
|
---|
1872 | default:
|
---|
1873 | /* Unknown format. Simply copy format into output buffer. */
|
---|
1874 | ADDCHAR( bufPtr, bufSize, '%' );
|
---|
1875 | ADDCHAR( bufPtr, bufSize, *fmtPtr );
|
---|
1876 | break;
|
---|
1877 |
|
---|
1878 | }
|
---|
1879 | }
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | ADDCHAR( bufPtr, bufSize, '\0' );
|
---|
1883 | return (PRUint32)(bufPtr - buf - 1);
|
---|
1884 | }
|
---|
1885 |
|
---|
1886 |
|
---|
1887 |
|
---|
1888 | /***********************************************************************************
|
---|
1889 | *
|
---|
1890 | * Description:
|
---|
1891 | * Returns the week number of the year (0-53) for the given time. firstDayOfWeek
|
---|
1892 | * is the day on which the week is considered to start (0=Sun, 1=Mon, ...).
|
---|
1893 | * Week 1 starts the first time firstDayOfWeek occurs in the year. In other words,
|
---|
1894 | * a partial week at the start of the year is considered week 0.
|
---|
1895 | *
|
---|
1896 | **********************************************************************************/
|
---|
1897 |
|
---|
1898 | static unsigned int
|
---|
1899 | pr_WeekOfYear(const PRExplodedTime* time, unsigned int firstDayOfWeek)
|
---|
1900 | {
|
---|
1901 | int dayOfWeek;
|
---|
1902 | int dayOfYear;
|
---|
1903 |
|
---|
1904 | /* Get the day of the year for the given time then adjust it to represent the
|
---|
1905 | * first day of the week containing the given time.
|
---|
1906 | */
|
---|
1907 | dayOfWeek = time->tm_wday - firstDayOfWeek;
|
---|
1908 | if (dayOfWeek < 0)
|
---|
1909 | dayOfWeek += 7;
|
---|
1910 |
|
---|
1911 | dayOfYear = time->tm_yday - dayOfWeek;
|
---|
1912 |
|
---|
1913 |
|
---|
1914 | if( dayOfYear <= 0 )
|
---|
1915 | {
|
---|
1916 | /* If dayOfYear is <= 0, it is in the first partial week of the year. */
|
---|
1917 | return 0;
|
---|
1918 | }
|
---|
1919 | else
|
---|
1920 | {
|
---|
1921 | /* Count the number of full weeks ( dayOfYear / 7 ) then add a week if there
|
---|
1922 | * are any days left over ( dayOfYear % 7 ). Because we are only counting to
|
---|
1923 | * the first day of the week containing the given time, rather than to the
|
---|
1924 | * actual day representing the given time, any days in week 0 will be "absorbed"
|
---|
1925 | * as extra days in the given week.
|
---|
1926 | */
|
---|
1927 | return (dayOfYear / 7) + ( (dayOfYear % 7) == 0 ? 0 : 1 );
|
---|
1928 | }
|
---|
1929 | }
|
---|
1930 |
|
---|