Record error when CreateDir fails.
[chromium-blink-merge.git] / base / third_party / nspr / prtime.cc
blob5fc89c5e4491b2f64e2ac3f444faae00e76df3a4
1 /* Portions are Copyright (C) 2011 Google Inc */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
39 * prtime.cc --
40 * NOTE: The original nspr file name is prtime.c
42 * NSPR date and time functions
44 * CVS revision 3.37
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 * This was modified to use the Win32 SYSTEMTIME/FILETIME structures
57 * and the timezone offsets are applied to the FILETIME structure.
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
66 #include "base/logging.h"
67 #include "base/third_party/nspr/prtime.h"
68 #include "build/build_config.h"
70 #if defined(OS_WIN)
71 #include <windows.h>
72 #elif defined(OS_MACOSX)
73 #include <CoreFoundation/CoreFoundation.h>
74 #elif defined(OS_ANDROID)
75 #include <ctype.h>
76 #include "base/os_compat_android.h" // For timegm()
77 #elif defined(OS_NACL)
78 #include "base/os_compat_nacl.h" // For timegm()
79 #endif
80 #include <errno.h> /* for EINVAL */
81 #include <time.h>
83 /* Implements the Unix localtime_r() function for windows */
84 #if defined(OS_WIN)
85 static void localtime_r(const time_t* secs, struct tm* time) {
86 (void) localtime_s(time, secs);
88 #endif
91 *------------------------------------------------------------------------
93 * PR_ImplodeTime --
95 * Cf. time_t mktime(struct tm *tp)
96 * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough.
98 *------------------------------------------------------------------------
100 PRTime
101 PR_ImplodeTime(const PRExplodedTime *exploded)
103 // This is important, we want to make sure multiplications are
104 // done with the correct precision.
105 static const PRTime kSecondsToMicroseconds = static_cast<PRTime>(1000000);
106 #if defined(OS_WIN)
107 // Create the system struct representing our exploded time.
108 SYSTEMTIME st = {0};
109 FILETIME ft = {0};
110 ULARGE_INTEGER uli = {0};
112 st.wYear = exploded->tm_year;
113 st.wMonth = exploded->tm_month + 1;
114 st.wDayOfWeek = exploded->tm_wday;
115 st.wDay = exploded->tm_mday;
116 st.wHour = exploded->tm_hour;
117 st.wMinute = exploded->tm_min;
118 st.wSecond = exploded->tm_sec;
119 st.wMilliseconds = exploded->tm_usec/1000;
120 // Convert to FILETIME.
121 if (!SystemTimeToFileTime(&st, &ft)) {
122 NOTREACHED() << "Unable to convert time";
123 return 0;
125 // Apply offsets.
126 uli.LowPart = ft.dwLowDateTime;
127 uli.HighPart = ft.dwHighDateTime;
128 // Convert from Windows epoch to NSPR epoch, and 100-nanoseconds units
129 // to microsecond units.
130 PRTime result =
131 static_cast<PRTime>((uli.QuadPart / 10) - 11644473600000000i64);
132 // Adjust for time zone and dst. Convert from seconds to microseconds.
133 result -= (exploded->tm_params.tp_gmt_offset +
134 exploded->tm_params.tp_dst_offset) * kSecondsToMicroseconds;
135 return result;
136 #elif defined(OS_MACOSX)
137 // Create the system struct representing our exploded time.
138 CFGregorianDate gregorian_date;
139 gregorian_date.year = exploded->tm_year;
140 gregorian_date.month = exploded->tm_month + 1;
141 gregorian_date.day = exploded->tm_mday;
142 gregorian_date.hour = exploded->tm_hour;
143 gregorian_date.minute = exploded->tm_min;
144 gregorian_date.second = exploded->tm_sec;
146 // Compute |absolute_time| in seconds, correct for gmt and dst
147 // (note the combined offset will be negative when we need to add it), then
148 // convert to microseconds which is what PRTime expects.
149 CFAbsoluteTime absolute_time =
150 CFGregorianDateGetAbsoluteTime(gregorian_date, NULL);
151 PRTime result = static_cast<PRTime>(absolute_time);
152 result -= exploded->tm_params.tp_gmt_offset +
153 exploded->tm_params.tp_dst_offset;
154 result += kCFAbsoluteTimeIntervalSince1970; // PRTime epoch is 1970
155 result *= kSecondsToMicroseconds;
156 result += exploded->tm_usec;
157 return result;
158 #elif defined(OS_POSIX)
159 struct tm exp_tm = {0};
160 exp_tm.tm_sec = exploded->tm_sec;
161 exp_tm.tm_min = exploded->tm_min;
162 exp_tm.tm_hour = exploded->tm_hour;
163 exp_tm.tm_mday = exploded->tm_mday;
164 exp_tm.tm_mon = exploded->tm_month;
165 exp_tm.tm_year = exploded->tm_year - 1900;
167 time_t absolute_time = timegm(&exp_tm);
169 // If timegm returned -1. Since we don't pass it a time zone, the only
170 // valid case of returning -1 is 1 second before Epoch (Dec 31, 1969).
171 if (absolute_time == -1 &&
172 !(exploded->tm_year == 1969 && exploded->tm_month == 11 &&
173 exploded->tm_mday == 31 && exploded->tm_hour == 23 &&
174 exploded->tm_min == 59 && exploded->tm_sec == 59)) {
175 // If we get here, time_t must be 32 bits.
176 // Date was possibly too far in the future and would overflow. Return
177 // the most future date possible (year 2038).
178 if (exploded->tm_year >= 1970)
179 return INT_MAX * kSecondsToMicroseconds;
180 // Date was possibly too far in the past and would underflow. Return
181 // the most past date possible (year 1901).
182 return INT_MIN * kSecondsToMicroseconds;
185 PRTime result = static_cast<PRTime>(absolute_time);
186 result -= exploded->tm_params.tp_gmt_offset +
187 exploded->tm_params.tp_dst_offset;
188 result *= kSecondsToMicroseconds;
189 result += exploded->tm_usec;
190 return result;
191 #else
192 #error No PR_ImplodeTime implemented on your platform.
193 #endif
197 * The COUNT_LEAPS macro counts the number of leap years passed by
198 * till the start of the given year Y. At the start of the year 4
199 * A.D. the number of leap years passed by is 0, while at the start of
200 * the year 5 A.D. this count is 1. The number of years divisible by
201 * 100 but not divisible by 400 (the non-leap years) is deducted from
202 * the count to get the correct number of leap years.
204 * The COUNT_DAYS macro counts the number of days since 01/01/01 till the
205 * start of the given year Y. The number of days at the start of the year
206 * 1 is 0 while the number of days at the start of the year 2 is 365
207 * (which is ((2)-1) * 365) and so on. The reference point is 01/01/01
208 * midnight 00:00:00.
211 #define COUNT_LEAPS(Y) ( ((Y)-1)/4 - ((Y)-1)/100 + ((Y)-1)/400 )
212 #define COUNT_DAYS(Y) ( ((Y)-1)*365 + COUNT_LEAPS(Y) )
213 #define DAYS_BETWEEN_YEARS(A, B) (COUNT_DAYS(B) - COUNT_DAYS(A))
216 * Static variables used by functions in this file
220 * The following array contains the day of year for the last day of
221 * each month, where index 1 is January, and day 0 is January 1.
224 static const int lastDayOfMonth[2][13] = {
225 {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
226 {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
230 * The number of days in a month
233 static const PRInt8 nDays[2][12] = {
234 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
235 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
239 *-------------------------------------------------------------------------
241 * IsLeapYear --
243 * Returns 1 if the year is a leap year, 0 otherwise.
245 *-------------------------------------------------------------------------
248 static int IsLeapYear(PRInt16 year)
250 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
251 return 1;
252 else
253 return 0;
257 * 'secOffset' should be less than 86400 (i.e., a day).
258 * 'time' should point to a normalized PRExplodedTime.
261 static void
262 ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset)
264 time->tm_sec += secOffset;
266 /* Note that in this implementation we do not count leap seconds */
267 if (time->tm_sec < 0 || time->tm_sec >= 60) {
268 time->tm_min += time->tm_sec / 60;
269 time->tm_sec %= 60;
270 if (time->tm_sec < 0) {
271 time->tm_sec += 60;
272 time->tm_min--;
276 if (time->tm_min < 0 || time->tm_min >= 60) {
277 time->tm_hour += time->tm_min / 60;
278 time->tm_min %= 60;
279 if (time->tm_min < 0) {
280 time->tm_min += 60;
281 time->tm_hour--;
285 if (time->tm_hour < 0) {
286 /* Decrement mday, yday, and wday */
287 time->tm_hour += 24;
288 time->tm_mday--;
289 time->tm_yday--;
290 if (time->tm_mday < 1) {
291 time->tm_month--;
292 if (time->tm_month < 0) {
293 time->tm_month = 11;
294 time->tm_year--;
295 if (IsLeapYear(time->tm_year))
296 time->tm_yday = 365;
297 else
298 time->tm_yday = 364;
300 time->tm_mday = nDays[IsLeapYear(time->tm_year)][time->tm_month];
302 time->tm_wday--;
303 if (time->tm_wday < 0)
304 time->tm_wday = 6;
305 } else if (time->tm_hour > 23) {
306 /* Increment mday, yday, and wday */
307 time->tm_hour -= 24;
308 time->tm_mday++;
309 time->tm_yday++;
310 if (time->tm_mday >
311 nDays[IsLeapYear(time->tm_year)][time->tm_month]) {
312 time->tm_mday = 1;
313 time->tm_month++;
314 if (time->tm_month > 11) {
315 time->tm_month = 0;
316 time->tm_year++;
317 time->tm_yday = 0;
320 time->tm_wday++;
321 if (time->tm_wday > 6)
322 time->tm_wday = 0;
326 void
327 PR_NormalizeTime(PRExplodedTime *time, PRTimeParamFn params)
329 int daysInMonth;
330 PRInt32 numDays;
332 /* Get back to GMT */
333 time->tm_sec -= time->tm_params.tp_gmt_offset
334 + time->tm_params.tp_dst_offset;
335 time->tm_params.tp_gmt_offset = 0;
336 time->tm_params.tp_dst_offset = 0;
338 /* Now normalize GMT */
340 if (time->tm_usec < 0 || time->tm_usec >= 1000000) {
341 time->tm_sec += time->tm_usec / 1000000;
342 time->tm_usec %= 1000000;
343 if (time->tm_usec < 0) {
344 time->tm_usec += 1000000;
345 time->tm_sec--;
349 /* Note that we do not count leap seconds in this implementation */
350 if (time->tm_sec < 0 || time->tm_sec >= 60) {
351 time->tm_min += time->tm_sec / 60;
352 time->tm_sec %= 60;
353 if (time->tm_sec < 0) {
354 time->tm_sec += 60;
355 time->tm_min--;
359 if (time->tm_min < 0 || time->tm_min >= 60) {
360 time->tm_hour += time->tm_min / 60;
361 time->tm_min %= 60;
362 if (time->tm_min < 0) {
363 time->tm_min += 60;
364 time->tm_hour--;
368 if (time->tm_hour < 0 || time->tm_hour >= 24) {
369 time->tm_mday += time->tm_hour / 24;
370 time->tm_hour %= 24;
371 if (time->tm_hour < 0) {
372 time->tm_hour += 24;
373 time->tm_mday--;
377 /* Normalize month and year before mday */
378 if (time->tm_month < 0 || time->tm_month >= 12) {
379 time->tm_year += time->tm_month / 12;
380 time->tm_month %= 12;
381 if (time->tm_month < 0) {
382 time->tm_month += 12;
383 time->tm_year--;
387 /* Now that month and year are in proper range, normalize mday */
389 if (time->tm_mday < 1) {
390 /* mday too small */
391 do {
392 /* the previous month */
393 time->tm_month--;
394 if (time->tm_month < 0) {
395 time->tm_month = 11;
396 time->tm_year--;
398 time->tm_mday += nDays[IsLeapYear(time->tm_year)][time->tm_month];
399 } while (time->tm_mday < 1);
400 } else {
401 daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month];
402 while (time->tm_mday > daysInMonth) {
403 /* mday too large */
404 time->tm_mday -= daysInMonth;
405 time->tm_month++;
406 if (time->tm_month > 11) {
407 time->tm_month = 0;
408 time->tm_year++;
410 daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month];
414 /* Recompute yday and wday */
415 time->tm_yday = time->tm_mday +
416 lastDayOfMonth[IsLeapYear(time->tm_year)][time->tm_month];
418 numDays = DAYS_BETWEEN_YEARS(1970, time->tm_year) + time->tm_yday;
419 time->tm_wday = (numDays + 4) % 7;
420 if (time->tm_wday < 0) {
421 time->tm_wday += 7;
424 /* Recompute time parameters */
426 time->tm_params = params(time);
428 ApplySecOffset(time, time->tm_params.tp_gmt_offset
429 + time->tm_params.tp_dst_offset);
433 *------------------------------------------------------------------------
435 * PR_GMTParameters --
437 * Returns the PRTimeParameters for Greenwich Mean Time.
438 * Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0.
440 *------------------------------------------------------------------------
443 PRTimeParameters
444 PR_GMTParameters(const PRExplodedTime *gmt)
446 #if defined(XP_MAC)
447 #pragma unused (gmt)
448 #endif
450 PRTimeParameters retVal = { 0, 0 };
451 return retVal;
455 * The following code implements PR_ParseTimeString(). It is based on
456 * ns/lib/xp/xp_time.c, revision 1.25, by Jamie Zawinski <jwz@netscape.com>.
460 * We only recognize the abbreviations of a small subset of time zones
461 * in North America, Europe, and Japan.
463 * PST/PDT: Pacific Standard/Daylight Time
464 * MST/MDT: Mountain Standard/Daylight Time
465 * CST/CDT: Central Standard/Daylight Time
466 * EST/EDT: Eastern Standard/Daylight Time
467 * AST: Atlantic Standard Time
468 * NST: Newfoundland Standard Time
469 * GMT: Greenwich Mean Time
470 * BST: British Summer Time
471 * MET: Middle Europe Time
472 * EET: Eastern Europe Time
473 * JST: Japan Standard Time
476 typedef enum
478 TT_UNKNOWN,
480 TT_SUN, TT_MON, TT_TUE, TT_WED, TT_THU, TT_FRI, TT_SAT,
482 TT_JAN, TT_FEB, TT_MAR, TT_APR, TT_MAY, TT_JUN,
483 TT_JUL, TT_AUG, TT_SEP, TT_OCT, TT_NOV, TT_DEC,
485 TT_PST, TT_PDT, TT_MST, TT_MDT, TT_CST, TT_CDT, TT_EST, TT_EDT,
486 TT_AST, TT_NST, TT_GMT, TT_BST, TT_MET, TT_EET, TT_JST
487 } TIME_TOKEN;
490 * This parses a time/date string into a PRTime
491 * (microseconds after "1-Jan-1970 00:00:00 GMT").
492 * It returns PR_SUCCESS on success, and PR_FAILURE
493 * if the time/date string can't be parsed.
495 * Many formats are handled, including:
497 * 14 Apr 89 03:20:12
498 * 14 Apr 89 03:20 GMT
499 * Fri, 17 Mar 89 4:01:33
500 * Fri, 17 Mar 89 4:01 GMT
501 * Mon Jan 16 16:12 PDT 1989
502 * Mon Jan 16 16:12 +0130 1989
503 * 6 May 1992 16:41-JST (Wednesday)
504 * 22-AUG-1993 10:59:12.82
505 * 22-AUG-1993 10:59pm
506 * 22-AUG-1993 12:59am
507 * 22-AUG-1993 12:59 PM
508 * Friday, August 04, 1995 3:54 PM
509 * 06/21/95 04:24:34 PM
510 * 20/06/95 21:07
511 * 95-06-08 19:32:48 EDT
513 * If the input string doesn't contain a description of the timezone,
514 * we consult the `default_to_gmt' to decide whether the string should
515 * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
516 * The correct value for this argument depends on what standard specified
517 * the time string which you are parsing.
520 PRStatus
521 PR_ParseTimeString(
522 const char *string,
523 PRBool default_to_gmt,
524 PRTime *result_imploded)
526 PRExplodedTime tm;
527 PRExplodedTime *result = &tm;
528 TIME_TOKEN dotw = TT_UNKNOWN;
529 TIME_TOKEN month = TT_UNKNOWN;
530 TIME_TOKEN zone = TT_UNKNOWN;
531 int zone_offset = -1;
532 int dst_offset = 0;
533 int date = -1;
534 PRInt32 year = -1;
535 int hour = -1;
536 int min = -1;
537 int sec = -1;
539 const char *rest = string;
541 int iterations = 0;
543 PR_ASSERT(string && result);
544 if (!string || !result) return PR_FAILURE;
546 while (*rest)
549 if (iterations++ > 1000)
551 return PR_FAILURE;
554 switch (*rest)
556 case 'a': case 'A':
557 if (month == TT_UNKNOWN &&
558 (rest[1] == 'p' || rest[1] == 'P') &&
559 (rest[2] == 'r' || rest[2] == 'R'))
560 month = TT_APR;
561 else if (zone == TT_UNKNOWN &&
562 (rest[1] == 's' || rest[1] == 'S') &&
563 (rest[2] == 't' || rest[2] == 'T'))
564 zone = TT_AST;
565 else if (month == TT_UNKNOWN &&
566 (rest[1] == 'u' || rest[1] == 'U') &&
567 (rest[2] == 'g' || rest[2] == 'G'))
568 month = TT_AUG;
569 break;
570 case 'b': case 'B':
571 if (zone == TT_UNKNOWN &&
572 (rest[1] == 's' || rest[1] == 'S') &&
573 (rest[2] == 't' || rest[2] == 'T'))
574 zone = TT_BST;
575 break;
576 case 'c': case 'C':
577 if (zone == TT_UNKNOWN &&
578 (rest[1] == 'd' || rest[1] == 'D') &&
579 (rest[2] == 't' || rest[2] == 'T'))
580 zone = TT_CDT;
581 else if (zone == TT_UNKNOWN &&
582 (rest[1] == 's' || rest[1] == 'S') &&
583 (rest[2] == 't' || rest[2] == 'T'))
584 zone = TT_CST;
585 break;
586 case 'd': case 'D':
587 if (month == TT_UNKNOWN &&
588 (rest[1] == 'e' || rest[1] == 'E') &&
589 (rest[2] == 'c' || rest[2] == 'C'))
590 month = TT_DEC;
591 break;
592 case 'e': case 'E':
593 if (zone == TT_UNKNOWN &&
594 (rest[1] == 'd' || rest[1] == 'D') &&
595 (rest[2] == 't' || rest[2] == 'T'))
596 zone = TT_EDT;
597 else if (zone == TT_UNKNOWN &&
598 (rest[1] == 'e' || rest[1] == 'E') &&
599 (rest[2] == 't' || rest[2] == 'T'))
600 zone = TT_EET;
601 else if (zone == TT_UNKNOWN &&
602 (rest[1] == 's' || rest[1] == 'S') &&
603 (rest[2] == 't' || rest[2] == 'T'))
604 zone = TT_EST;
605 break;
606 case 'f': case 'F':
607 if (month == TT_UNKNOWN &&
608 (rest[1] == 'e' || rest[1] == 'E') &&
609 (rest[2] == 'b' || rest[2] == 'B'))
610 month = TT_FEB;
611 else if (dotw == TT_UNKNOWN &&
612 (rest[1] == 'r' || rest[1] == 'R') &&
613 (rest[2] == 'i' || rest[2] == 'I'))
614 dotw = TT_FRI;
615 break;
616 case 'g': case 'G':
617 if (zone == TT_UNKNOWN &&
618 (rest[1] == 'm' || rest[1] == 'M') &&
619 (rest[2] == 't' || rest[2] == 'T'))
620 zone = TT_GMT;
621 break;
622 case 'j': case 'J':
623 if (month == TT_UNKNOWN &&
624 (rest[1] == 'a' || rest[1] == 'A') &&
625 (rest[2] == 'n' || rest[2] == 'N'))
626 month = TT_JAN;
627 else if (zone == TT_UNKNOWN &&
628 (rest[1] == 's' || rest[1] == 'S') &&
629 (rest[2] == 't' || rest[2] == 'T'))
630 zone = TT_JST;
631 else if (month == TT_UNKNOWN &&
632 (rest[1] == 'u' || rest[1] == 'U') &&
633 (rest[2] == 'l' || rest[2] == 'L'))
634 month = TT_JUL;
635 else if (month == TT_UNKNOWN &&
636 (rest[1] == 'u' || rest[1] == 'U') &&
637 (rest[2] == 'n' || rest[2] == 'N'))
638 month = TT_JUN;
639 break;
640 case 'm': case 'M':
641 if (month == TT_UNKNOWN &&
642 (rest[1] == 'a' || rest[1] == 'A') &&
643 (rest[2] == 'r' || rest[2] == 'R'))
644 month = TT_MAR;
645 else if (month == TT_UNKNOWN &&
646 (rest[1] == 'a' || rest[1] == 'A') &&
647 (rest[2] == 'y' || rest[2] == 'Y'))
648 month = TT_MAY;
649 else if (zone == TT_UNKNOWN &&
650 (rest[1] == 'd' || rest[1] == 'D') &&
651 (rest[2] == 't' || rest[2] == 'T'))
652 zone = TT_MDT;
653 else if (zone == TT_UNKNOWN &&
654 (rest[1] == 'e' || rest[1] == 'E') &&
655 (rest[2] == 't' || rest[2] == 'T'))
656 zone = TT_MET;
657 else if (dotw == TT_UNKNOWN &&
658 (rest[1] == 'o' || rest[1] == 'O') &&
659 (rest[2] == 'n' || rest[2] == 'N'))
660 dotw = TT_MON;
661 else if (zone == TT_UNKNOWN &&
662 (rest[1] == 's' || rest[1] == 'S') &&
663 (rest[2] == 't' || rest[2] == 'T'))
664 zone = TT_MST;
665 break;
666 case 'n': case 'N':
667 if (month == TT_UNKNOWN &&
668 (rest[1] == 'o' || rest[1] == 'O') &&
669 (rest[2] == 'v' || rest[2] == 'V'))
670 month = TT_NOV;
671 else if (zone == TT_UNKNOWN &&
672 (rest[1] == 's' || rest[1] == 'S') &&
673 (rest[2] == 't' || rest[2] == 'T'))
674 zone = TT_NST;
675 break;
676 case 'o': case 'O':
677 if (month == TT_UNKNOWN &&
678 (rest[1] == 'c' || rest[1] == 'C') &&
679 (rest[2] == 't' || rest[2] == 'T'))
680 month = TT_OCT;
681 break;
682 case 'p': case 'P':
683 if (zone == TT_UNKNOWN &&
684 (rest[1] == 'd' || rest[1] == 'D') &&
685 (rest[2] == 't' || rest[2] == 'T'))
686 zone = TT_PDT;
687 else if (zone == TT_UNKNOWN &&
688 (rest[1] == 's' || rest[1] == 'S') &&
689 (rest[2] == 't' || rest[2] == 'T'))
690 zone = TT_PST;
691 break;
692 case 's': case 'S':
693 if (dotw == TT_UNKNOWN &&
694 (rest[1] == 'a' || rest[1] == 'A') &&
695 (rest[2] == 't' || rest[2] == 'T'))
696 dotw = TT_SAT;
697 else if (month == TT_UNKNOWN &&
698 (rest[1] == 'e' || rest[1] == 'E') &&
699 (rest[2] == 'p' || rest[2] == 'P'))
700 month = TT_SEP;
701 else if (dotw == TT_UNKNOWN &&
702 (rest[1] == 'u' || rest[1] == 'U') &&
703 (rest[2] == 'n' || rest[2] == 'N'))
704 dotw = TT_SUN;
705 break;
706 case 't': case 'T':
707 if (dotw == TT_UNKNOWN &&
708 (rest[1] == 'h' || rest[1] == 'H') &&
709 (rest[2] == 'u' || rest[2] == 'U'))
710 dotw = TT_THU;
711 else if (dotw == TT_UNKNOWN &&
712 (rest[1] == 'u' || rest[1] == 'U') &&
713 (rest[2] == 'e' || rest[2] == 'E'))
714 dotw = TT_TUE;
715 break;
716 case 'u': case 'U':
717 if (zone == TT_UNKNOWN &&
718 (rest[1] == 't' || rest[1] == 'T') &&
719 !(rest[2] >= 'A' && rest[2] <= 'Z') &&
720 !(rest[2] >= 'a' && rest[2] <= 'z'))
721 /* UT is the same as GMT but UTx is not. */
722 zone = TT_GMT;
723 break;
724 case 'w': case 'W':
725 if (dotw == TT_UNKNOWN &&
726 (rest[1] == 'e' || rest[1] == 'E') &&
727 (rest[2] == 'd' || rest[2] == 'D'))
728 dotw = TT_WED;
729 break;
731 case '+': case '-':
733 const char *end;
734 int sign;
735 if (zone_offset != -1)
737 /* already got one... */
738 rest++;
739 break;
741 if (zone != TT_UNKNOWN && zone != TT_GMT)
743 /* GMT+0300 is legal, but PST+0300 is not. */
744 rest++;
745 break;
748 sign = ((*rest == '+') ? 1 : -1);
749 rest++; /* move over sign */
750 end = rest;
751 while (*end >= '0' && *end <= '9')
752 end++;
753 if (rest == end) /* no digits here */
754 break;
756 if ((end - rest) == 4)
757 /* offset in HHMM */
758 zone_offset = (((((rest[0]-'0')*10) + (rest[1]-'0')) * 60) +
759 (((rest[2]-'0')*10) + (rest[3]-'0')));
760 else if ((end - rest) == 2)
761 /* offset in hours */
762 zone_offset = (((rest[0]-'0')*10) + (rest[1]-'0')) * 60;
763 else if ((end - rest) == 1)
764 /* offset in hours */
765 zone_offset = (rest[0]-'0') * 60;
766 else
767 /* 3 or >4 */
768 break;
770 zone_offset *= sign;
771 zone = TT_GMT;
772 break;
775 case '0': case '1': case '2': case '3': case '4':
776 case '5': case '6': case '7': case '8': case '9':
778 int tmp_hour = -1;
779 int tmp_min = -1;
780 int tmp_sec = -1;
781 const char *end = rest + 1;
782 while (*end >= '0' && *end <= '9')
783 end++;
785 /* end is now the first character after a range of digits. */
787 if (*end == ':')
789 if (hour >= 0 && min >= 0) /* already got it */
790 break;
792 /* We have seen "[0-9]+:", so this is probably HH:MM[:SS] */
793 if ((end - rest) > 2)
794 /* it is [0-9][0-9][0-9]+: */
795 break;
796 else if ((end - rest) == 2)
797 tmp_hour = ((rest[0]-'0')*10 +
798 (rest[1]-'0'));
799 else
800 tmp_hour = (rest[0]-'0');
802 /* move over the colon, and parse minutes */
804 rest = ++end;
805 while (*end >= '0' && *end <= '9')
806 end++;
808 if (end == rest)
809 /* no digits after first colon? */
810 break;
811 else if ((end - rest) > 2)
812 /* it is [0-9][0-9][0-9]+: */
813 break;
814 else if ((end - rest) == 2)
815 tmp_min = ((rest[0]-'0')*10 +
816 (rest[1]-'0'));
817 else
818 tmp_min = (rest[0]-'0');
820 /* now go for seconds */
821 rest = end;
822 if (*rest == ':')
823 rest++;
824 end = rest;
825 while (*end >= '0' && *end <= '9')
826 end++;
828 if (end == rest)
829 /* no digits after second colon - that's ok. */
831 else if ((end - rest) > 2)
832 /* it is [0-9][0-9][0-9]+: */
833 break;
834 else if ((end - rest) == 2)
835 tmp_sec = ((rest[0]-'0')*10 +
836 (rest[1]-'0'));
837 else
838 tmp_sec = (rest[0]-'0');
840 /* If we made it here, we've parsed hour and min,
841 and possibly sec, so it worked as a unit. */
843 /* skip over whitespace and see if there's an AM or PM
844 directly following the time.
846 if (tmp_hour <= 12)
848 const char *s = end;
849 while (*s && (*s == ' ' || *s == '\t'))
850 s++;
851 if ((s[0] == 'p' || s[0] == 'P') &&
852 (s[1] == 'm' || s[1] == 'M'))
853 /* 10:05pm == 22:05, and 12:05pm == 12:05 */
854 tmp_hour = (tmp_hour == 12 ? 12 : tmp_hour + 12);
855 else if (tmp_hour == 12 &&
856 (s[0] == 'a' || s[0] == 'A') &&
857 (s[1] == 'm' || s[1] == 'M'))
858 /* 12:05am == 00:05 */
859 tmp_hour = 0;
862 hour = tmp_hour;
863 min = tmp_min;
864 sec = tmp_sec;
865 rest = end;
866 break;
868 else if ((*end == '/' || *end == '-') &&
869 end[1] >= '0' && end[1] <= '9')
871 /* Perhaps this is 6/16/95, 16/6/95, 6-16-95, or 16-6-95
872 or even 95-06-05...
873 #### But it doesn't handle 1995-06-22.
875 int n1, n2, n3;
876 const char *s;
878 if (month != TT_UNKNOWN)
879 /* if we saw a month name, this can't be. */
880 break;
882 s = rest;
884 n1 = (*s++ - '0'); /* first 1 or 2 digits */
885 if (*s >= '0' && *s <= '9')
886 n1 = n1*10 + (*s++ - '0');
888 if (*s != '/' && *s != '-') /* slash */
889 break;
890 s++;
892 if (*s < '0' || *s > '9') /* second 1 or 2 digits */
893 break;
894 n2 = (*s++ - '0');
895 if (*s >= '0' && *s <= '9')
896 n2 = n2*10 + (*s++ - '0');
898 if (*s != '/' && *s != '-') /* slash */
899 break;
900 s++;
902 if (*s < '0' || *s > '9') /* third 1, 2, 4, or 5 digits */
903 break;
904 n3 = (*s++ - '0');
905 if (*s >= '0' && *s <= '9')
906 n3 = n3*10 + (*s++ - '0');
908 if (*s >= '0' && *s <= '9') /* optional digits 3, 4, and 5 */
910 n3 = n3*10 + (*s++ - '0');
911 if (*s < '0' || *s > '9')
912 break;
913 n3 = n3*10 + (*s++ - '0');
914 if (*s >= '0' && *s <= '9')
915 n3 = n3*10 + (*s++ - '0');
918 if ((*s >= '0' && *s <= '9') || /* followed by non-alphanum */
919 (*s >= 'A' && *s <= 'Z') ||
920 (*s >= 'a' && *s <= 'z'))
921 break;
923 /* Ok, we parsed three 1-2 digit numbers, with / or -
924 between them. Now decide what the hell they are
925 (DD/MM/YY or MM/DD/YY or YY/MM/DD.)
928 if (n1 > 31 || n1 == 0) /* must be YY/MM/DD */
930 if (n2 > 12) break;
931 if (n3 > 31) break;
932 year = n1;
933 if (year < 70)
934 year += 2000;
935 else if (year < 100)
936 year += 1900;
937 month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1);
938 date = n3;
939 rest = s;
940 break;
943 if (n1 > 12 && n2 > 12) /* illegal */
945 rest = s;
946 break;
949 if (n3 < 70)
950 n3 += 2000;
951 else if (n3 < 100)
952 n3 += 1900;
954 if (n1 > 12) /* must be DD/MM/YY */
956 date = n1;
957 month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1);
958 year = n3;
960 else /* assume MM/DD/YY */
962 /* #### In the ambiguous case, should we consult the
963 locale to find out the local default? */
964 month = (TIME_TOKEN)(n1 + ((int)TT_JAN) - 1);
965 date = n2;
966 year = n3;
968 rest = s;
970 else if ((*end >= 'A' && *end <= 'Z') ||
971 (*end >= 'a' && *end <= 'z'))
972 /* Digits followed by non-punctuation - what's that? */
974 else if ((end - rest) == 5) /* five digits is a year */
975 year = (year < 0
976 ? ((rest[0]-'0')*10000L +
977 (rest[1]-'0')*1000L +
978 (rest[2]-'0')*100L +
979 (rest[3]-'0')*10L +
980 (rest[4]-'0'))
981 : year);
982 else if ((end - rest) == 4) /* four digits is a year */
983 year = (year < 0
984 ? ((rest[0]-'0')*1000L +
985 (rest[1]-'0')*100L +
986 (rest[2]-'0')*10L +
987 (rest[3]-'0'))
988 : year);
989 else if ((end - rest) == 2) /* two digits - date or year */
991 int n = ((rest[0]-'0')*10 +
992 (rest[1]-'0'));
993 /* If we don't have a date (day of the month) and we see a number
994 less than 32, then assume that is the date.
996 Otherwise, if we have a date and not a year, assume this is the
997 year. If it is less than 70, then assume it refers to the 21st
998 century. If it is two digits (>= 70), assume it refers to this
999 century. Otherwise, assume it refers to an unambiguous year.
1001 The world will surely end soon.
1003 if (date < 0 && n < 32)
1004 date = n;
1005 else if (year < 0)
1007 if (n < 70)
1008 year = 2000 + n;
1009 else if (n < 100)
1010 year = 1900 + n;
1011 else
1012 year = n;
1014 /* else what the hell is this. */
1016 else if ((end - rest) == 1) /* one digit - date */
1017 date = (date < 0 ? (rest[0]-'0') : date);
1018 /* else, three or more than five digits - what's that? */
1020 break;
1024 /* Skip to the end of this token, whether we parsed it or not.
1025 Tokens are delimited by whitespace, or ,;-/
1026 But explicitly not :+-.
1028 while (*rest &&
1029 *rest != ' ' && *rest != '\t' &&
1030 *rest != ',' && *rest != ';' &&
1031 *rest != '-' && *rest != '+' &&
1032 *rest != '/' &&
1033 *rest != '(' && *rest != ')' && *rest != '[' && *rest != ']')
1034 rest++;
1035 /* skip over uninteresting chars. */
1036 SKIP_MORE:
1037 while (*rest &&
1038 (*rest == ' ' || *rest == '\t' ||
1039 *rest == ',' || *rest == ';' || *rest == '/' ||
1040 *rest == '(' || *rest == ')' || *rest == '[' || *rest == ']'))
1041 rest++;
1043 /* "-" is ignored at the beginning of a token if we have not yet
1044 parsed a year (e.g., the second "-" in "30-AUG-1966"), or if
1045 the character after the dash is not a digit. */
1046 if (*rest == '-' && ((rest > string &&
1047 isalpha((unsigned char)rest[-1]) && year < 0) ||
1048 rest[1] < '0' || rest[1] > '9'))
1050 rest++;
1051 goto SKIP_MORE;
1056 if (zone != TT_UNKNOWN && zone_offset == -1)
1058 switch (zone)
1060 case TT_PST: zone_offset = -8 * 60; break;
1061 case TT_PDT: zone_offset = -8 * 60; dst_offset = 1 * 60; break;
1062 case TT_MST: zone_offset = -7 * 60; break;
1063 case TT_MDT: zone_offset = -7 * 60; dst_offset = 1 * 60; break;
1064 case TT_CST: zone_offset = -6 * 60; break;
1065 case TT_CDT: zone_offset = -6 * 60; dst_offset = 1 * 60; break;
1066 case TT_EST: zone_offset = -5 * 60; break;
1067 case TT_EDT: zone_offset = -5 * 60; dst_offset = 1 * 60; break;
1068 case TT_AST: zone_offset = -4 * 60; break;
1069 case TT_NST: zone_offset = -3 * 60 - 30; break;
1070 case TT_GMT: zone_offset = 0 * 60; break;
1071 case TT_BST: zone_offset = 0 * 60; dst_offset = 1 * 60; break;
1072 case TT_MET: zone_offset = 1 * 60; break;
1073 case TT_EET: zone_offset = 2 * 60; break;
1074 case TT_JST: zone_offset = 9 * 60; break;
1075 default:
1076 PR_ASSERT (0);
1077 break;
1081 /* If we didn't find a year, month, or day-of-the-month, we can't
1082 possibly parse this, and in fact, mktime() will do something random
1083 (I'm seeing it return "Tue Feb 5 06:28:16 2036", which is no doubt
1084 a numerologically significant date... */
1085 if (month == TT_UNKNOWN || date == -1 || year == -1 || year > PR_INT16_MAX)
1086 return PR_FAILURE;
1088 memset(result, 0, sizeof(*result));
1089 if (sec != -1)
1090 result->tm_sec = sec;
1091 if (min != -1)
1092 result->tm_min = min;
1093 if (hour != -1)
1094 result->tm_hour = hour;
1095 if (date != -1)
1096 result->tm_mday = date;
1097 if (month != TT_UNKNOWN)
1098 result->tm_month = (((int)month) - ((int)TT_JAN));
1099 if (year != -1)
1100 result->tm_year = year;
1101 if (dotw != TT_UNKNOWN)
1102 result->tm_wday = (((int)dotw) - ((int)TT_SUN));
1104 * Mainly to compute wday and yday, but normalized time is also required
1105 * by the check below that works around a Visual C++ 2005 mktime problem.
1107 PR_NormalizeTime(result, PR_GMTParameters);
1108 /* The remaining work is to set the gmt and dst offsets in tm_params. */
1110 if (zone == TT_UNKNOWN && default_to_gmt)
1112 /* No zone was specified, so pretend the zone was GMT. */
1113 zone = TT_GMT;
1114 zone_offset = 0;
1117 if (zone_offset == -1)
1119 /* no zone was specified, and we're to assume that everything
1120 is local. */
1121 struct tm localTime;
1122 time_t secs;
1124 PR_ASSERT(result->tm_month > -1 &&
1125 result->tm_mday > 0 &&
1126 result->tm_hour > -1 &&
1127 result->tm_min > -1 &&
1128 result->tm_sec > -1);
1131 * To obtain time_t from a tm structure representing the local
1132 * time, we call mktime(). However, we need to see if we are
1133 * on 1-Jan-1970 or before. If we are, we can't call mktime()
1134 * because mktime() will crash on win16. In that case, we
1135 * calculate zone_offset based on the zone offset at
1136 * 00:00:00, 2 Jan 1970 GMT, and subtract zone_offset from the
1137 * date we are parsing to transform the date to GMT. We also
1138 * do so if mktime() returns (time_t) -1 (time out of range).
1141 /* month, day, hours, mins and secs are always non-negative
1142 so we dont need to worry about them. */
1143 if(result->tm_year >= 1970)
1145 PRInt64 usec_per_sec;
1147 localTime.tm_sec = result->tm_sec;
1148 localTime.tm_min = result->tm_min;
1149 localTime.tm_hour = result->tm_hour;
1150 localTime.tm_mday = result->tm_mday;
1151 localTime.tm_mon = result->tm_month;
1152 localTime.tm_year = result->tm_year - 1900;
1153 /* Set this to -1 to tell mktime "I don't care". If you set
1154 it to 0 or 1, you are making assertions about whether the
1155 date you are handing it is in daylight savings mode or not;
1156 and if you're wrong, it will "fix" it for you. */
1157 localTime.tm_isdst = -1;
1159 #if _MSC_VER == 1400 /* 1400 = Visual C++ 2005 (8.0) */
1161 * mktime will return (time_t) -1 if the input is a date
1162 * after 23:59:59, December 31, 3000, US Pacific Time (not
1163 * UTC as documented):
1164 * http://msdn.microsoft.com/en-us/library/d1y53h2a(VS.80).aspx
1165 * But if the year is 3001, mktime also invokes the invalid
1166 * parameter handler, causing the application to crash. This
1167 * problem has been reported in
1168 * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=266036.
1169 * We avoid this crash by not calling mktime if the date is
1170 * out of range. To use a simple test that works in any time
1171 * zone, we consider year 3000 out of range as well. (See
1172 * bug 480740.)
1174 if (result->tm_year >= 3000) {
1175 /* Emulate what mktime would have done. */
1176 errno = EINVAL;
1177 secs = (time_t) -1;
1178 } else {
1179 secs = mktime(&localTime);
1181 #else
1182 secs = mktime(&localTime);
1183 #endif
1184 if (secs != (time_t) -1)
1186 PRTime usecs64;
1187 LL_I2L(usecs64, secs);
1188 LL_I2L(usec_per_sec, PR_USEC_PER_SEC);
1189 LL_MUL(usecs64, usecs64, usec_per_sec);
1190 *result_imploded = usecs64;
1191 return PR_SUCCESS;
1195 /* So mktime() can't handle this case. We assume the
1196 zone_offset for the date we are parsing is the same as
1197 the zone offset on 00:00:00 2 Jan 1970 GMT. */
1198 secs = 86400;
1199 localtime_r(&secs, &localTime);
1200 zone_offset = localTime.tm_min
1201 + 60 * localTime.tm_hour
1202 + 1440 * (localTime.tm_mday - 2);
1205 result->tm_params.tp_gmt_offset = zone_offset * 60;
1206 result->tm_params.tp_dst_offset = dst_offset * 60;
1208 *result_imploded = PR_ImplodeTime(result);
1209 return PR_SUCCESS;