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