1 /* Portions are Copyright (C) 2011 Google Inc */ 2 /* ***** BEGIN LICENSE BLOCK ***** 3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 4 * 5 * The contents of this file are subject to the Mozilla Public License Version 6 * 1.1 (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * http://www.mozilla.org/MPL/ 9 * 10 * Software distributed under the License is distributed on an "AS IS" basis, 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 12 * for the specific language governing rights and limitations under the 13 * License. 14 * 15 * The Original Code is the Netscape Portable Runtime (NSPR). 16 * 17 * The Initial Developer of the Original Code is 18 * Netscape Communications Corporation. 19 * Portions created by the Initial Developer are Copyright (C) 1998-2000 20 * the Initial Developer. All Rights Reserved. 21 * 22 * Contributor(s): 23 * 24 * Alternatively, the contents of this file may be used under the terms of 25 * either the GNU General Public License Version 2 or later (the "GPL"), or 26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 27 * in which case the provisions of the GPL or the LGPL are applicable instead 28 * of those above. If you wish to allow use of your version of this file only 29 * under the terms of either the GPL or the LGPL, and not to allow others to 30 * use your version of this file under the terms of the MPL, indicate your 31 * decision by deleting the provisions above and replace them with the notice 32 * and other provisions required by the GPL or the LGPL. If you do not delete 33 * the provisions above, a recipient may use your version of this file under 34 * the terms of any one of the MPL, the GPL or the LGPL. 35 * 36 * ***** END LICENSE BLOCK ***** */ 37 38 /* 39 * prtime.cc -- 40 * NOTE: The original nspr file name is prtime.c 41 * 42 * NSPR date and time functions 43 * 44 * CVS revision 3.37 45 */ 46 47 /* 48 * The following functions were copied from the NSPR prtime.c file. 49 * PR_ParseTimeString 50 * We inlined the new PR_ParseTimeStringToExplodedTime function to avoid 51 * copying PR_ExplodeTime and PR_LocalTimeParameters. (The PR_ExplodeTime 52 * and PR_ImplodeTime calls cancel each other out.) 53 * PR_NormalizeTime 54 * PR_GMTParameters 55 * PR_ImplodeTime 56 * 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 * Unit tests are in base/time/pr_time_unittest.cc. 66 */ 67 68 #include "base/logging.h" 69 #include "base/third_party/nspr/prtime.h" 70 #include "build/build_config.h" 71 72 #if defined(OS_WIN) 73 #include <windows.h> 74 #elif defined(OS_MACOSX) 75 #include <CoreFoundation/CoreFoundation.h> 76 #elif defined(OS_ANDROID) 77 #include <ctype.h> 78 #include "base/os_compat_android.h" // For timegm() 79 #elif defined(OS_NACL) 80 #include "base/os_compat_nacl.h" // For timegm() 81 #endif 82 #include <errno.h> /* for EINVAL */ 83 #include <time.h> 84 85 /* Implements the Unix localtime_r() function for windows */ 86 #if defined(OS_WIN) 87 static void localtime_r(const time_t* secs, struct tm* time) { 88 (void) localtime_s(time, secs); 89 } 90 #endif 91 92 /* 93 *------------------------------------------------------------------------ 94 * 95 * PR_ImplodeTime -- 96 * 97 * Cf. time_t mktime(struct tm *tp) 98 * Note that 1 year has < 2^25 seconds. So an PRInt32 is large enough. 99 * 100 *------------------------------------------------------------------------ 101 */ 102 PRTime 103 PR_ImplodeTime(const PRExplodedTime *exploded) 104 { 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); 108 #if defined(OS_WIN) 109 // Create the system struct representing our exploded time. 110 SYSTEMTIME st = {0}; 111 FILETIME ft = {0}; 112 ULARGE_INTEGER uli = {0}; 113 114 st.wYear = exploded->tm_year; 115 st.wMonth = static_cast<WORD>(exploded->tm_month + 1); 116 st.wDayOfWeek = exploded->tm_wday; 117 st.wDay = static_cast<WORD>(exploded->tm_mday); 118 st.wHour = static_cast<WORD>(exploded->tm_hour); 119 st.wMinute = static_cast<WORD>(exploded->tm_min); 120 st.wSecond = static_cast<WORD>(exploded->tm_sec); 121 st.wMilliseconds = static_cast<WORD>(exploded->tm_usec/1000); 122 // Convert to FILETIME. 123 if (!SystemTimeToFileTime(&st, &ft)) { 124 NOTREACHED() << "Unable to convert time"; 125 return 0; 126 } 127 // Apply offsets. 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. 132 PRTime result = 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; 139 return result; 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; 149 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; 161 return result; 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; 170 171 time_t absolute_time = timegm(&exp_tm); 172 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; 187 } 188 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; 194 return result; 195 #else 196 #error No PR_ImplodeTime implemented on your platform. 197 #endif 198 } 199 200 /* 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. 207 * 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 212 * midnight 00:00:00. 213 */ 214 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)) 218 219 /* 220 * Static variables used by functions in this file 221 */ 222 223 /* 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. 226 */ 227 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} 231 }; 232 233 /* 234 * The number of days in a month 235 */ 236 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} 240 }; 241 242 /* 243 *------------------------------------------------------------------------- 244 * 245 * IsLeapYear -- 246 * 247 * Returns 1 if the year is a leap year, 0 otherwise. 248 * 249 *------------------------------------------------------------------------- 250 */ 251 252 static int IsLeapYear(PRInt16 year) 253 { 254 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) 255 return 1; 256 else 257 return 0; 258 } 259 260 /* 261 * 'secOffset' should be less than 86400 (i.e., a day). 262 * 'time' should point to a normalized PRExplodedTime. 263 */ 264 265 static void 266 ApplySecOffset(PRExplodedTime *time, PRInt32 secOffset) 267 { 268 time->tm_sec += secOffset; 269 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; 273 time->tm_sec %= 60; 274 if (time->tm_sec < 0) { 275 time->tm_sec += 60; 276 time->tm_min--; 277 } 278 } 279 280 if (time->tm_min < 0 || time->tm_min >= 60) { 281 time->tm_hour += time->tm_min / 60; 282 time->tm_min %= 60; 283 if (time->tm_min < 0) { 284 time->tm_min += 60; 285 time->tm_hour--; 286 } 287 } 288 289 if (time->tm_hour < 0) { 290 /* Decrement mday, yday, and wday */ 291 time->tm_hour += 24; 292 time->tm_mday--; 293 time->tm_yday--; 294 if (time->tm_mday < 1) { 295 time->tm_month--; 296 if (time->tm_month < 0) { 297 time->tm_month = 11; 298 time->tm_year--; 299 if (IsLeapYear(time->tm_year)) 300 time->tm_yday = 365; 301 else 302 time->tm_yday = 364; 303 } 304 time->tm_mday = nDays[IsLeapYear(time->tm_year)][time->tm_month]; 305 } 306 time->tm_wday--; 307 if (time->tm_wday < 0) 308 time->tm_wday = 6; 309 } else if (time->tm_hour > 23) { 310 /* Increment mday, yday, and wday */ 311 time->tm_hour -= 24; 312 time->tm_mday++; 313 time->tm_yday++; 314 if (time->tm_mday > 315 nDays[IsLeapYear(time->tm_year)][time->tm_month]) { 316 time->tm_mday = 1; 317 time->tm_month++; 318 if (time->tm_month > 11) { 319 time->tm_month = 0; 320 time->tm_year++; 321 time->tm_yday = 0; 322 } 323 } 324 time->tm_wday++; 325 if (time->tm_wday > 6) 326 time->tm_wday = 0; 327 } 328 } 329 330 void 331 PR_NormalizeTime(PRExplodedTime *time, PRTimeParamFn params) 332 { 333 int daysInMonth; 334 PRInt32 numDays; 335 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; 341 342 /* Now normalize GMT */ 343 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; 349 time->tm_sec--; 350 } 351 } 352 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; 356 time->tm_sec %= 60; 357 if (time->tm_sec < 0) { 358 time->tm_sec += 60; 359 time->tm_min--; 360 } 361 } 362 363 if (time->tm_min < 0 || time->tm_min >= 60) { 364 time->tm_hour += time->tm_min / 60; 365 time->tm_min %= 60; 366 if (time->tm_min < 0) { 367 time->tm_min += 60; 368 time->tm_hour--; 369 } 370 } 371 372 if (time->tm_hour < 0 || time->tm_hour >= 24) { 373 time->tm_mday += time->tm_hour / 24; 374 time->tm_hour %= 24; 375 if (time->tm_hour < 0) { 376 time->tm_hour += 24; 377 time->tm_mday--; 378 } 379 } 380 381 /* Normalize month and year before mday */ 382 if (time->tm_month < 0 || time->tm_month >= 12) { 383 time->tm_year += static_cast<PRInt16>(time->tm_month / 12); 384 time->tm_month %= 12; 385 if (time->tm_month < 0) { 386 time->tm_month += 12; 387 time->tm_year--; 388 } 389 } 390 391 /* Now that month and year are in proper range, normalize mday */ 392 393 if (time->tm_mday < 1) { 394 /* mday too small */ 395 do { 396 /* the previous month */ 397 time->tm_month--; 398 if (time->tm_month < 0) { 399 time->tm_month = 11; 400 time->tm_year--; 401 } 402 time->tm_mday += nDays[IsLeapYear(time->tm_year)][time->tm_month]; 403 } while (time->tm_mday < 1); 404 } else { 405 daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month]; 406 while (time->tm_mday > daysInMonth) { 407 /* mday too large */ 408 time->tm_mday -= daysInMonth; 409 time->tm_month++; 410 if (time->tm_month > 11) { 411 time->tm_month = 0; 412 time->tm_year++; 413 } 414 daysInMonth = nDays[IsLeapYear(time->tm_year)][time->tm_month]; 415 } 416 } 417 418 /* Recompute yday and wday */ 419 time->tm_yday = static_cast<PRInt16>(time->tm_mday + 420 lastDayOfMonth[IsLeapYear(time->tm_year)][time->tm_month]); 421 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) { 425 time->tm_wday += 7; 426 } 427 428 /* Recompute time parameters */ 429 430 time->tm_params = params(time); 431 432 ApplySecOffset(time, time->tm_params.tp_gmt_offset 433 + time->tm_params.tp_dst_offset); 434 } 435 436 /* 437 *------------------------------------------------------------------------ 438 * 439 * PR_GMTParameters -- 440 * 441 * Returns the PRTimeParameters for Greenwich Mean Time. 442 * Trivially, both the tp_gmt_offset and tp_dst_offset fields are 0. 443 * 444 *------------------------------------------------------------------------ 445 */ 446 447 PRTimeParameters 448 PR_GMTParameters(const PRExplodedTime *gmt) 449 { 450 PRTimeParameters retVal = { 0, 0 }; 451 return retVal; 452 } 453 454 /* 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 (at) netscape.com>. 457 */ 458 459 /* 460 * We only recognize the abbreviations of a small subset of time zones 461 * in North America, Europe, and Japan. 462 * 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 474 */ 475 476 typedef enum 477 { 478 TT_UNKNOWN, 479 480 TT_SUN, TT_MON, TT_TUE, TT_WED, TT_THU, TT_FRI, TT_SAT, 481 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, 484 485 TT_PST, TT_PDT, TT_MST, TT_MDT, TT_CST, TT_CDT, TT_EST, TT_EDT, 486 TT_AST, TT_NST, TT_GMT, TT_BST, TT_MET, TT_EET, TT_JST 487 } TIME_TOKEN; 488 489 /* 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. 494 * 495 * Many formats are handled, including: 496 * 497 * 14 Apr 89 03:20:12 498 * 14 Apr 89 03:20 GMT 499 * Fri, 17 Mar 89 4:01:33 500 * Fri, 17 Mar 89 4:01 GMT 501 * Mon Jan 16 16:12 PDT 1989 502 * Mon Jan 16 16:12 +0130 1989 503 * 6 May 1992 16:41-JST (Wednesday) 504 * 22-AUG-1993 10:59:12.82 505 * 22-AUG-1993 10:59pm 506 * 22-AUG-1993 12:59am 507 * 22-AUG-1993 12:59 PM 508 * Friday, August 04, 1995 3:54 PM 509 * 06/21/95 04:24:34 PM 510 * 20/06/95 21:07 511 * 95-06-08 19:32:48 EDT 512 * 1995-06-17T23:11:25.342156Z 513 * 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. 519 */ 520 521 PRStatus 522 PR_ParseTimeString( 523 const char *string, 524 PRBool default_to_gmt, 525 PRTime *result_imploded) 526 { 527 PRExplodedTime tm; 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; 533 int dst_offset = 0; 534 int date = -1; 535 PRInt32 year = -1; 536 int hour = -1; 537 int min = -1; 538 int sec = -1; 539 int usec = -1; 540 541 const char *rest = string; 542 543 int iterations = 0; 544 545 PR_ASSERT(string && result); 546 if (!string || !result) return PR_FAILURE; 547 548 while (*rest) 549 { 550 551 if (iterations++ > 1000) 552 { 553 return PR_FAILURE; 554 } 555 556 switch (*rest) 557 { 558 case 'a': case 'A': 559 if (month == TT_UNKNOWN && 560 (rest[1] == 'p' || rest[1] == 'P') && 561 (rest[2] == 'r' || rest[2] == 'R')) 562 month = TT_APR; 563 else if (zone == TT_UNKNOWN && 564 (rest[1] == 's' || rest[1] == 'S') && 565 (rest[2] == 't' || rest[2] == 'T')) 566 zone = TT_AST; 567 else if (month == TT_UNKNOWN && 568 (rest[1] == 'u' || rest[1] == 'U') && 569 (rest[2] == 'g' || rest[2] == 'G')) 570 month = TT_AUG; 571 break; 572 case 'b': case 'B': 573 if (zone == TT_UNKNOWN && 574 (rest[1] == 's' || rest[1] == 'S') && 575 (rest[2] == 't' || rest[2] == 'T')) 576 zone = TT_BST; 577 break; 578 case 'c': case 'C': 579 if (zone == TT_UNKNOWN && 580 (rest[1] == 'd' || rest[1] == 'D') && 581 (rest[2] == 't' || rest[2] == 'T')) 582 zone = TT_CDT; 583 else if (zone == TT_UNKNOWN && 584 (rest[1] == 's' || rest[1] == 'S') && 585 (rest[2] == 't' || rest[2] == 'T')) 586 zone = TT_CST; 587 break; 588 case 'd': case 'D': 589 if (month == TT_UNKNOWN && 590 (rest[1] == 'e' || rest[1] == 'E') && 591 (rest[2] == 'c' || rest[2] == 'C')) 592 month = TT_DEC; 593 break; 594 case 'e': case 'E': 595 if (zone == TT_UNKNOWN && 596 (rest[1] == 'd' || rest[1] == 'D') && 597 (rest[2] == 't' || rest[2] == 'T')) 598 zone = TT_EDT; 599 else if (zone == TT_UNKNOWN && 600 (rest[1] == 'e' || rest[1] == 'E') && 601 (rest[2] == 't' || rest[2] == 'T')) 602 zone = TT_EET; 603 else if (zone == TT_UNKNOWN && 604 (rest[1] == 's' || rest[1] == 'S') && 605 (rest[2] == 't' || rest[2] == 'T')) 606 zone = TT_EST; 607 break; 608 case 'f': case 'F': 609 if (month == TT_UNKNOWN && 610 (rest[1] == 'e' || rest[1] == 'E') && 611 (rest[2] == 'b' || rest[2] == 'B')) 612 month = TT_FEB; 613 else if (dotw == TT_UNKNOWN && 614 (rest[1] == 'r' || rest[1] == 'R') && 615 (rest[2] == 'i' || rest[2] == 'I')) 616 dotw = TT_FRI; 617 break; 618 case 'g': case 'G': 619 if (zone == TT_UNKNOWN && 620 (rest[1] == 'm' || rest[1] == 'M') && 621 (rest[2] == 't' || rest[2] == 'T')) 622 zone = TT_GMT; 623 break; 624 case 'j': case 'J': 625 if (month == TT_UNKNOWN && 626 (rest[1] == 'a' || rest[1] == 'A') && 627 (rest[2] == 'n' || rest[2] == 'N')) 628 month = TT_JAN; 629 else if (zone == TT_UNKNOWN && 630 (rest[1] == 's' || rest[1] == 'S') && 631 (rest[2] == 't' || rest[2] == 'T')) 632 zone = TT_JST; 633 else if (month == TT_UNKNOWN && 634 (rest[1] == 'u' || rest[1] == 'U') && 635 (rest[2] == 'l' || rest[2] == 'L')) 636 month = TT_JUL; 637 else if (month == TT_UNKNOWN && 638 (rest[1] == 'u' || rest[1] == 'U') && 639 (rest[2] == 'n' || rest[2] == 'N')) 640 month = TT_JUN; 641 break; 642 case 'm': case 'M': 643 if (month == TT_UNKNOWN && 644 (rest[1] == 'a' || rest[1] == 'A') && 645 (rest[2] == 'r' || rest[2] == 'R')) 646 month = TT_MAR; 647 else if (month == TT_UNKNOWN && 648 (rest[1] == 'a' || rest[1] == 'A') && 649 (rest[2] == 'y' || rest[2] == 'Y')) 650 month = TT_MAY; 651 else if (zone == TT_UNKNOWN && 652 (rest[1] == 'd' || rest[1] == 'D') && 653 (rest[2] == 't' || rest[2] == 'T')) 654 zone = TT_MDT; 655 else if (zone == TT_UNKNOWN && 656 (rest[1] == 'e' || rest[1] == 'E') && 657 (rest[2] == 't' || rest[2] == 'T')) 658 zone = TT_MET; 659 else if (dotw == TT_UNKNOWN && 660 (rest[1] == 'o' || rest[1] == 'O') && 661 (rest[2] == 'n' || rest[2] == 'N')) 662 dotw = TT_MON; 663 else if (zone == TT_UNKNOWN && 664 (rest[1] == 's' || rest[1] == 'S') && 665 (rest[2] == 't' || rest[2] == 'T')) 666 zone = TT_MST; 667 break; 668 case 'n': case 'N': 669 if (month == TT_UNKNOWN && 670 (rest[1] == 'o' || rest[1] == 'O') && 671 (rest[2] == 'v' || rest[2] == 'V')) 672 month = TT_NOV; 673 else if (zone == TT_UNKNOWN && 674 (rest[1] == 's' || rest[1] == 'S') && 675 (rest[2] == 't' || rest[2] == 'T')) 676 zone = TT_NST; 677 break; 678 case 'o': case 'O': 679 if (month == TT_UNKNOWN && 680 (rest[1] == 'c' || rest[1] == 'C') && 681 (rest[2] == 't' || rest[2] == 'T')) 682 month = TT_OCT; 683 break; 684 case 'p': case 'P': 685 if (zone == TT_UNKNOWN && 686 (rest[1] == 'd' || rest[1] == 'D') && 687 (rest[2] == 't' || rest[2] == 'T')) 688 zone = TT_PDT; 689 else if (zone == TT_UNKNOWN && 690 (rest[1] == 's' || rest[1] == 'S') && 691 (rest[2] == 't' || rest[2] == 'T')) 692 zone = TT_PST; 693 break; 694 case 's': case 'S': 695 if (dotw == TT_UNKNOWN && 696 (rest[1] == 'a' || rest[1] == 'A') && 697 (rest[2] == 't' || rest[2] == 'T')) 698 dotw = TT_SAT; 699 else if (month == TT_UNKNOWN && 700 (rest[1] == 'e' || rest[1] == 'E') && 701 (rest[2] == 'p' || rest[2] == 'P')) 702 month = TT_SEP; 703 else if (dotw == TT_UNKNOWN && 704 (rest[1] == 'u' || rest[1] == 'U') && 705 (rest[2] == 'n' || rest[2] == 'N')) 706 dotw = TT_SUN; 707 break; 708 case 't': case 'T': 709 if (dotw == TT_UNKNOWN && 710 (rest[1] == 'h' || rest[1] == 'H') && 711 (rest[2] == 'u' || rest[2] == 'U')) 712 dotw = TT_THU; 713 else if (dotw == TT_UNKNOWN && 714 (rest[1] == 'u' || rest[1] == 'U') && 715 (rest[2] == 'e' || rest[2] == 'E')) 716 dotw = TT_TUE; 717 break; 718 case 'u': case 'U': 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. */ 724 zone = TT_GMT; 725 break; 726 case 'w': case 'W': 727 if (dotw == TT_UNKNOWN && 728 (rest[1] == 'e' || rest[1] == 'E') && 729 (rest[2] == 'd' || rest[2] == 'D')) 730 dotw = TT_WED; 731 break; 732 733 case '+': case '-': 734 { 735 const char *end; 736 int sign; 737 if (zone_offset != -1) 738 { 739 /* already got one... */ 740 rest++; 741 break; 742 } 743 if (zone != TT_UNKNOWN && zone != TT_GMT) 744 { 745 /* GMT+0300 is legal, but PST+0300 is not. */ 746 rest++; 747 break; 748 } 749 750 sign = ((*rest == '+') ? 1 : -1); 751 rest++; /* move over sign */ 752 end = rest; 753 while (*end >= '0' && *end <= '9') 754 end++; 755 if (rest == end) /* no digits here */ 756 break; 757 758 if ((end - rest) == 4) 759 /* offset in HHMM */ 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; 768 else 769 /* 3 or >4 */ 770 break; 771 772 zone_offset *= sign; 773 zone = TT_GMT; 774 break; 775 } 776 777 case '0': case '1': case '2': case '3': case '4': 778 case '5': case '6': case '7': case '8': case '9': 779 { 780 int tmp_hour = -1; 781 int tmp_min = -1; 782 int tmp_sec = -1; 783 int tmp_usec = -1; 784 const char *end = rest + 1; 785 while (*end >= '0' && *end <= '9') 786 end++; 787 788 /* end is now the first character after a range of digits. */ 789 790 if (*end == ':') 791 { 792 if (hour >= 0 && min >= 0) /* already got it */ 793 break; 794 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]+: */ 798 break; 799 else if ((end - rest) == 2) 800 tmp_hour = ((rest[0]-'0')*10 + 801 (rest[1]-'0')); 802 else 803 tmp_hour = (rest[0]-'0'); 804 805 /* move over the colon, and parse minutes */ 806 807 rest = ++end; 808 while (*end >= '0' && *end <= '9') 809 end++; 810 811 if (end == rest) 812 /* no digits after first colon? */ 813 break; 814 else if ((end - rest) > 2) 815 /* it is [0-9][0-9][0-9]+: */ 816 break; 817 else if ((end - rest) == 2) 818 tmp_min = ((rest[0]-'0')*10 + 819 (rest[1]-'0')); 820 else 821 tmp_min = (rest[0]-'0'); 822 823 /* now go for seconds */ 824 rest = end; 825 if (*rest == ':') 826 rest++; 827 end = rest; 828 while (*end >= '0' && *end <= '9') 829 end++; 830 831 if (end == rest) 832 /* no digits after second colon - that's ok. */ 833 ; 834 else if ((end - rest) > 2) 835 /* it is [0-9][0-9][0-9]+: */ 836 break; 837 else if ((end - rest) == 2) 838 tmp_sec = ((rest[0]-'0')*10 + 839 (rest[1]-'0')); 840 else 841 tmp_sec = (rest[0]-'0'); 842 843 /* fractional second */ 844 rest = end; 845 if (*rest == '.') 846 { 847 rest++; 848 end++; 849 tmp_usec = 0; 850 /* use up to 6 digits, skip over the rest */ 851 while (*end >= '0' && *end <= '9') 852 { 853 if (end - rest < 6) 854 tmp_usec = tmp_usec * 10 + *end - '0'; 855 end++; 856 } 857 int ndigits = end - rest; 858 while (ndigits++ < 6) 859 tmp_usec *= 10; 860 rest = end; 861 } 862 863 if (*rest == 'Z') 864 { 865 zone = TT_GMT; 866 rest++; 867 } 868 else if (tmp_hour <= 12) 869 { 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. 874 */ 875 const char *s = end; 876 while (*s && (*s == ' ' || *s == '\t')) 877 s++; 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 */ 886 tmp_hour = 0; 887 } 888 889 hour = tmp_hour; 890 min = tmp_min; 891 sec = tmp_sec; 892 usec = tmp_usec; 893 rest = end; 894 break; 895 } 896 else if ((*end == '/' || *end == '-') && 897 end[1] >= '0' && end[1] <= '9') 898 { 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. 901 */ 902 int n1, n2, n3; 903 const char *s; 904 905 if (month != TT_UNKNOWN) 906 /* if we saw a month name, this can't be. */ 907 break; 908 909 s = rest; 910 911 n1 = (*s++ - '0'); /* first 1, 2 or 4 digits */ 912 if (*s >= '0' && *s <= '9') 913 { 914 n1 = n1*10 + (*s++ - '0'); 915 916 if (*s >= '0' && *s <= '9') /* optional digits 3 and 4 */ 917 { 918 n1 = n1*10 + (*s++ - '0'); 919 if (*s < '0' || *s > '9') 920 break; 921 n1 = n1*10 + (*s++ - '0'); 922 } 923 } 924 925 if (*s != '/' && *s != '-') /* slash */ 926 break; 927 s++; 928 929 if (*s < '0' || *s > '9') /* second 1 or 2 digits */ 930 break; 931 n2 = (*s++ - '0'); 932 if (*s >= '0' && *s <= '9') 933 n2 = n2*10 + (*s++ - '0'); 934 935 if (*s != '/' && *s != '-') /* slash */ 936 break; 937 s++; 938 939 if (*s < '0' || *s > '9') /* third 1, 2, 4, or 5 digits */ 940 break; 941 n3 = (*s++ - '0'); 942 if (*s >= '0' && *s <= '9') 943 n3 = n3*10 + (*s++ - '0'); 944 945 if (*s >= '0' && *s <= '9') /* optional digits 3, 4, and 5 */ 946 { 947 n3 = n3*10 + (*s++ - '0'); 948 if (*s < '0' || *s > '9') 949 break; 950 n3 = n3*10 + (*s++ - '0'); 951 if (*s >= '0' && *s <= '9') 952 n3 = n3*10 + (*s++ - '0'); 953 } 954 955 if (*s == 'T' && s[1] >= '0' && s[1] <= '9') 956 /* followed by ISO 8601 T delimiter and number is ok */ 957 ; 958 else if ((*s >= '0' && *s <= '9') || 959 (*s >= 'A' && *s <= 'Z') || 960 (*s >= 'a' && *s <= 'z')) 961 /* but other alphanumerics are not ok */ 962 break; 963 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.) 967 */ 968 969 if (n1 > 31 || n1 == 0) /* must be [YY]YY/MM/DD */ 970 { 971 if (n2 > 12) break; 972 if (n3 > 31) break; 973 year = n1; 974 if (year < 70) 975 year += 2000; 976 else if (year < 100) 977 year += 1900; 978 month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1); 979 date = n3; 980 rest = s; 981 break; 982 } 983 984 if (n1 > 12 && n2 > 12) /* illegal */ 985 { 986 rest = s; 987 break; 988 } 989 990 if (n3 < 70) 991 n3 += 2000; 992 else if (n3 < 100) 993 n3 += 1900; 994 995 if (n1 > 12) /* must be DD/MM/YY */ 996 { 997 date = n1; 998 month = (TIME_TOKEN)(n2 + ((int)TT_JAN) - 1); 999 year = n3; 1000 } 1001 else /* assume MM/DD/YY */ 1002 { 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); 1006 date = n2; 1007 year = n3; 1008 } 1009 rest = s; 1010 } 1011 else if ((*end >= 'A' && *end <= 'Z') || 1012 (*end >= 'a' && *end <= 'z')) 1013 /* Digits followed by non-punctuation - what's that? */ 1014 ; 1015 else if ((end - rest) == 5) /* five digits is a year */ 1016 year = (year < 0 1017 ? ((rest[0]-'0')*10000L + 1018 (rest[1]-'0')*1000L + 1019 (rest[2]-'0')*100L + 1020 (rest[3]-'0')*10L + 1021 (rest[4]-'0')) 1022 : year); 1023 else if ((end - rest) == 4) /* four digits is a year */ 1024 year = (year < 0 1025 ? ((rest[0]-'0')*1000L + 1026 (rest[1]-'0')*100L + 1027 (rest[2]-'0')*10L + 1028 (rest[3]-'0')) 1029 : year); 1030 else if ((end - rest) == 2) /* two digits - date or year */ 1031 { 1032 int n = ((rest[0]-'0')*10 + 1033 (rest[1]-'0')); 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. 1036 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. 1041 1042 The world will surely end soon. 1043 */ 1044 if (date < 0 && n < 32) 1045 date = n; 1046 else if (year < 0) 1047 { 1048 if (n < 70) 1049 year = 2000 + n; 1050 else if (n < 100) 1051 year = 1900 + n; 1052 else 1053 year = n; 1054 } 1055 /* else what the hell is this. */ 1056 } 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? */ 1060 1061 break; 1062 } /* case '0' .. '9' */ 1063 } /* switch */ 1064 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). 1068 */ 1069 while (*rest && 1070 *rest != ' ' && *rest != '\t' && 1071 *rest != ',' && *rest != ';' && 1072 *rest != '-' && *rest != '+' && 1073 *rest != '/' && 1074 *rest != '(' && *rest != ')' && *rest != '[' && *rest != ']' && 1075 !(*rest == 'T' && rest[1] >= '0' && rest[1] <= '9') 1076 ) 1077 rest++; 1078 /* skip over uninteresting chars. */ 1079 SKIP_MORE: 1080 while (*rest == ' ' || *rest == '\t' || 1081 *rest == ',' || *rest == ';' || *rest == '/' || 1082 *rest == '(' || *rest == ')' || *rest == '[' || *rest == ']') 1083 rest++; 1084 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')) 1091 { 1092 rest++; 1093 goto SKIP_MORE; 1094 } 1095 1096 /* Skip T that may precede ISO 8601 time. */ 1097 if (*rest == 'T' && rest[1] >= '0' && rest[1] <= '9') 1098 rest++; 1099 } /* while */ 1100 1101 if (zone != TT_UNKNOWN && zone_offset == -1) 1102 { 1103 switch (zone) 1104 { 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; 1120 default: 1121 PR_ASSERT (0); 1122 break; 1123 } 1124 } 1125 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) 1131 return PR_FAILURE; 1132 1133 memset(result, 0, sizeof(*result)); 1134 if (usec != -1) 1135 result->tm_usec = usec; 1136 if (sec != -1) 1137 result->tm_sec = sec; 1138 if (min != -1) 1139 result->tm_min = min; 1140 if (hour != -1) 1141 result->tm_hour = hour; 1142 if (date != -1) 1143 result->tm_mday = date; 1144 if (month != TT_UNKNOWN) 1145 result->tm_month = (((int)month) - ((int)TT_JAN)); 1146 if (year != -1) 1147 result->tm_year = static_cast<PRInt16>(year); 1148 if (dotw != TT_UNKNOWN) 1149 result->tm_wday = static_cast<PRInt8>(((int)dotw) - ((int)TT_SUN)); 1150 /* 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. 1153 */ 1154 PR_NormalizeTime(result, PR_GMTParameters); 1155 /* The remaining work is to set the gmt and dst offsets in tm_params. */ 1156 1157 if (zone == TT_UNKNOWN && default_to_gmt) 1158 { 1159 /* No zone was specified, so pretend the zone was GMT. */ 1160 zone = TT_GMT; 1161 zone_offset = 0; 1162 } 1163 1164 if (zone_offset == -1) 1165 { 1166 /* no zone was specified, and we're to assume that everything 1167 is local. */ 1168 struct tm localTime; 1169 time_t secs; 1170 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); 1176 1177 /* 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). 1186 */ 1187 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) 1191 { 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; 1203 1204 #if _MSC_VER == 1400 /* 1400 = Visual C++ 2005 (8.0) */ 1205 /* 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 1217 * bug 480740.) 1218 */ 1219 if (result->tm_year >= 3000) { 1220 /* Emulate what mktime would have done. */ 1221 errno = EINVAL; 1222 secs = (time_t) -1; 1223 } else { 1224 secs = mktime(&localTime); 1225 } 1226 #else 1227 secs = mktime(&localTime); 1228 #endif 1229 if (secs != (time_t) -1) 1230 { 1231 *result_imploded = (PRInt64)secs * PR_USEC_PER_SEC; 1232 *result_imploded += result->tm_usec; 1233 return PR_SUCCESS; 1234 } 1235 } 1236 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. */ 1240 secs = 86400; 1241 localtime_r(&secs, &localTime); 1242 zone_offset = localTime.tm_min 1243 + 60 * localTime.tm_hour 1244 + 1440 * (localTime.tm_mday - 2); 1245 } 1246 1247 result->tm_params.tp_gmt_offset = zone_offset * 60; 1248 result->tm_params.tp_dst_offset = dst_offset * 60; 1249 1250 *result_imploded = PR_ImplodeTime(result); 1251 return PR_SUCCESS; 1252 } 1253