1 /* Portions are Copyright (C) 2011 Google Inc */
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.cc --
40 * NOTE: The original nspr file name is prtime.c
41 *
42 * NSPR date and time functions
43 *
44 * CVS revision 3.37
45 */
46
47 /*
48 * The following functions were copied from the NSPR prtime.c file.
49 * PR_ParseTimeString
50 * We inlined the new PR_ParseTimeStringToExplodedTime function to avoid
51 * copying PR_ExplodeTime and PR_LocalTimeParameters. (The PR_ExplodeTime
52 * and PR_ImplodeTime calls cancel each other out.)
53 * PR_NormalizeTime
54 * PR_GMTParameters
55 * PR_ImplodeTime
56 * Upstream implementation from
57 * http://lxr.mozilla.org/nspr/source/pr/src/misc/prtime.c#221
58 * All types and macros are defined in the base/third_party/prtime.h file.
59 * These have been copied from the following nspr files. We have only copied
60 * over the types we need.
61 * 1. prtime.h
62 * 2. prtypes.h
63 * 3. prlong.h
64 *
65 * Unit tests are in base/time/pr_time_unittest.cc.
66 */
67
68 #include "base/third_party/nspr/prtime.h"
69
70 #include "base/check.h"
71 #include "build/build_config.h"
72
73 #include <ctype.h>
74 #include <errno.h> /* for EINVAL */
75 #include <limits.h>
76 #include <stddef.h>
77 #include <string.h>
78 #include <time.h>
79
80 /*
81 * The COUNT_LEAPS macro counts the number of leap years passed by
82 * till the start of the given year Y. At the start of the year 4
83 * A.D. the number of leap years passed by is 0, while at the start of
84 * the year 5 A.D. this count is 1. The number of years divisible by
85 * 100 but not divisible by 400 (the non-leap years) is deducted from
86 * the count to get the correct number of leap years.
87 *
88 * The COUNT_DAYS macro counts the number of days since 01/01/01 till the
89 * start of the given year Y. The number of days at the start of the year
90 * 1 is 0 while the number of days at the start of the year 2 is 365
91 * (which is ((2)-1) * 365) and so on. The reference point is 01/01/01
92 * midnight 00:00:00.
93 */
94
95 #define COUNT_LEAPS(Y) (((Y)-1) / 4 - ((Y)-1) / 100 + ((Y)-1) / 400)
96 #define COUNT_DAYS(Y) (((Y)-1) * 365 + COUNT_LEAPS(Y))
97 #define DAYS_BETWEEN_YEARS(A, B) (COUNT_DAYS(B) - COUNT_DAYS(A))
98
99 /* Implements the Unix localtime_r() function for windows */
100 #if BUILDFLAG(IS_WIN)
localtime_r(const time_t * secs,struct tm * time)101 static void localtime_r(const time_t* secs, struct tm* time) {
102 (void) localtime_s(time, secs);
103 }
104 #endif
105
106 /*
107 * Static variables used by functions in this file
108 */
109
110 /*
111 * The following array contains the day of year for the last day of
112 * each month, where index 1 is January, and day 0 is January 1.
113 */
114
115 static const int lastDayOfMonth[2][13] = {
116 {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
117 {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
118 };
119
120 /*
121 * The number of days in a month
122 */
123
124 static const PRInt8 nDays[2][12] = {
125 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
126 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
127 };
128
129 /*
130 *------------------------------------------------------------------------
131 *
132 * PR_ImplodeTime --
133 *
134 * Cf. time_t mktime(struct tm *tp)
135 * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough.
136 *
137 *------------------------------------------------------------------------
138 */
139 PRTime
PR_ImplodeTime(const PRExplodedTime * exploded)140 PR_ImplodeTime(const PRExplodedTime *exploded)
141 {
142 PRExplodedTime copy;
143 PRTime retVal;
144 PRInt64 secPerDay, usecPerSec;
145 PRInt64 temp;
146 PRInt64 numSecs64;
147 PRInt32 numDays;
148 PRInt32 numSecs;
149
150 /* Normalize first. Do this on our copy */
151 copy = *exploded;
152 PR_NormalizeTime(©, PR_GMTParameters);
153
154 numDays = DAYS_BETWEEN_YEARS(1970, copy.tm_year);
155
156 numSecs = copy.tm_yday * 86400 + copy.tm_hour * 3600 + copy.tm_min * 60 +
157 copy.tm_sec;
158
159 LL_I2L(temp, numDays);
160 LL_I2L(secPerDay, 86400);
161 LL_MUL(temp, temp, secPerDay);
162 LL_I2L(numSecs64, numSecs);
163 LL_ADD(numSecs64, numSecs64, temp);
164
165 /* apply the GMT and DST offsets */
166 LL_I2L(temp, copy.tm_params.tp_gmt_offset);
167 LL_SUB(numSecs64, numSecs64, temp);
168 LL_I2L(temp, copy.tm_params.tp_dst_offset);
169 LL_SUB(numSecs64, numSecs64, temp);
170
171 LL_I2L(usecPerSec, 1000000L);
172 LL_MUL(temp, numSecs64, usecPerSec);
173 LL_I2L(retVal, copy.tm_usec);
174 LL_ADD(retVal, retVal, temp);
175
176 return retVal;
177 }
178
179 /*
180 *-------------------------------------------------------------------------
181 *
182 * IsLeapYear --
183 *
184 * Returns 1 if the year is a leap year, 0 otherwise.
185 *
186 *-------------------------------------------------------------------------
187 */
188
IsLeapYear(PRInt16 year)189 static int IsLeapYear(PRInt16 year)
190 {
191 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
192 return 1;
193 else
194 return 0;
195 }
196
197 /*
198 * 'secOffset' should be less than 86400 (i.e., a day).
199 * 'time' should point to a normalized PRExplodedTime.
200 */
201
202 static void
ApplySecOffset(PRExplodedTime * time,PRInt32 secOffset)203 ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset)
204 {
205 time->tm_sec += secOffset;
206
207 /* Note that in this implementation we do not count leap seconds */
208 if (time->tm_sec < 0 || time->tm_sec >= 60) {
209 time->tm_min += time->tm_sec / 60;
210 time->tm_sec %= 60;
211 if (time->tm_sec < 0) {
212 time->tm_sec += 60;
213 time->tm_min--;
214 }
215 }
216
217 if (time->tm_min < 0 || time->tm_min >= 60) {
218 time->tm_hour += time->tm_min / 60;
219 time->tm_min %= 60;
220 if (time->tm_min < 0) {
221 time->tm_min += 60;
222 time->tm_hour--;
223 }
224 }
225
226 if (time->tm_hour < 0) {
227 /* Decrement mday, yday, and wday */
228 time->tm_hour += 24;
229 time->tm_mday--;
230 time->tm_yday--;
231 if (time->tm_mday < 1) {
232 time->tm_month--;
233 if (time->tm_month < 0) {
234 time->tm_month = 11;
235 time->tm_year--;
236 if (IsLeapYear(time->tm_year))
237 time->tm_yday = 365;
238 else
239 time->tm_yday = 364;
240 }
241 time->tm_mday = nDays[IsLeapYear(time->tm_year)][time->tm_month];
242 }
243 time->tm_wday--;
244 if (time->tm_wday < 0)
245 time->tm_wday = 6;
246 } else if (time->tm_hour > 23) {
247 /* Increment mday, yday, and wday */
248 time->tm_hour -= 24;
249 time->tm_mday++;
250 time->tm_yday++;
251 if (time->tm_mday >
252 nDays[IsLeapYear(time->tm_year)][time->tm_month]) {
253 time->tm_mday = 1;
254 time->tm_month++;
255 if (time->tm_month > 11) {
256 time->tm_month = 0;
257 time->tm_year++;
258 time->tm_yday = 0;
259 }
260 }
261 time->tm_wday++;
262 if (time->tm_wday > 6)
263 time->tm_wday = 0;
264 }
265 }
266
267 void
PR_NormalizeTime(PRExplodedTime * time,PRTimeParamFn params)268 PR_NormalizeTime(PRExplodedTime *time, PRTimeParamFn params)
269 {
270 int daysInMonth;
271 PRInt32 numDays;
272
273 /* Get back to GMT */
274 time->tm_sec -= time->tm_params.tp_gmt_offset
275 + time->tm_params.tp_dst_offset;
276 time->tm_params.tp_gmt_offset = 0;
277 time->tm_params.tp_dst_offset = 0;
278
279 /* Now normalize GMT */
280
281 if (time->tm_usec < 0 || time->tm_usec >= 1000000) {
282 time->tm_sec += time->tm_usec / 1000000;
283 time->tm_usec %= 1000000;
284 if (time->tm_usec < 0) {
285 time->tm_usec += 1000000;
286 time->tm_sec--;
287 }
288 }
289
290 /* Note that we do not count leap seconds in this implementation */
291 if (time->tm_sec < 0 || time->tm_sec >= 60) {
292 time->tm_min += time->tm_sec / 60;
293 time->tm_sec %= 60;
294 if (time->tm_sec < 0) {
295 time->tm_sec += 60;
296 time->tm_min--;
297 }
298 }
299
300 if (time->tm_min < 0 || time->tm_min >= 60) {
301 time->tm_hour += time->tm_min / 60;
302 time->tm_min %= 60;
303 if (time->tm_min < 0) {
304 time->tm_min += 60;
305 time->tm_hour--;
306 }
307 }
308
309 if (time->tm_hour < 0 || time->tm_hour >= 24) {
310 time->tm_mday += time->tm_hour / 24;
311 time->tm_hour %= 24;
312 if (time->tm_hour < 0) {
313 time->tm_hour += 24;
314 time->tm_mday--;
315 }
316 }
317
318 /* Normalize month and year before mday */
319 if (time->tm_month < 0 || time->tm_month >= 12) {
320 time->tm_year += static_cast<PRInt16>(time->tm_month / 12);
321 time->tm_month %= 12;
322 if (time->tm_month < 0) {
323 time->tm_month += 12;
324 time->tm_year--;
325 }
326 }
327
328 /* Now that month and year are in proper range, normalize mday */
329
330 if (time->tm_mday < 1) {
331 /* mday too small */
332 do {
333 /* the previous month */
334 time->tm_month--;
335 if (time->tm_month < 0) {
336 time->tm_month = 11;
337 time->tm_year--;
338 }
339 time->tm_mday += nDays[IsLeapYear(time->tm_year)][time->tm_month];
340 } while (time->tm_mday < 1);
341 } else {
342 daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month];
343 while (time->tm_mday > daysInMonth) {
344 /* mday too large */
345 time->tm_mday -= daysInMonth;
346 time->tm_month++;
347 if (time->tm_month > 11) {
348 time->tm_month = 0;
349 time->tm_year++;
350 }
351 daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month];
352 }
353 }
354
355 /* Recompute yday and wday */
356 time->tm_yday = static_cast<PRInt16>(time->tm_mday +
357 lastDayOfMonth[IsLeapYear(time->tm_year)][time->tm_month]);
358
359 numDays = DAYS_BETWEEN_YEARS(1970, time->tm_year) + time->tm_yday;
360 time->tm_wday = (numDays + 4) % 7;
361 if (time->tm_wday < 0) {
362 time->tm_wday += 7;
363 }
364
365 /* Recompute time parameters */
366
367 time->tm_params = params(time);
368
369 ApplySecOffset(time, time->tm_params.tp_gmt_offset
370 + time->tm_params.tp_dst_offset);
371 }
372
373 /*
374 *------------------------------------------------------------------------
375 *
376 * PR_GMTParameters --
377 *
378 * Returns the PRTimeParameters for Greenwich Mean Time.
379 * Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0.
380 *
381 *------------------------------------------------------------------------
382 */
383
384 PRTimeParameters
PR_GMTParameters(const PRExplodedTime * gmt)385 PR_GMTParameters(const PRExplodedTime *gmt)
386 {
387 PRTimeParameters retVal = { 0, 0 };
388 return retVal;
389 }
390
391 /*
392 * The following code implements PR_ParseTimeString(). It is based on
393 * ns/lib/xp/xp_time.c, revision 1.25, by Jamie Zawinski <[email protected]>.
394 */
395
396 /*
397 * We only recognize the abbreviations of a small subset of time zones
398 * in North America, Europe, and Japan.
399 *
400 * PST/PDT: Pacific Standard/Daylight Time
401 * MST/MDT: Mountain Standard/Daylight Time
402 * CST/CDT: Central Standard/Daylight Time
403 * EST/EDT: Eastern Standard/Daylight Time
404 * AST: Atlantic Standard Time
405 * NST: Newfoundland Standard Time
406 * GMT: Greenwich Mean Time
407 * BST: British Summer Time
408 * MET: Middle Europe Time
409 * EET: Eastern Europe Time
410 * JST: Japan Standard Time
411 */
412
413 typedef enum
414 {
415 TT_UNKNOWN,
416
417 TT_SUN, TT_MON, TT_TUE, TT_WED, TT_THU, TT_FRI, TT_SAT,
418
419 TT_JAN, TT_FEB, TT_MAR, TT_APR, TT_MAY, TT_JUN,
420 TT_JUL, TT_AUG, TT_SEP, TT_OCT, TT_NOV, TT_DEC,
421
422 TT_PST, TT_PDT, TT_MST, TT_MDT, TT_CST, TT_CDT, TT_EST, TT_EDT,
423 TT_AST, TT_NST, TT_GMT, TT_BST, TT_MET, TT_EET, TT_JST
424 } TIME_TOKEN;
425
426 /*
427 * This parses a time/date string into a PRTime
428 * (microseconds after "1-Jan-1970 00:00:00 GMT").
429 * It returns PR_SUCCESS on success, and PR_FAILURE
430 * if the time/date string can't be parsed.
431 *
432 * Many formats are handled, including:
433 *
434 * 14 Apr 89 03:20:12
435 * 14 Apr 89 03:20 GMT
436 * Fri, 17 Mar 89 4:01:33
437 * Fri, 17 Mar 89 4:01 GMT
438 * Mon Jan 16 16:12 PDT 1989
439 * Mon Jan 16 16:12 +0130 1989
440 * 6 May 1992 16:41-JST (Wednesday)
441 * 22-AUG-1993 10:59:12.82
442 * 22-AUG-1993 10:59pm
443 * 22-AUG-1993 12:59am
444 * 22-AUG-1993 12:59 PM
445 * Friday, August 04, 1995 3:54 PM
446 * 06/21/95 04:24:34 PM
447 * 20/06/95 21:07
448 * 95-06-08 19:32:48 EDT
449 * 1995-06-17T23:11:25.342156Z
450 *
451 * If the input string doesn't contain a description of the timezone,
452 * we consult the `default_to_gmt' to decide whether the string should
453 * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
454 * The correct value for this argument depends on what standard specified
455 * the time string which you are parsing.
456 */
457
458 PRStatus
PR_ParseTimeString(const char * string,PRBool default_to_gmt,PRTime * result_imploded)459 PR_ParseTimeString(
460 const char *string,
461 PRBool default_to_gmt,
462 PRTime *result_imploded)
463 {
464 PRExplodedTime tm;
465 PRExplodedTime *result = &tm;
466 TIME_TOKEN dotw = TT_UNKNOWN;
467 TIME_TOKEN month = TT_UNKNOWN;
468 TIME_TOKEN zone = TT_UNKNOWN;
469 int zone_offset = -1;
470 int dst_offset = 0;
471 int date = -1;
472 PRInt32 year = -1;
473 int hour = -1;
474 int min = -1;
475 int sec = -1;
476 int usec = -1;
477
478 const char *rest = string;
479
480 int iterations = 0;
481
482 PR_ASSERT(string && result);
483 if (!string || !result) return PR_FAILURE;
484
485 while (*rest)
486 {
487
488 if (iterations++ > 1000)
489 {
490 return PR_FAILURE;
491 }
492
493 switch (*rest)
494 {
495 case 'a': case 'A':
496 if (month == TT_UNKNOWN &&
497 (rest[1] == 'p' || rest[1] == 'P') &&
498 (rest[2] == 'r' || rest[2] == 'R'))
499 month = TT_APR;
500 else if (zone == TT_UNKNOWN &&
501 (rest[1] == 's' || rest[1] == 'S') &&
502 (rest[2] == 't' || rest[2] == 'T'))
503 zone = TT_AST;
504 else if (month == TT_UNKNOWN &&
505 (rest[1] == 'u' || rest[1] == 'U') &&
506 (rest[2] == 'g' || rest[2] == 'G'))
507 month = TT_AUG;
508 break;
509 case 'b': case 'B':
510 if (zone == TT_UNKNOWN &&
511 (rest[1] == 's' || rest[1] == 'S') &&
512 (rest[2] == 't' || rest[2] == 'T'))
513 zone = TT_BST;
514 break;
515 case 'c': case 'C':
516 if (zone == TT_UNKNOWN &&
517 (rest[1] == 'd' || rest[1] == 'D') &&
518 (rest[2] == 't' || rest[2] == 'T'))
519 zone = TT_CDT;
520 else if (zone == TT_UNKNOWN &&
521 (rest[1] == 's' || rest[1] == 'S') &&
522 (rest[2] == 't' || rest[2] == 'T'))
523 zone = TT_CST;
524 break;
525 case 'd': case 'D':
526 if (month == TT_UNKNOWN &&
527 (rest[1] == 'e' || rest[1] == 'E') &&
528 (rest[2] == 'c' || rest[2] == 'C'))
529 month = TT_DEC;
530 break;
531 case 'e': case 'E':
532 if (zone == TT_UNKNOWN &&
533 (rest[1] == 'd' || rest[1] == 'D') &&
534 (rest[2] == 't' || rest[2] == 'T'))
535 zone = TT_EDT;
536 else if (zone == TT_UNKNOWN &&
537 (rest[1] == 'e' || rest[1] == 'E') &&
538 (rest[2] == 't' || rest[2] == 'T'))
539 zone = TT_EET;
540 else if (zone == TT_UNKNOWN &&
541 (rest[1] == 's' || rest[1] == 'S') &&
542 (rest[2] == 't' || rest[2] == 'T'))
543 zone = TT_EST;
544 break;
545 case 'f': case 'F':
546 if (month == TT_UNKNOWN &&
547 (rest[1] == 'e' || rest[1] == 'E') &&
548 (rest[2] == 'b' || rest[2] == 'B'))
549 month = TT_FEB;
550 else if (dotw == TT_UNKNOWN &&
551 (rest[1] == 'r' || rest[1] == 'R') &&
552 (rest[2] == 'i' || rest[2] == 'I'))
553 dotw = TT_FRI;
554 break;
555 case 'g': case 'G':
556 if (zone == TT_UNKNOWN &&
557 (rest[1] == 'm' || rest[1] == 'M') &&
558 (rest[2] == 't' || rest[2] == 'T'))
559 zone = TT_GMT;
560 break;
561 case 'j': case 'J':
562 if (month == TT_UNKNOWN &&
563 (rest[1] == 'a' || rest[1] == 'A') &&
564 (rest[2] == 'n' || rest[2] == 'N'))
565 month = TT_JAN;
566 else if (zone == TT_UNKNOWN &&
567 (rest[1] == 's' || rest[1] == 'S') &&
568 (rest[2] == 't' || rest[2] == 'T'))
569 zone = TT_JST;
570 else if (month == TT_UNKNOWN &&
571 (rest[1] == 'u' || rest[1] == 'U') &&
572 (rest[2] == 'l' || rest[2] == 'L'))
573 month = TT_JUL;
574 else if (month == TT_UNKNOWN &&
575 (rest[1] == 'u' || rest[1] == 'U') &&
576 (rest[2] == 'n' || rest[2] == 'N'))
577 month = TT_JUN;
578 break;
579 case 'm': case 'M':
580 if (month == TT_UNKNOWN &&
581 (rest[1] == 'a' || rest[1] == 'A') &&
582 (rest[2] == 'r' || rest[2] == 'R'))
583 month = TT_MAR;
584 else if (month == TT_UNKNOWN &&
585 (rest[1] == 'a' || rest[1] == 'A') &&
586 (rest[2] == 'y' || rest[2] == 'Y'))
587 month = TT_MAY;
588 else if (zone == TT_UNKNOWN &&
589 (rest[1] == 'd' || rest[1] == 'D') &&
590 (rest[2] == 't' || rest[2] == 'T'))
591 zone = TT_MDT;
592 else if (zone == TT_UNKNOWN &&
593 (rest[1] == 'e' || rest[1] == 'E') &&
594 (rest[2] == 't' || rest[2] == 'T'))
595 zone = TT_MET;
596 else if (dotw == TT_UNKNOWN &&
597 (rest[1] == 'o' || rest[1] == 'O') &&
598 (rest[2] == 'n' || rest[2] == 'N'))
599 dotw = TT_MON;
600 else if (zone == TT_UNKNOWN &&
601 (rest[1] == 's' || rest[1] == 'S') &&
602 (rest[2] == 't' || rest[2] == 'T'))
603 zone = TT_MST;
604 break;
605 case 'n': case 'N':
606 if (month == TT_UNKNOWN &&
607 (rest[1] == 'o' || rest[1] == 'O') &&
608 (rest[2] == 'v' || rest[2] == 'V'))
609 month = TT_NOV;
610 else if (zone == TT_UNKNOWN &&
611 (rest[1] == 's' || rest[1] == 'S') &&
612 (rest[2] == 't' || rest[2] == 'T'))
613 zone = TT_NST;
614 break;
615 case 'o': case 'O':
616 if (month == TT_UNKNOWN &&
617 (rest[1] == 'c' || rest[1] == 'C') &&
618 (rest[2] == 't' || rest[2] == 'T'))
619 month = TT_OCT;
620 break;
621 case 'p': case 'P':
622 if (zone == TT_UNKNOWN &&
623 (rest[1] == 'd' || rest[1] == 'D') &&
624 (rest[2] == 't' || rest[2] == 'T'))
625 zone = TT_PDT;
626 else if (zone == TT_UNKNOWN &&
627 (rest[1] == 's' || rest[1] == 'S') &&
628 (rest[2] == 't' || rest[2] == 'T'))
629 zone = TT_PST;
630 break;
631 case 's': case 'S':
632 if (dotw == TT_UNKNOWN &&
633 (rest[1] == 'a' || rest[1] == 'A') &&
634 (rest[2] == 't' || rest[2] == 'T'))
635 dotw = TT_SAT;
636 else if (month == TT_UNKNOWN &&
637 (rest[1] == 'e' || rest[1] == 'E') &&
638 (rest[2] == 'p' || rest[2] == 'P'))
639 month = TT_SEP;
640 else if (dotw == TT_UNKNOWN &&
641 (rest[1] == 'u' || rest[1] == 'U') &&
642 (rest[2] == 'n' || rest[2] == 'N'))
643 dotw = TT_SUN;
644 break;
645 case 't': case 'T':
646 if (dotw == TT_UNKNOWN &&
647 (rest[1] == 'h' || rest[1] == 'H') &&
648 (rest[2] == 'u' || rest[2] == 'U'))
649 dotw = TT_THU;
650 else if (dotw == TT_UNKNOWN &&
651 (rest[1] == 'u' || rest[1] == 'U') &&
652 (rest[2] == 'e' || rest[2] == 'E'))
653 dotw = TT_TUE;
654 break;
655 case 'u': case 'U':
656 if (zone == TT_UNKNOWN &&
657 (rest[1] == 't' || rest[1] == 'T') &&
658 !(rest[2] >= 'A' && rest[2] <= 'Z') &&
659 !(rest[2] >= 'a' && rest[2] <= 'z'))
660 /* UT is the same as GMT but UTx is not. */
661 zone = TT_GMT;
662 break;
663 case 'w': case 'W':
664 if (dotw == TT_UNKNOWN &&
665 (rest[1] == 'e' || rest[1] == 'E') &&
666 (rest[2] == 'd' || rest[2] == 'D'))
667 dotw = TT_WED;
668 break;
669
670 case '+': case '-':
671 {
672 const char *end;
673 int sign;
674 if (zone_offset != -1)
675 {
676 /* already got one... */
677 rest++;
678 break;
679 }
680 if (zone != TT_UNKNOWN && zone != TT_GMT)
681 {
682 /* GMT+0300 is legal, but PST+0300 is not. */
683 rest++;
684 break;
685 }
686
687 sign = ((*rest == '+') ? 1 : -1);
688 rest++; /* move over sign */
689 end = rest;
690 while (*end >= '0' && *end <= '9')
691 end++;
692 if (rest == end) /* no digits here */
693 break;
694
695 if ((end - rest) == 4)
696 /* offset in HHMM */
697 zone_offset = (((((rest[0]-'0')*10) + (rest[1]-'0')) * 60) +
698 (((rest[2]-'0')*10) + (rest[3]-'0')));
699 else if ((end - rest) == 2)
700 /* offset in hours */
701 zone_offset = (((rest[0]-'0')*10) + (rest[1]-'0')) * 60;
702 else if ((end - rest) == 1)
703 /* offset in hours */
704 zone_offset = (rest[0]-'0') * 60;
705 else
706 /* 3 or >4 */
707 break;
708
709 zone_offset *= sign;
710 zone = TT_GMT;
711 break;
712 }
713
714 case '0': case '1': case '2': case '3': case '4':
715 case '5': case '6': case '7': case '8': case '9':
716 {
717 int tmp_hour = -1;
718 int tmp_min = -1;
719 int tmp_sec = -1;
720 int tmp_usec = -1;
721 const char *end = rest + 1;
722 while (*end >= '0' && *end <= '9')
723 end++;
724
725 /* end is now the first character after a range of digits. */
726
727 if (*end == ':')
728 {
729 if (hour >= 0 && min >= 0) /* already got it */
730 break;
731
732 /* We have seen "[0-9]+:", so this is probably HH:MM[:SS] */
733 if ((end - rest) > 2)
734 /* it is [0-9][0-9][0-9]+: */
735 break;
736 else if ((end - rest) == 2)
737 tmp_hour = ((rest[0]-'0')*10 +
738 (rest[1]-'0'));
739 else
740 tmp_hour = (rest[0]-'0');
741
742 /* move over the colon, and parse minutes */
743
744 rest = ++end;
745 while (*end >= '0' && *end <= '9')
746 end++;
747
748 if (end == rest)
749 /* no digits after first colon? */
750 break;
751 else if ((end - rest) > 2)
752 /* it is [0-9][0-9][0-9]+: */
753 break;
754 else if ((end - rest) == 2)
755 tmp_min = ((rest[0]-'0')*10 +
756 (rest[1]-'0'));
757 else
758 tmp_min = (rest[0]-'0');
759
760 /* now go for seconds */
761 rest = end;
762 if (*rest == ':')
763 rest++;
764 end = rest;
765 while (*end >= '0' && *end <= '9')
766 end++;
767
768 if (end == rest)
769 /* no digits after second colon - that's ok. */
770 ;
771 else if ((end - rest) > 2)
772 /* it is [0-9][0-9][0-9]+: */
773 break;
774 else if ((end - rest) == 2)
775 tmp_sec = ((rest[0]-'0')*10 +
776 (rest[1]-'0'));
777 else
778 tmp_sec = (rest[0]-'0');
779
780 /* fractional second */
781 rest = end;
782 if (*rest == '.')
783 {
784 rest++;
785 end++;
786 tmp_usec = 0;
787 /* use up to 6 digits, skip over the rest */
788 while (*end >= '0' && *end <= '9')
789 {
790 if (end - rest < 6)
791 tmp_usec = tmp_usec * 10 + *end - '0';
792 end++;
793 }
794 ptrdiff_t ndigits = end - rest;
795 while (ndigits++ < 6)
796 tmp_usec *= 10;
797 rest = end;
798 }
799
800 if (*rest == 'Z')
801 {
802 zone = TT_GMT;
803 rest++;
804 }
805 else if (tmp_hour <= 12)
806 {
807 /* If we made it here, we've parsed hour and min,
808 and possibly sec, so the current token is a time.
809 Now skip over whitespace and see if there's an AM
810 or PM directly following the time.
811 */
812 const char *s = end;
813 while (*s && (*s == ' ' || *s == '\t'))
814 s++;
815 if ((s[0] == 'p' || s[0] == 'P') &&
816 (s[1] == 'm' || s[1] == 'M'))
817 /* 10:05pm == 22:05, and 12:05pm == 12:05 */
818 tmp_hour = (tmp_hour == 12 ? 12 : tmp_hour + 12);
819 else if (tmp_hour == 12 &&
820 (s[0] == 'a' || s[0] == 'A') &&
821 (s[1] == 'm' || s[1] == 'M'))
822 /* 12:05am == 00:05 */
823 tmp_hour = 0;
824 }
825
826 hour = tmp_hour;
827 min = tmp_min;
828 sec = tmp_sec;
829 usec = tmp_usec;
830 rest = end;
831 break;
832 }
833 else if ((*end == '/' || *end == '-') &&
834 end[1] >= '0' && end[1] <= '9')
835 {
836 /* Perhaps this is 6/16/95, 16/6/95, 6-16-95, or 16-6-95
837 or even 95-06-05 or 1995-06-22.
838 */
839 int n1, n2, n3;
840 const char *s;
841
842 if (month != TT_UNKNOWN)
843 /* if we saw a month name, this can't be. */
844 break;
845
846 s = rest;
847
848 n1 = (*s++ - '0'); /* first 1, 2 or 4 digits */
849 if (*s >= '0' && *s <= '9')
850 {
851 n1 = n1*10 + (*s++ - '0');
852
853 if (*s >= '0' && *s <= '9') /* optional digits 3 and 4 */
854 {
855 n1 = n1*10 + (*s++ - '0');
856 if (*s < '0' || *s > '9')
857 break;
858 n1 = n1*10 + (*s++ - '0');
859 }
860 }
861
862 if (*s != '/' && *s != '-') /* slash */
863 break;
864 s++;
865
866 if (*s < '0' || *s > '9') /* second 1 or 2 digits */
867 break;
868 n2 = (*s++ - '0');
869 if (*s >= '0' && *s <= '9')
870 n2 = n2*10 + (*s++ - '0');
871
872 if (*s != '/' && *s != '-') /* slash */
873 break;
874 s++;
875
876 if (*s < '0' || *s > '9') /* third 1, 2, 4, or 5 digits */
877 break;
878 n3 = (*s++ - '0');
879 if (*s >= '0' && *s <= '9')
880 n3 = n3*10 + (*s++ - '0');
881
882 if (*s >= '0' && *s <= '9') /* optional digits 3, 4, and 5 */
883 {
884 n3 = n3*10 + (*s++ - '0');
885 if (*s < '0' || *s > '9')
886 break;
887 n3 = n3*10 + (*s++ - '0');
888 if (*s >= '0' && *s <= '9')
889 n3 = n3*10 + (*s++ - '0');
890 }
891
892 if (*s == 'T' && s[1] >= '0' && s[1] <= '9')
893 /* followed by ISO 8601 T delimiter and number is ok */
894 ;
895 else if ((*s >= '0' && *s <= '9') ||
896 (*s >= 'A' && *s <= 'Z') ||
897 (*s >= 'a' && *s <= 'z'))
898 /* but other alphanumerics are not ok */
899 break;
900
901 /* Ok, we parsed three multi-digit numbers, with / or -
902 between them. Now decide what the hell they are
903 (DD/MM/YY or MM/DD/YY or [YY]YY/MM/DD.)
904 */
905
906 if (n1 > 31 || n1 == 0) /* must be [YY]YY/MM/DD */
907 {
908 if (n2 > 12) break;
909 if (n3 > 31) break;
910 year = n1;
911 if (year < 70)
912 year += 2000;
913 else if (year < 100)
914 year += 1900;
915 month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1);
916 date = n3;
917 rest = s;
918 break;
919 }
920
921 if (n1 > 12 && n2 > 12) /* illegal */
922 {
923 rest = s;
924 break;
925 }
926
927 if (n3 < 70)
928 n3 += 2000;
929 else if (n3 < 100)
930 n3 += 1900;
931
932 if (n1 > 12) /* must be DD/MM/YY */
933 {
934 date = n1;
935 month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1);
936 year = n3;
937 }
938 else /* assume MM/DD/YY */
939 {
940 /* #### In the ambiguous case, should we consult the
941 locale to find out the local default? */
942 month = (TIME_TOKEN)(n1 + ((int)TT_JAN) - 1);
943 date = n2;
944 year = n3;
945 }
946 rest = s;
947 }
948 else if ((*end >= 'A' && *end <= 'Z') ||
949 (*end >= 'a' && *end <= 'z'))
950 /* Digits followed by non-punctuation - what's that? */
951 ;
952 else if ((end - rest) == 5) /* five digits is a year */
953 year = (year < 0
954 ? ((rest[0]-'0')*10000L +
955 (rest[1]-'0')*1000L +
956 (rest[2]-'0')*100L +
957 (rest[3]-'0')*10L +
958 (rest[4]-'0'))
959 : year);
960 else if ((end - rest) == 4) /* four digits is a year */
961 year = (year < 0
962 ? ((rest[0]-'0')*1000L +
963 (rest[1]-'0')*100L +
964 (rest[2]-'0')*10L +
965 (rest[3]-'0'))
966 : year);
967 else if ((end - rest) == 2) /* two digits - date or year */
968 {
969 int n = ((rest[0]-'0')*10 +
970 (rest[1]-'0'));
971 /* If we don't have a date (day of the month) and we see a number
972 less than 32, then assume that is the date.
973
974 Otherwise, if we have a date and not a year, assume this is the
975 year. If it is less than 70, then assume it refers to the 21st
976 century. If it is two digits (>= 70), assume it refers to this
977 century. Otherwise, assume it refers to an unambiguous year.
978
979 The world will surely end soon.
980 */
981 if (date < 0 && n < 32)
982 date = n;
983 else if (year < 0)
984 {
985 if (n < 70)
986 year = 2000 + n;
987 else if (n < 100)
988 year = 1900 + n;
989 else
990 year = n;
991 }
992 /* else what the hell is this. */
993 }
994 else if ((end - rest) == 1) /* one digit - date */
995 date = (date < 0 ? (rest[0]-'0') : date);
996 /* else, three or more than five digits - what's that? */
997
998 break;
999 } /* case '0' .. '9' */
1000 } /* switch */
1001
1002 /* Skip to the end of this token, whether we parsed it or not.
1003 Tokens are delimited by whitespace, or ,;-+/()[] but explicitly not .:
1004 'T' is also treated as delimiter when followed by a digit (ISO 8601).
1005 */
1006 while (*rest &&
1007 *rest != ' ' && *rest != '\t' &&
1008 *rest != ',' && *rest != ';' &&
1009 *rest != '-' && *rest != '+' &&
1010 *rest != '/' &&
1011 *rest != '(' && *rest != ')' && *rest != '[' && *rest != ']' &&
1012 !(*rest == 'T' && rest[1] >= '0' && rest[1] <= '9')
1013 )
1014 rest++;
1015 /* skip over uninteresting chars. */
1016 SKIP_MORE:
1017 while (*rest == ' ' || *rest == '\t' ||
1018 *rest == ',' || *rest == ';' || *rest == '/' ||
1019 *rest == '(' || *rest == ')' || *rest == '[' || *rest == ']')
1020 rest++;
1021
1022 /* "-" is ignored at the beginning of a token if we have not yet
1023 parsed a year (e.g., the second "-" in "30-AUG-1966"), or if
1024 the character after the dash is not a digit. */
1025 if (*rest == '-' && ((rest > string &&
1026 isalpha((unsigned char)rest[-1]) && year < 0) ||
1027 rest[1] < '0' || rest[1] > '9'))
1028 {
1029 rest++;
1030 goto SKIP_MORE;
1031 }
1032
1033 /* Skip T that may precede ISO 8601 time. */
1034 if (*rest == 'T' && rest[1] >= '0' && rest[1] <= '9')
1035 rest++;
1036 } /* while */
1037
1038 if (zone != TT_UNKNOWN && zone_offset == -1)
1039 {
1040 switch (zone)
1041 {
1042 case TT_PST: zone_offset = -8 * 60; break;
1043 case TT_PDT: zone_offset = -8 * 60; dst_offset = 1 * 60; break;
1044 case TT_MST: zone_offset = -7 * 60; break;
1045 case TT_MDT: zone_offset = -7 * 60; dst_offset = 1 * 60; break;
1046 case TT_CST: zone_offset = -6 * 60; break;
1047 case TT_CDT: zone_offset = -6 * 60; dst_offset = 1 * 60; break;
1048 case TT_EST: zone_offset = -5 * 60; break;
1049 case TT_EDT: zone_offset = -5 * 60; dst_offset = 1 * 60; break;
1050 case TT_AST: zone_offset = -4 * 60; break;
1051 case TT_NST: zone_offset = -3 * 60 - 30; break;
1052 case TT_GMT: zone_offset = 0 * 60; break;
1053 case TT_BST: zone_offset = 0 * 60; dst_offset = 1 * 60; break;
1054 case TT_MET: zone_offset = 1 * 60; break;
1055 case TT_EET: zone_offset = 2 * 60; break;
1056 case TT_JST: zone_offset = 9 * 60; break;
1057 default:
1058 PR_ASSERT (0);
1059 break;
1060 }
1061 }
1062
1063 /* If we didn't find a year, month, or day-of-the-month, we can't
1064 possibly parse this, and in fact, mktime() will do something random
1065 (I'm seeing it return "Tue Feb 5 06:28:16 2036", which is no doubt
1066 a numerologically significant date... */
1067 if (month == TT_UNKNOWN || date == -1 || year == -1 || year > PR_INT16_MAX)
1068 return PR_FAILURE;
1069
1070 memset(result, 0, sizeof(*result));
1071 if (usec != -1)
1072 result->tm_usec = usec;
1073 if (sec != -1)
1074 result->tm_sec = sec;
1075 if (min != -1)
1076 result->tm_min = min;
1077 if (hour != -1)
1078 result->tm_hour = hour;
1079 if (date != -1)
1080 result->tm_mday = date;
1081 if (month != TT_UNKNOWN)
1082 result->tm_month = (((int)month) - ((int)TT_JAN));
1083 if (year != -1)
1084 result->tm_year = static_cast<PRInt16>(year);
1085 if (dotw != TT_UNKNOWN)
1086 result->tm_wday = static_cast<PRInt8>(((int)dotw) - ((int)TT_SUN));
1087 /*
1088 * Mainly to compute wday and yday, but normalized time is also required
1089 * by the check below that works around a Visual C++ 2005 mktime problem.
1090 */
1091 PR_NormalizeTime(result, PR_GMTParameters);
1092 /* The remaining work is to set the gmt and dst offsets in tm_params. */
1093
1094 if (zone == TT_UNKNOWN && default_to_gmt)
1095 {
1096 /* No zone was specified, so pretend the zone was GMT. */
1097 zone = TT_GMT;
1098 zone_offset = 0;
1099 }
1100
1101 if (zone_offset == -1)
1102 {
1103 /* no zone was specified, and we're to assume that everything
1104 is local. */
1105 struct tm localTime;
1106 time_t secs;
1107
1108 PR_ASSERT(result->tm_month > -1 &&
1109 result->tm_mday > 0 &&
1110 result->tm_hour > -1 &&
1111 result->tm_min > -1 &&
1112 result->tm_sec > -1);
1113
1114 /*
1115 * To obtain time_t from a tm structure representing the local
1116 * time, we call mktime(). However, we need to see if we are
1117 * on 1-Jan-1970 or before. If we are, we can't call mktime()
1118 * because mktime() will crash on win16. In that case, we
1119 * calculate zone_offset based on the zone offset at
1120 * 00:00:00, 2 Jan 1970 GMT, and subtract zone_offset from the
1121 * date we are parsing to transform the date to GMT. We also
1122 * do so if mktime() returns (time_t) -1 (time out of range).
1123 */
1124
1125 /* month, day, hours, mins and secs are always non-negative
1126 so we dont need to worry about them. */
1127 if (result->tm_year >= 1970)
1128 {
1129 localTime.tm_sec = result->tm_sec;
1130 localTime.tm_min = result->tm_min;
1131 localTime.tm_hour = result->tm_hour;
1132 localTime.tm_mday = result->tm_mday;
1133 localTime.tm_mon = result->tm_month;
1134 localTime.tm_year = result->tm_year - 1900;
1135 /* Set this to -1 to tell mktime "I don't care". If you set
1136 it to 0 or 1, you are making assertions about whether the
1137 date you are handing it is in daylight savings mode or not;
1138 and if you're wrong, it will "fix" it for you. */
1139 localTime.tm_isdst = -1;
1140
1141 #if _MSC_VER == 1400 /* 1400 = Visual C++ 2005 (8.0) */
1142 /*
1143 * mktime will return (time_t) -1 if the input is a date
1144 * after 23:59:59, December 31, 3000, US Pacific Time (not
1145 * UTC as documented):
1146 * http://msdn.microsoft.com/en-us/library/d1y53h2a(VS.80).aspx
1147 * But if the year is 3001, mktime also invokes the invalid
1148 * parameter handler, causing the application to crash. This
1149 * problem has been reported in
1150 * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=266036.
1151 * We avoid this crash by not calling mktime if the date is
1152 * out of range. To use a simple test that works in any time
1153 * zone, we consider year 3000 out of range as well. (See
1154 * bug 480740.)
1155 */
1156 if (result->tm_year >= 3000) {
1157 /* Emulate what mktime would have done. */
1158 errno = EINVAL;
1159 secs = (time_t) -1;
1160 } else {
1161 secs = mktime(&localTime);
1162 }
1163 #else
1164 secs = mktime(&localTime);
1165 #endif
1166 if (secs != (time_t) -1)
1167 {
1168 *result_imploded = secs * (PRTime)PR_USEC_PER_SEC;
1169 *result_imploded += result->tm_usec;
1170 return PR_SUCCESS;
1171 }
1172 }
1173
1174 /* So mktime() can't handle this case. We assume the
1175 zone_offset for the date we are parsing is the same as
1176 the zone offset on 00:00:00 2 Jan 1970 GMT. */
1177 secs = 86400;
1178 localtime_r(&secs, &localTime);
1179 zone_offset = localTime.tm_min
1180 + 60 * localTime.tm_hour
1181 + 1440 * (localTime.tm_mday - 2);
1182 }
1183
1184 result->tm_params.tp_gmt_offset = zone_offset * 60;
1185 result->tm_params.tp_dst_offset = dst_offset * 60;
1186
1187 *result_imploded = PR_ImplodeTime(result);
1188 return PR_SUCCESS;
1189 }
1190