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.
65 * Unit tests are in base/time/pr_time_unittest.cc.
68 #include "base/logging.h"
69 #include "base/third_party/nspr/prtime.h"
70 #include "build/build_config.h"
74 #elif defined(OS_MACOSX)
75 #include <CoreFoundation/CoreFoundation.h>
76 #elif defined(OS_ANDROID)
78 #include "base/os_compat_android.h" // For timegm()
79 #elif defined(OS_NACL)
80 #include "base/os_compat_nacl.h" // For timegm()
82 #include <errno.h> /* for EINVAL */
85 /* Implements the Unix localtime_r() function for windows */
87 static void localtime_r(const time_t* secs
, struct tm
* time
) {
88 (void) localtime_s(time
, secs
);
93 *------------------------------------------------------------------------
97 * Cf. time_t mktime(struct tm *tp)
98 * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough.
100 *------------------------------------------------------------------------
103 PR_ImplodeTime(const PRExplodedTime
*exploded
)
105 // This is important, we want to make sure multiplications are
106 // done with the correct precision.
107 static const PRTime kSecondsToMicroseconds
= static_cast<PRTime
>(1000000);
109 // Create the system struct representing our exploded time.
112 ULARGE_INTEGER uli
= {0};
114 st
.wYear
= exploded
->tm_year
;
115 st
.wMonth
= exploded
->tm_month
+ 1;
116 st
.wDayOfWeek
= exploded
->tm_wday
;
117 st
.wDay
= exploded
->tm_mday
;
118 st
.wHour
= exploded
->tm_hour
;
119 st
.wMinute
= exploded
->tm_min
;
120 st
.wSecond
= exploded
->tm_sec
;
121 st
.wMilliseconds
= exploded
->tm_usec
/1000;
122 // Convert to FILETIME.
123 if (!SystemTimeToFileTime(&st
, &ft
)) {
124 NOTREACHED() << "Unable to convert time";
128 uli
.LowPart
= ft
.dwLowDateTime
;
129 uli
.HighPart
= ft
.dwHighDateTime
;
130 // Convert from Windows epoch to NSPR epoch, and 100-nanoseconds units
131 // to microsecond units.
133 static_cast<PRTime
>((uli
.QuadPart
/ 10) - 11644473600000000i64
);
134 // Adjust for time zone and dst. Convert from seconds to microseconds.
135 result
-= (exploded
->tm_params
.tp_gmt_offset
+
136 exploded
->tm_params
.tp_dst_offset
) * kSecondsToMicroseconds
;
137 // Add microseconds that cannot be represented in |st|.
138 result
+= exploded
->tm_usec
% 1000;
140 #elif defined(OS_MACOSX)
141 // Create the system struct representing our exploded time.
142 CFGregorianDate gregorian_date
;
143 gregorian_date
.year
= exploded
->tm_year
;
144 gregorian_date
.month
= exploded
->tm_month
+ 1;
145 gregorian_date
.day
= exploded
->tm_mday
;
146 gregorian_date
.hour
= exploded
->tm_hour
;
147 gregorian_date
.minute
= exploded
->tm_min
;
148 gregorian_date
.second
= exploded
->tm_sec
;
150 // Compute |absolute_time| in seconds, correct for gmt and dst
151 // (note the combined offset will be negative when we need to add it), then
152 // convert to microseconds which is what PRTime expects.
153 CFAbsoluteTime absolute_time
=
154 CFGregorianDateGetAbsoluteTime(gregorian_date
, NULL
);
155 PRTime result
= static_cast<PRTime
>(absolute_time
);
156 result
-= exploded
->tm_params
.tp_gmt_offset
+
157 exploded
->tm_params
.tp_dst_offset
;
158 result
+= kCFAbsoluteTimeIntervalSince1970
; // PRTime epoch is 1970
159 result
*= kSecondsToMicroseconds
;
160 result
+= exploded
->tm_usec
;
162 #elif defined(OS_POSIX)
163 struct tm exp_tm
= {0};
164 exp_tm
.tm_sec
= exploded
->tm_sec
;
165 exp_tm
.tm_min
= exploded
->tm_min
;
166 exp_tm
.tm_hour
= exploded
->tm_hour
;
167 exp_tm
.tm_mday
= exploded
->tm_mday
;
168 exp_tm
.tm_mon
= exploded
->tm_month
;
169 exp_tm
.tm_year
= exploded
->tm_year
- 1900;
171 time_t absolute_time
= timegm(&exp_tm
);
173 // If timegm returned -1. Since we don't pass it a time zone, the only
174 // valid case of returning -1 is 1 second before Epoch (Dec 31, 1969).
175 if (absolute_time
== -1 &&
176 !(exploded
->tm_year
== 1969 && exploded
->tm_month
== 11 &&
177 exploded
->tm_mday
== 31 && exploded
->tm_hour
== 23 &&
178 exploded
->tm_min
== 59 && exploded
->tm_sec
== 59)) {
179 // If we get here, time_t must be 32 bits.
180 // Date was possibly too far in the future and would overflow. Return
181 // the most future date possible (year 2038).
182 if (exploded
->tm_year
>= 1970)
183 return INT_MAX
* kSecondsToMicroseconds
;
184 // Date was possibly too far in the past and would underflow. Return
185 // the most past date possible (year 1901).
186 return INT_MIN
* kSecondsToMicroseconds
;
189 PRTime result
= static_cast<PRTime
>(absolute_time
);
190 result
-= exploded
->tm_params
.tp_gmt_offset
+
191 exploded
->tm_params
.tp_dst_offset
;
192 result
*= kSecondsToMicroseconds
;
193 result
+= exploded
->tm_usec
;
196 #error No PR_ImplodeTime implemented on your platform.
201 * The COUNT_LEAPS macro counts the number of leap years passed by
202 * till the start of the given year Y. At the start of the year 4
203 * A.D. the number of leap years passed by is 0, while at the start of
204 * the year 5 A.D. this count is 1. The number of years divisible by
205 * 100 but not divisible by 400 (the non-leap years) is deducted from
206 * the count to get the correct number of leap years.
208 * The COUNT_DAYS macro counts the number of days since 01/01/01 till the
209 * start of the given year Y. The number of days at the start of the year
210 * 1 is 0 while the number of days at the start of the year 2 is 365
211 * (which is ((2)-1) * 365) and so on. The reference point is 01/01/01
215 #define COUNT_LEAPS(Y) ( ((Y)-1)/4 - ((Y)-1)/100 + ((Y)-1)/400 )
216 #define COUNT_DAYS(Y) ( ((Y)-1)*365 + COUNT_LEAPS(Y) )
217 #define DAYS_BETWEEN_YEARS(A, B) (COUNT_DAYS(B) - COUNT_DAYS(A))
220 * Static variables used by functions in this file
224 * The following array contains the day of year for the last day of
225 * each month, where index 1 is January, and day 0 is January 1.
228 static const int lastDayOfMonth
[2][13] = {
229 {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364},
230 {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}
234 * The number of days in a month
237 static const PRInt8 nDays
[2][12] = {
238 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
239 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
243 *-------------------------------------------------------------------------
247 * Returns 1 if the year is a leap year, 0 otherwise.
249 *-------------------------------------------------------------------------
252 static int IsLeapYear(PRInt16 year
)
254 if ((year
% 4 == 0 && year
% 100 != 0) || year
% 400 == 0)
261 * 'secOffset' should be less than 86400 (i.e., a day).
262 * 'time' should point to a normalized PRExplodedTime.
266 ApplySecOffset(PRExplodedTime
*time
, PRInt32 secOffset
)
268 time
->tm_sec
+= secOffset
;
270 /* Note that in this implementation we do not count leap seconds */
271 if (time
->tm_sec
< 0 || time
->tm_sec
>= 60) {
272 time
->tm_min
+= time
->tm_sec
/ 60;
274 if (time
->tm_sec
< 0) {
280 if (time
->tm_min
< 0 || time
->tm_min
>= 60) {
281 time
->tm_hour
+= time
->tm_min
/ 60;
283 if (time
->tm_min
< 0) {
289 if (time
->tm_hour
< 0) {
290 /* Decrement mday, yday, and wday */
294 if (time
->tm_mday
< 1) {
296 if (time
->tm_month
< 0) {
299 if (IsLeapYear(time
->tm_year
))
304 time
->tm_mday
= nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
];
307 if (time
->tm_wday
< 0)
309 } else if (time
->tm_hour
> 23) {
310 /* Increment mday, yday, and wday */
315 nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
]) {
318 if (time
->tm_month
> 11) {
325 if (time
->tm_wday
> 6)
331 PR_NormalizeTime(PRExplodedTime
*time
, PRTimeParamFn params
)
336 /* Get back to GMT */
337 time
->tm_sec
-= time
->tm_params
.tp_gmt_offset
338 + time
->tm_params
.tp_dst_offset
;
339 time
->tm_params
.tp_gmt_offset
= 0;
340 time
->tm_params
.tp_dst_offset
= 0;
342 /* Now normalize GMT */
344 if (time
->tm_usec
< 0 || time
->tm_usec
>= 1000000) {
345 time
->tm_sec
+= time
->tm_usec
/ 1000000;
346 time
->tm_usec
%= 1000000;
347 if (time
->tm_usec
< 0) {
348 time
->tm_usec
+= 1000000;
353 /* Note that we do not count leap seconds in this implementation */
354 if (time
->tm_sec
< 0 || time
->tm_sec
>= 60) {
355 time
->tm_min
+= time
->tm_sec
/ 60;
357 if (time
->tm_sec
< 0) {
363 if (time
->tm_min
< 0 || time
->tm_min
>= 60) {
364 time
->tm_hour
+= time
->tm_min
/ 60;
366 if (time
->tm_min
< 0) {
372 if (time
->tm_hour
< 0 || time
->tm_hour
>= 24) {
373 time
->tm_mday
+= time
->tm_hour
/ 24;
375 if (time
->tm_hour
< 0) {
381 /* Normalize month and year before mday */
382 if (time
->tm_month
< 0 || time
->tm_month
>= 12) {
383 time
->tm_year
+= time
->tm_month
/ 12;
384 time
->tm_month
%= 12;
385 if (time
->tm_month
< 0) {
386 time
->tm_month
+= 12;
391 /* Now that month and year are in proper range, normalize mday */
393 if (time
->tm_mday
< 1) {
396 /* the previous month */
398 if (time
->tm_month
< 0) {
402 time
->tm_mday
+= nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
];
403 } while (time
->tm_mday
< 1);
405 daysInMonth
= nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
];
406 while (time
->tm_mday
> daysInMonth
) {
408 time
->tm_mday
-= daysInMonth
;
410 if (time
->tm_month
> 11) {
414 daysInMonth
= nDays
[IsLeapYear(time
->tm_year
)][time
->tm_month
];
418 /* Recompute yday and wday */
419 time
->tm_yday
= time
->tm_mday
+
420 lastDayOfMonth
[IsLeapYear(time
->tm_year
)][time
->tm_month
];
422 numDays
= DAYS_BETWEEN_YEARS(1970, time
->tm_year
) + time
->tm_yday
;
423 time
->tm_wday
= (numDays
+ 4) % 7;
424 if (time
->tm_wday
< 0) {
428 /* Recompute time parameters */
430 time
->tm_params
= params(time
);
432 ApplySecOffset(time
, time
->tm_params
.tp_gmt_offset
433 + time
->tm_params
.tp_dst_offset
);
437 *------------------------------------------------------------------------
439 * PR_GMTParameters --
441 * Returns the PRTimeParameters for Greenwich Mean Time.
442 * Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0.
444 *------------------------------------------------------------------------
448 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
512 * 1995-06-17T23:11:25.342156Z
514 * If the input string doesn't contain a description of the timezone,
515 * we consult the `default_to_gmt' to decide whether the string should
516 * be interpreted relative to the local time zone (PR_FALSE) or GMT (PR_TRUE).
517 * The correct value for this argument depends on what standard specified
518 * the time string which you are parsing.
524 PRBool default_to_gmt
,
525 PRTime
*result_imploded
)
528 PRExplodedTime
*result
= &tm
;
529 TIME_TOKEN dotw
= TT_UNKNOWN
;
530 TIME_TOKEN month
= TT_UNKNOWN
;
531 TIME_TOKEN zone
= TT_UNKNOWN
;
532 int zone_offset
= -1;
541 const char *rest
= string
;
545 PR_ASSERT(string
&& result
);
546 if (!string
|| !result
) return PR_FAILURE
;
551 if (iterations
++ > 1000)
559 if (month
== TT_UNKNOWN
&&
560 (rest
[1] == 'p' || rest
[1] == 'P') &&
561 (rest
[2] == 'r' || rest
[2] == 'R'))
563 else if (zone
== TT_UNKNOWN
&&
564 (rest
[1] == 's' || rest
[1] == 'S') &&
565 (rest
[2] == 't' || rest
[2] == 'T'))
567 else if (month
== TT_UNKNOWN
&&
568 (rest
[1] == 'u' || rest
[1] == 'U') &&
569 (rest
[2] == 'g' || rest
[2] == 'G'))
573 if (zone
== TT_UNKNOWN
&&
574 (rest
[1] == 's' || rest
[1] == 'S') &&
575 (rest
[2] == 't' || rest
[2] == 'T'))
579 if (zone
== TT_UNKNOWN
&&
580 (rest
[1] == 'd' || rest
[1] == 'D') &&
581 (rest
[2] == 't' || rest
[2] == 'T'))
583 else if (zone
== TT_UNKNOWN
&&
584 (rest
[1] == 's' || rest
[1] == 'S') &&
585 (rest
[2] == 't' || rest
[2] == 'T'))
589 if (month
== TT_UNKNOWN
&&
590 (rest
[1] == 'e' || rest
[1] == 'E') &&
591 (rest
[2] == 'c' || rest
[2] == 'C'))
595 if (zone
== TT_UNKNOWN
&&
596 (rest
[1] == 'd' || rest
[1] == 'D') &&
597 (rest
[2] == 't' || rest
[2] == 'T'))
599 else if (zone
== TT_UNKNOWN
&&
600 (rest
[1] == 'e' || rest
[1] == 'E') &&
601 (rest
[2] == 't' || rest
[2] == 'T'))
603 else if (zone
== TT_UNKNOWN
&&
604 (rest
[1] == 's' || rest
[1] == 'S') &&
605 (rest
[2] == 't' || rest
[2] == 'T'))
609 if (month
== TT_UNKNOWN
&&
610 (rest
[1] == 'e' || rest
[1] == 'E') &&
611 (rest
[2] == 'b' || rest
[2] == 'B'))
613 else if (dotw
== TT_UNKNOWN
&&
614 (rest
[1] == 'r' || rest
[1] == 'R') &&
615 (rest
[2] == 'i' || rest
[2] == 'I'))
619 if (zone
== TT_UNKNOWN
&&
620 (rest
[1] == 'm' || rest
[1] == 'M') &&
621 (rest
[2] == 't' || rest
[2] == 'T'))
625 if (month
== TT_UNKNOWN
&&
626 (rest
[1] == 'a' || rest
[1] == 'A') &&
627 (rest
[2] == 'n' || rest
[2] == 'N'))
629 else if (zone
== TT_UNKNOWN
&&
630 (rest
[1] == 's' || rest
[1] == 'S') &&
631 (rest
[2] == 't' || rest
[2] == 'T'))
633 else if (month
== TT_UNKNOWN
&&
634 (rest
[1] == 'u' || rest
[1] == 'U') &&
635 (rest
[2] == 'l' || rest
[2] == 'L'))
637 else if (month
== TT_UNKNOWN
&&
638 (rest
[1] == 'u' || rest
[1] == 'U') &&
639 (rest
[2] == 'n' || rest
[2] == 'N'))
643 if (month
== TT_UNKNOWN
&&
644 (rest
[1] == 'a' || rest
[1] == 'A') &&
645 (rest
[2] == 'r' || rest
[2] == 'R'))
647 else if (month
== TT_UNKNOWN
&&
648 (rest
[1] == 'a' || rest
[1] == 'A') &&
649 (rest
[2] == 'y' || rest
[2] == 'Y'))
651 else if (zone
== TT_UNKNOWN
&&
652 (rest
[1] == 'd' || rest
[1] == 'D') &&
653 (rest
[2] == 't' || rest
[2] == 'T'))
655 else if (zone
== TT_UNKNOWN
&&
656 (rest
[1] == 'e' || rest
[1] == 'E') &&
657 (rest
[2] == 't' || rest
[2] == 'T'))
659 else if (dotw
== TT_UNKNOWN
&&
660 (rest
[1] == 'o' || rest
[1] == 'O') &&
661 (rest
[2] == 'n' || rest
[2] == 'N'))
663 else if (zone
== TT_UNKNOWN
&&
664 (rest
[1] == 's' || rest
[1] == 'S') &&
665 (rest
[2] == 't' || rest
[2] == 'T'))
669 if (month
== TT_UNKNOWN
&&
670 (rest
[1] == 'o' || rest
[1] == 'O') &&
671 (rest
[2] == 'v' || rest
[2] == 'V'))
673 else if (zone
== TT_UNKNOWN
&&
674 (rest
[1] == 's' || rest
[1] == 'S') &&
675 (rest
[2] == 't' || rest
[2] == 'T'))
679 if (month
== TT_UNKNOWN
&&
680 (rest
[1] == 'c' || rest
[1] == 'C') &&
681 (rest
[2] == 't' || rest
[2] == 'T'))
685 if (zone
== TT_UNKNOWN
&&
686 (rest
[1] == 'd' || rest
[1] == 'D') &&
687 (rest
[2] == 't' || rest
[2] == 'T'))
689 else if (zone
== TT_UNKNOWN
&&
690 (rest
[1] == 's' || rest
[1] == 'S') &&
691 (rest
[2] == 't' || rest
[2] == 'T'))
695 if (dotw
== TT_UNKNOWN
&&
696 (rest
[1] == 'a' || rest
[1] == 'A') &&
697 (rest
[2] == 't' || rest
[2] == 'T'))
699 else if (month
== TT_UNKNOWN
&&
700 (rest
[1] == 'e' || rest
[1] == 'E') &&
701 (rest
[2] == 'p' || rest
[2] == 'P'))
703 else if (dotw
== TT_UNKNOWN
&&
704 (rest
[1] == 'u' || rest
[1] == 'U') &&
705 (rest
[2] == 'n' || rest
[2] == 'N'))
709 if (dotw
== TT_UNKNOWN
&&
710 (rest
[1] == 'h' || rest
[1] == 'H') &&
711 (rest
[2] == 'u' || rest
[2] == 'U'))
713 else if (dotw
== TT_UNKNOWN
&&
714 (rest
[1] == 'u' || rest
[1] == 'U') &&
715 (rest
[2] == 'e' || rest
[2] == 'E'))
719 if (zone
== TT_UNKNOWN
&&
720 (rest
[1] == 't' || rest
[1] == 'T') &&
721 !(rest
[2] >= 'A' && rest
[2] <= 'Z') &&
722 !(rest
[2] >= 'a' && rest
[2] <= 'z'))
723 /* UT is the same as GMT but UTx is not. */
727 if (dotw
== TT_UNKNOWN
&&
728 (rest
[1] == 'e' || rest
[1] == 'E') &&
729 (rest
[2] == 'd' || rest
[2] == 'D'))
737 if (zone_offset
!= -1)
739 /* already got one... */
743 if (zone
!= TT_UNKNOWN
&& zone
!= TT_GMT
)
745 /* GMT+0300 is legal, but PST+0300 is not. */
750 sign
= ((*rest
== '+') ? 1 : -1);
751 rest
++; /* move over sign */
753 while (*end
>= '0' && *end
<= '9')
755 if (rest
== end
) /* no digits here */
758 if ((end
- rest
) == 4)
760 zone_offset
= (((((rest
[0]-'0')*10) + (rest
[1]-'0')) * 60) +
761 (((rest
[2]-'0')*10) + (rest
[3]-'0')));
762 else if ((end
- rest
) == 2)
763 /* offset in hours */
764 zone_offset
= (((rest
[0]-'0')*10) + (rest
[1]-'0')) * 60;
765 else if ((end
- rest
) == 1)
766 /* offset in hours */
767 zone_offset
= (rest
[0]-'0') * 60;
777 case '0': case '1': case '2': case '3': case '4':
778 case '5': case '6': case '7': case '8': case '9':
784 const char *end
= rest
+ 1;
785 while (*end
>= '0' && *end
<= '9')
788 /* end is now the first character after a range of digits. */
792 if (hour
>= 0 && min
>= 0) /* already got it */
795 /* We have seen "[0-9]+:", so this is probably HH:MM[:SS] */
796 if ((end
- rest
) > 2)
797 /* it is [0-9][0-9][0-9]+: */
799 else if ((end
- rest
) == 2)
800 tmp_hour
= ((rest
[0]-'0')*10 +
803 tmp_hour
= (rest
[0]-'0');
805 /* move over the colon, and parse minutes */
808 while (*end
>= '0' && *end
<= '9')
812 /* no digits after first colon? */
814 else if ((end
- rest
) > 2)
815 /* it is [0-9][0-9][0-9]+: */
817 else if ((end
- rest
) == 2)
818 tmp_min
= ((rest
[0]-'0')*10 +
821 tmp_min
= (rest
[0]-'0');
823 /* now go for seconds */
828 while (*end
>= '0' && *end
<= '9')
832 /* no digits after second colon - that's ok. */
834 else if ((end
- rest
) > 2)
835 /* it is [0-9][0-9][0-9]+: */
837 else if ((end
- rest
) == 2)
838 tmp_sec
= ((rest
[0]-'0')*10 +
841 tmp_sec
= (rest
[0]-'0');
843 /* fractional second */
850 /* use up to 6 digits, skip over the rest */
851 while (*end
>= '0' && *end
<= '9')
854 tmp_usec
= tmp_usec
* 10 + *end
- '0';
857 int ndigits
= end
- rest
;
858 while (ndigits
++ < 6)
868 else if (tmp_hour
<= 12)
870 /* If we made it here, we've parsed hour and min,
871 and possibly sec, so the current token is a time.
872 Now skip over whitespace and see if there's an AM
873 or PM directly following the time.
876 while (*s
&& (*s
== ' ' || *s
== '\t'))
878 if ((s
[0] == 'p' || s
[0] == 'P') &&
879 (s
[1] == 'm' || s
[1] == 'M'))
880 /* 10:05pm == 22:05, and 12:05pm == 12:05 */
881 tmp_hour
= (tmp_hour
== 12 ? 12 : tmp_hour
+ 12);
882 else if (tmp_hour
== 12 &&
883 (s
[0] == 'a' || s
[0] == 'A') &&
884 (s
[1] == 'm' || s
[1] == 'M'))
885 /* 12:05am == 00:05 */
896 else if ((*end
== '/' || *end
== '-') &&
897 end
[1] >= '0' && end
[1] <= '9')
899 /* Perhaps this is 6/16/95, 16/6/95, 6-16-95, or 16-6-95
900 or even 95-06-05 or 1995-06-22.
905 if (month
!= TT_UNKNOWN
)
906 /* if we saw a month name, this can't be. */
911 n1
= (*s
++ - '0'); /* first 1, 2 or 4 digits */
912 if (*s
>= '0' && *s
<= '9')
914 n1
= n1
*10 + (*s
++ - '0');
916 if (*s
>= '0' && *s
<= '9') /* optional digits 3 and 4 */
918 n1
= n1
*10 + (*s
++ - '0');
919 if (*s
< '0' || *s
> '9')
921 n1
= n1
*10 + (*s
++ - '0');
925 if (*s
!= '/' && *s
!= '-') /* slash */
929 if (*s
< '0' || *s
> '9') /* second 1 or 2 digits */
932 if (*s
>= '0' && *s
<= '9')
933 n2
= n2
*10 + (*s
++ - '0');
935 if (*s
!= '/' && *s
!= '-') /* slash */
939 if (*s
< '0' || *s
> '9') /* third 1, 2, 4, or 5 digits */
942 if (*s
>= '0' && *s
<= '9')
943 n3
= n3
*10 + (*s
++ - '0');
945 if (*s
>= '0' && *s
<= '9') /* optional digits 3, 4, and 5 */
947 n3
= n3
*10 + (*s
++ - '0');
948 if (*s
< '0' || *s
> '9')
950 n3
= n3
*10 + (*s
++ - '0');
951 if (*s
>= '0' && *s
<= '9')
952 n3
= n3
*10 + (*s
++ - '0');
955 if (*s
== 'T' && s
[1] >= '0' && s
[1] <= '9')
956 /* followed by ISO 8601 T delimiter and number is ok */
958 else if ((*s
>= '0' && *s
<= '9') ||
959 (*s
>= 'A' && *s
<= 'Z') ||
960 (*s
>= 'a' && *s
<= 'z'))
961 /* but other alphanumerics are not ok */
964 /* Ok, we parsed three multi-digit numbers, with / or -
965 between them. Now decide what the hell they are
966 (DD/MM/YY or MM/DD/YY or [YY]YY/MM/DD.)
969 if (n1
> 31 || n1
== 0) /* must be [YY]YY/MM/DD */
978 month
= (TIME_TOKEN
)(n2
+ ((int)TT_JAN
) - 1);
984 if (n1
> 12 && n2
> 12) /* illegal */
995 if (n1
> 12) /* must be DD/MM/YY */
998 month
= (TIME_TOKEN
)(n2
+ ((int)TT_JAN
) - 1);
1001 else /* assume MM/DD/YY */
1003 /* #### In the ambiguous case, should we consult the
1004 locale to find out the local default? */
1005 month
= (TIME_TOKEN
)(n1
+ ((int)TT_JAN
) - 1);
1011 else if ((*end
>= 'A' && *end
<= 'Z') ||
1012 (*end
>= 'a' && *end
<= 'z'))
1013 /* Digits followed by non-punctuation - what's that? */
1015 else if ((end
- rest
) == 5) /* five digits is a year */
1017 ? ((rest
[0]-'0')*10000L +
1018 (rest
[1]-'0')*1000L +
1019 (rest
[2]-'0')*100L +
1023 else if ((end
- rest
) == 4) /* four digits is a year */
1025 ? ((rest
[0]-'0')*1000L +
1026 (rest
[1]-'0')*100L +
1030 else if ((end
- rest
) == 2) /* two digits - date or year */
1032 int n
= ((rest
[0]-'0')*10 +
1034 /* If we don't have a date (day of the month) and we see a number
1035 less than 32, then assume that is the date.
1037 Otherwise, if we have a date and not a year, assume this is the
1038 year. If it is less than 70, then assume it refers to the 21st
1039 century. If it is two digits (>= 70), assume it refers to this
1040 century. Otherwise, assume it refers to an unambiguous year.
1042 The world will surely end soon.
1044 if (date
< 0 && n
< 32)
1055 /* else what the hell is this. */
1057 else if ((end
- rest
) == 1) /* one digit - date */
1058 date
= (date
< 0 ? (rest
[0]-'0') : date
);
1059 /* else, three or more than five digits - what's that? */
1062 } /* case '0' .. '9' */
1065 /* Skip to the end of this token, whether we parsed it or not.
1066 Tokens are delimited by whitespace, or ,;-+/()[] but explicitly not .:
1067 'T' is also treated as delimiter when followed by a digit (ISO 8601).
1070 *rest
!= ' ' && *rest
!= '\t' &&
1071 *rest
!= ',' && *rest
!= ';' &&
1072 *rest
!= '-' && *rest
!= '+' &&
1074 *rest
!= '(' && *rest
!= ')' && *rest
!= '[' && *rest
!= ']' &&
1075 !(*rest
== 'T' && rest
[1] >= '0' && rest
[1] <= '9')
1078 /* skip over uninteresting chars. */
1080 while (*rest
== ' ' || *rest
== '\t' ||
1081 *rest
== ',' || *rest
== ';' || *rest
== '/' ||
1082 *rest
== '(' || *rest
== ')' || *rest
== '[' || *rest
== ']')
1085 /* "-" is ignored at the beginning of a token if we have not yet
1086 parsed a year (e.g., the second "-" in "30-AUG-1966"), or if
1087 the character after the dash is not a digit. */
1088 if (*rest
== '-' && ((rest
> string
&&
1089 isalpha((unsigned char)rest
[-1]) && year
< 0) ||
1090 rest
[1] < '0' || rest
[1] > '9'))
1096 /* Skip T that may precede ISO 8601 time. */
1097 if (*rest
== 'T' && rest
[1] >= '0' && rest
[1] <= '9')
1101 if (zone
!= TT_UNKNOWN
&& zone_offset
== -1)
1105 case TT_PST
: zone_offset
= -8 * 60; break;
1106 case TT_PDT
: zone_offset
= -8 * 60; dst_offset
= 1 * 60; break;
1107 case TT_MST
: zone_offset
= -7 * 60; break;
1108 case TT_MDT
: zone_offset
= -7 * 60; dst_offset
= 1 * 60; break;
1109 case TT_CST
: zone_offset
= -6 * 60; break;
1110 case TT_CDT
: zone_offset
= -6 * 60; dst_offset
= 1 * 60; break;
1111 case TT_EST
: zone_offset
= -5 * 60; break;
1112 case TT_EDT
: zone_offset
= -5 * 60; dst_offset
= 1 * 60; break;
1113 case TT_AST
: zone_offset
= -4 * 60; break;
1114 case TT_NST
: zone_offset
= -3 * 60 - 30; break;
1115 case TT_GMT
: zone_offset
= 0 * 60; break;
1116 case TT_BST
: zone_offset
= 0 * 60; dst_offset
= 1 * 60; break;
1117 case TT_MET
: zone_offset
= 1 * 60; break;
1118 case TT_EET
: zone_offset
= 2 * 60; break;
1119 case TT_JST
: zone_offset
= 9 * 60; break;
1126 /* If we didn't find a year, month, or day-of-the-month, we can't
1127 possibly parse this, and in fact, mktime() will do something random
1128 (I'm seeing it return "Tue Feb 5 06:28:16 2036", which is no doubt
1129 a numerologically significant date... */
1130 if (month
== TT_UNKNOWN
|| date
== -1 || year
== -1 || year
> PR_INT16_MAX
)
1133 memset(result
, 0, sizeof(*result
));
1135 result
->tm_usec
= usec
;
1137 result
->tm_sec
= sec
;
1139 result
->tm_min
= min
;
1141 result
->tm_hour
= hour
;
1143 result
->tm_mday
= date
;
1144 if (month
!= TT_UNKNOWN
)
1145 result
->tm_month
= (((int)month
) - ((int)TT_JAN
));
1147 result
->tm_year
= year
;
1148 if (dotw
!= TT_UNKNOWN
)
1149 result
->tm_wday
= (((int)dotw
) - ((int)TT_SUN
));
1151 * Mainly to compute wday and yday, but normalized time is also required
1152 * by the check below that works around a Visual C++ 2005 mktime problem.
1154 PR_NormalizeTime(result
, PR_GMTParameters
);
1155 /* The remaining work is to set the gmt and dst offsets in tm_params. */
1157 if (zone
== TT_UNKNOWN
&& default_to_gmt
)
1159 /* No zone was specified, so pretend the zone was GMT. */
1164 if (zone_offset
== -1)
1166 /* no zone was specified, and we're to assume that everything
1168 struct tm localTime
;
1171 PR_ASSERT(result
->tm_month
> -1 &&
1172 result
->tm_mday
> 0 &&
1173 result
->tm_hour
> -1 &&
1174 result
->tm_min
> -1 &&
1175 result
->tm_sec
> -1);
1178 * To obtain time_t from a tm structure representing the local
1179 * time, we call mktime(). However, we need to see if we are
1180 * on 1-Jan-1970 or before. If we are, we can't call mktime()
1181 * because mktime() will crash on win16. In that case, we
1182 * calculate zone_offset based on the zone offset at
1183 * 00:00:00, 2 Jan 1970 GMT, and subtract zone_offset from the
1184 * date we are parsing to transform the date to GMT. We also
1185 * do so if mktime() returns (time_t) -1 (time out of range).
1188 /* month, day, hours, mins and secs are always non-negative
1189 so we dont need to worry about them. */
1190 if (result
->tm_year
>= 1970)
1192 localTime
.tm_sec
= result
->tm_sec
;
1193 localTime
.tm_min
= result
->tm_min
;
1194 localTime
.tm_hour
= result
->tm_hour
;
1195 localTime
.tm_mday
= result
->tm_mday
;
1196 localTime
.tm_mon
= result
->tm_month
;
1197 localTime
.tm_year
= result
->tm_year
- 1900;
1198 /* Set this to -1 to tell mktime "I don't care". If you set
1199 it to 0 or 1, you are making assertions about whether the
1200 date you are handing it is in daylight savings mode or not;
1201 and if you're wrong, it will "fix" it for you. */
1202 localTime
.tm_isdst
= -1;
1204 #if _MSC_VER == 1400 /* 1400 = Visual C++ 2005 (8.0) */
1206 * mktime will return (time_t) -1 if the input is a date
1207 * after 23:59:59, December 31, 3000, US Pacific Time (not
1208 * UTC as documented):
1209 * http://msdn.microsoft.com/en-us/library/d1y53h2a(VS.80).aspx
1210 * But if the year is 3001, mktime also invokes the invalid
1211 * parameter handler, causing the application to crash. This
1212 * problem has been reported in
1213 * http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=266036.
1214 * We avoid this crash by not calling mktime if the date is
1215 * out of range. To use a simple test that works in any time
1216 * zone, we consider year 3000 out of range as well. (See
1219 if (result
->tm_year
>= 3000) {
1220 /* Emulate what mktime would have done. */
1224 secs
= mktime(&localTime
);
1227 secs
= mktime(&localTime
);
1229 if (secs
!= (time_t) -1)
1231 *result_imploded
= (PRInt64
)secs
* PR_USEC_PER_SEC
;
1232 *result_imploded
+= result
->tm_usec
;
1237 /* So mktime() can't handle this case. We assume the
1238 zone_offset for the date we are parsing is the same as
1239 the zone offset on 00:00:00 2 Jan 1970 GMT. */
1241 localtime_r(&secs
, &localTime
);
1242 zone_offset
= localTime
.tm_min
1243 + 60 * localTime
.tm_hour
1244 + 1440 * (localTime
.tm_mday
- 2);
1247 result
->tm_params
.tp_gmt_offset
= zone_offset
* 60;
1248 result
->tm_params
.tp_dst_offset
= dst_offset
* 60;
1250 *result_imploded
= PR_ImplodeTime(result
);