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
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.
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 ***** */
40 * NOTE: The original nspr file name is prtime.c
42 * NSPR date and time functions
48 * The following functions were copied from the NSPR prtime.c file.
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.)
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.
66 #include "base/logging.h"
67 #include "base/third_party/nspr/prtime.h"
68 #include "build/build_config.h"
72 #elif defined(OS_MACOSX)
73 #include <CoreFoundation/CoreFoundation.h>
74 #elif defined(OS_ANDROID)
76 #include "base/os_compat_android.h" // For timegm()
77 #elif defined(OS_NACL)
78 #include "base/os_compat_nacl.h" // For timegm()
80 #include <errno.h> /* for EINVAL */
83 /* Implements the Unix localtime_r() function for windows */
85 static void localtime_r(const time_t* secs
, struct tm
* time
) {
86 (void) localtime_s(time
, secs
);
91 *------------------------------------------------------------------------
95 * Cf. time_t mktime(struct tm *tp)
96 * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough.
98 *------------------------------------------------------------------------
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);
107 // Create the system struct representing our exploded time.
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";
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.
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
;
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
;
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
;
192 #error No PR_ImplodeTime implemented on your platform.
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
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 *-------------------------------------------------------------------------
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)
257 * 'secOffset' should be less than 86400 (i.e., a day).
258 * 'time' should point to a normalized PRExplodedTime.
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;
270 if (time
->tm_sec
< 0) {
276 if (time
->tm_min
< 0 || time
->tm_min
>= 60) {
277 time
->tm_hour
+= time
->tm_min
/ 60;
279 if (time
->tm_min
< 0) {
285 if (time
->tm_hour
< 0) {
286 /* Decrement mday, yday, and wday */
290 if (time
->tm_mday
< 1) {
292 if (time
->tm_month
< 0) {
295 if (IsLeapYear(time
->tm_year
))
300 time
->tm_mday
= nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
];
303 if (time
->tm_wday
< 0)
305 } else if (time
->tm_hour
> 23) {
306 /* Increment mday, yday, and wday */
311 nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
]) {
314 if (time
->tm_month
> 11) {
321 if (time
->tm_wday
> 6)
327 PR_NormalizeTime(PRExplodedTime
*time
, PRTimeParamFn params
)
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;
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;
353 if (time
->tm_sec
< 0) {
359 if (time
->tm_min
< 0 || time
->tm_min
>= 60) {
360 time
->tm_hour
+= time
->tm_min
/ 60;
362 if (time
->tm_min
< 0) {
368 if (time
->tm_hour
< 0 || time
->tm_hour
>= 24) {
369 time
->tm_mday
+= time
->tm_hour
/ 24;
371 if (time
->tm_hour
< 0) {
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;
387 /* Now that month and year are in proper range, normalize mday */
389 if (time
->tm_mday
< 1) {
392 /* the previous month */
394 if (time
->tm_month
< 0) {
398 time
->tm_mday
+= nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
];
399 } while (time
->tm_mday
< 1);
401 daysInMonth
= nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
];
402 while (time
->tm_mday
> daysInMonth
) {
404 time
->tm_mday
-= daysInMonth
;
406 if (time
->tm_month
> 11) {
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) {
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 *------------------------------------------------------------------------
444 PR_GMTParameters(const PRExplodedTime
*gmt
)
450 PRTimeParameters retVal
= { 0, 0 };
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
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
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:
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
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.
523 PRBool default_to_gmt
,
524 PRTime
*result_imploded
)
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;
539 const char *rest
= string
;
543 PR_ASSERT(string
&& result
);
544 if (!string
|| !result
) return PR_FAILURE
;
549 if (iterations
++ > 1000)
557 if (month
== TT_UNKNOWN
&&
558 (rest
[1] == 'p' || rest
[1] == 'P') &&
559 (rest
[2] == 'r' || rest
[2] == 'R'))
561 else if (zone
== TT_UNKNOWN
&&
562 (rest
[1] == 's' || rest
[1] == 'S') &&
563 (rest
[2] == 't' || rest
[2] == 'T'))
565 else if (month
== TT_UNKNOWN
&&
566 (rest
[1] == 'u' || rest
[1] == 'U') &&
567 (rest
[2] == 'g' || rest
[2] == 'G'))
571 if (zone
== TT_UNKNOWN
&&
572 (rest
[1] == 's' || rest
[1] == 'S') &&
573 (rest
[2] == 't' || rest
[2] == 'T'))
577 if (zone
== TT_UNKNOWN
&&
578 (rest
[1] == 'd' || rest
[1] == 'D') &&
579 (rest
[2] == 't' || rest
[2] == 'T'))
581 else if (zone
== TT_UNKNOWN
&&
582 (rest
[1] == 's' || rest
[1] == 'S') &&
583 (rest
[2] == 't' || rest
[2] == 'T'))
587 if (month
== TT_UNKNOWN
&&
588 (rest
[1] == 'e' || rest
[1] == 'E') &&
589 (rest
[2] == 'c' || rest
[2] == 'C'))
593 if (zone
== TT_UNKNOWN
&&
594 (rest
[1] == 'd' || rest
[1] == 'D') &&
595 (rest
[2] == 't' || rest
[2] == 'T'))
597 else if (zone
== TT_UNKNOWN
&&
598 (rest
[1] == 'e' || rest
[1] == 'E') &&
599 (rest
[2] == 't' || rest
[2] == 'T'))
601 else if (zone
== TT_UNKNOWN
&&
602 (rest
[1] == 's' || rest
[1] == 'S') &&
603 (rest
[2] == 't' || rest
[2] == 'T'))
607 if (month
== TT_UNKNOWN
&&
608 (rest
[1] == 'e' || rest
[1] == 'E') &&
609 (rest
[2] == 'b' || rest
[2] == 'B'))
611 else if (dotw
== TT_UNKNOWN
&&
612 (rest
[1] == 'r' || rest
[1] == 'R') &&
613 (rest
[2] == 'i' || rest
[2] == 'I'))
617 if (zone
== TT_UNKNOWN
&&
618 (rest
[1] == 'm' || rest
[1] == 'M') &&
619 (rest
[2] == 't' || rest
[2] == 'T'))
623 if (month
== TT_UNKNOWN
&&
624 (rest
[1] == 'a' || rest
[1] == 'A') &&
625 (rest
[2] == 'n' || rest
[2] == 'N'))
627 else if (zone
== TT_UNKNOWN
&&
628 (rest
[1] == 's' || rest
[1] == 'S') &&
629 (rest
[2] == 't' || rest
[2] == 'T'))
631 else if (month
== TT_UNKNOWN
&&
632 (rest
[1] == 'u' || rest
[1] == 'U') &&
633 (rest
[2] == 'l' || rest
[2] == 'L'))
635 else if (month
== TT_UNKNOWN
&&
636 (rest
[1] == 'u' || rest
[1] == 'U') &&
637 (rest
[2] == 'n' || rest
[2] == 'N'))
641 if (month
== TT_UNKNOWN
&&
642 (rest
[1] == 'a' || rest
[1] == 'A') &&
643 (rest
[2] == 'r' || rest
[2] == 'R'))
645 else if (month
== TT_UNKNOWN
&&
646 (rest
[1] == 'a' || rest
[1] == 'A') &&
647 (rest
[2] == 'y' || rest
[2] == 'Y'))
649 else if (zone
== TT_UNKNOWN
&&
650 (rest
[1] == 'd' || rest
[1] == 'D') &&
651 (rest
[2] == 't' || rest
[2] == 'T'))
653 else if (zone
== TT_UNKNOWN
&&
654 (rest
[1] == 'e' || rest
[1] == 'E') &&
655 (rest
[2] == 't' || rest
[2] == 'T'))
657 else if (dotw
== TT_UNKNOWN
&&
658 (rest
[1] == 'o' || rest
[1] == 'O') &&
659 (rest
[2] == 'n' || rest
[2] == 'N'))
661 else if (zone
== TT_UNKNOWN
&&
662 (rest
[1] == 's' || rest
[1] == 'S') &&
663 (rest
[2] == 't' || rest
[2] == 'T'))
667 if (month
== TT_UNKNOWN
&&
668 (rest
[1] == 'o' || rest
[1] == 'O') &&
669 (rest
[2] == 'v' || rest
[2] == 'V'))
671 else if (zone
== TT_UNKNOWN
&&
672 (rest
[1] == 's' || rest
[1] == 'S') &&
673 (rest
[2] == 't' || rest
[2] == 'T'))
677 if (month
== TT_UNKNOWN
&&
678 (rest
[1] == 'c' || rest
[1] == 'C') &&
679 (rest
[2] == 't' || rest
[2] == 'T'))
683 if (zone
== TT_UNKNOWN
&&
684 (rest
[1] == 'd' || rest
[1] == 'D') &&
685 (rest
[2] == 't' || rest
[2] == 'T'))
687 else if (zone
== TT_UNKNOWN
&&
688 (rest
[1] == 's' || rest
[1] == 'S') &&
689 (rest
[2] == 't' || rest
[2] == 'T'))
693 if (dotw
== TT_UNKNOWN
&&
694 (rest
[1] == 'a' || rest
[1] == 'A') &&
695 (rest
[2] == 't' || rest
[2] == 'T'))
697 else if (month
== TT_UNKNOWN
&&
698 (rest
[1] == 'e' || rest
[1] == 'E') &&
699 (rest
[2] == 'p' || rest
[2] == 'P'))
701 else if (dotw
== TT_UNKNOWN
&&
702 (rest
[1] == 'u' || rest
[1] == 'U') &&
703 (rest
[2] == 'n' || rest
[2] == 'N'))
707 if (dotw
== TT_UNKNOWN
&&
708 (rest
[1] == 'h' || rest
[1] == 'H') &&
709 (rest
[2] == 'u' || rest
[2] == 'U'))
711 else if (dotw
== TT_UNKNOWN
&&
712 (rest
[1] == 'u' || rest
[1] == 'U') &&
713 (rest
[2] == 'e' || rest
[2] == 'E'))
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. */
725 if (dotw
== TT_UNKNOWN
&&
726 (rest
[1] == 'e' || rest
[1] == 'E') &&
727 (rest
[2] == 'd' || rest
[2] == 'D'))
735 if (zone_offset
!= -1)
737 /* already got one... */
741 if (zone
!= TT_UNKNOWN
&& zone
!= TT_GMT
)
743 /* GMT+0300 is legal, but PST+0300 is not. */
748 sign
= ((*rest
== '+') ? 1 : -1);
749 rest
++; /* move over sign */
751 while (*end
>= '0' && *end
<= '9')
753 if (rest
== end
) /* no digits here */
756 if ((end
- rest
) == 4)
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;
775 case '0': case '1': case '2': case '3': case '4':
776 case '5': case '6': case '7': case '8': case '9':
781 const char *end
= rest
+ 1;
782 while (*end
>= '0' && *end
<= '9')
785 /* end is now the first character after a range of digits. */
789 if (hour
>= 0 && min
>= 0) /* already got it */
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]+: */
796 else if ((end
- rest
) == 2)
797 tmp_hour
= ((rest
[0]-'0')*10 +
800 tmp_hour
= (rest
[0]-'0');
802 /* move over the colon, and parse minutes */
805 while (*end
>= '0' && *end
<= '9')
809 /* no digits after first colon? */
811 else if ((end
- rest
) > 2)
812 /* it is [0-9][0-9][0-9]+: */
814 else if ((end
- rest
) == 2)
815 tmp_min
= ((rest
[0]-'0')*10 +
818 tmp_min
= (rest
[0]-'0');
820 /* now go for seconds */
825 while (*end
>= '0' && *end
<= '9')
829 /* no digits after second colon - that's ok. */
831 else if ((end
- rest
) > 2)
832 /* it is [0-9][0-9][0-9]+: */
834 else if ((end
- rest
) == 2)
835 tmp_sec
= ((rest
[0]-'0')*10 +
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.
849 while (*s
&& (*s
== ' ' || *s
== '\t'))
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 */
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
873 #### But it doesn't handle 1995-06-22.
878 if (month
!= TT_UNKNOWN
)
879 /* if we saw a month name, this can't be. */
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 */
892 if (*s
< '0' || *s
> '9') /* second 1 or 2 digits */
895 if (*s
>= '0' && *s
<= '9')
896 n2
= n2
*10 + (*s
++ - '0');
898 if (*s
!= '/' && *s
!= '-') /* slash */
902 if (*s
< '0' || *s
> '9') /* third 1, 2, 4, or 5 digits */
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')
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'))
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 */
937 month
= (TIME_TOKEN
)(n2
+ ((int)TT_JAN
) - 1);
943 if (n1
> 12 && n2
> 12) /* illegal */
954 if (n1
> 12) /* must be DD/MM/YY */
957 month
= (TIME_TOKEN
)(n2
+ ((int)TT_JAN
) - 1);
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);
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 */
976 ? ((rest
[0]-'0')*10000L +
977 (rest
[1]-'0')*1000L +
982 else if ((end
- rest
) == 4) /* four digits is a year */
984 ? ((rest
[0]-'0')*1000L +
989 else if ((end
- rest
) == 2) /* two digits - date or year */
991 int n
= ((rest
[0]-'0')*10 +
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)
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? */
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 :+-.
1029 *rest
!= ' ' && *rest
!= '\t' &&
1030 *rest
!= ',' && *rest
!= ';' &&
1031 *rest
!= '-' && *rest
!= '+' &&
1033 *rest
!= '(' && *rest
!= ')' && *rest
!= '[' && *rest
!= ']')
1035 /* skip over uninteresting chars. */
1038 (*rest
== ' ' || *rest
== '\t' ||
1039 *rest
== ',' || *rest
== ';' || *rest
== '/' ||
1040 *rest
== '(' || *rest
== ')' || *rest
== '[' || *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'))
1056 if (zone
!= TT_UNKNOWN
&& zone_offset
== -1)
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;
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
)
1088 memset(result
, 0, sizeof(*result
));
1090 result
->tm_sec
= sec
;
1092 result
->tm_min
= min
;
1094 result
->tm_hour
= hour
;
1096 result
->tm_mday
= date
;
1097 if (month
!= TT_UNKNOWN
)
1098 result
->tm_month
= (((int)month
) - ((int)TT_JAN
));
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. */
1117 if (zone_offset
== -1)
1119 /* no zone was specified, and we're to assume that everything
1121 struct tm localTime
;
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
1174 if (result
->tm_year
>= 3000) {
1175 /* Emulate what mktime would have done. */
1179 secs
= mktime(&localTime
);
1182 secs
= mktime(&localTime
);
1184 if (secs
!= (time_t) -1)
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
;
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. */
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
);