| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) | |
| 3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. | |
| 4 * Copyright (C) 2009 Google Inc. All rights reserved. | |
| 5 * Copyright (C) 2007-2009 Torch Mobile, Inc. | |
| 6 * Copyright (C) 2010 &yet, LLC. (nate@andyet.net) | |
| 7 * | |
| 8 * The Original Code is Mozilla Communicator client code, released | |
| 9 * March 31, 1998. | |
| 10 * | |
| 11 * The Initial Developer of the Original Code is | |
| 12 * Netscape Communications Corporation. | |
| 13 * Portions created by the Initial Developer are Copyright (C) 1998 | |
| 14 * the Initial Developer. All Rights Reserved. | |
| 15 * | |
| 16 * This library is free software; you can redistribute it and/or | |
| 17 * modify it under the terms of the GNU Lesser General Public | |
| 18 * License as published by the Free Software Foundation; either | |
| 19 * version 2.1 of the License, or (at your option) any later version. | |
| 20 * | |
| 21 * This library is distributed in the hope that it will be useful, | |
| 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 24 * Lesser General Public License for more details. | |
| 25 * | |
| 26 * You should have received a copy of the GNU Lesser General Public | |
| 27 * License along with this library; if not, write to the Free Software | |
| 28 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
| 29 * | |
| 30 * Alternatively, the contents of this file may be used under the terms | |
| 31 * of either the Mozilla Public License Version 1.1, found at | |
| 32 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public | |
| 33 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html | |
| 34 * (the "GPL"), in which case the provisions of the MPL or the GPL are | |
| 35 * applicable instead of those above. If you wish to allow use of your | |
| 36 * version of this file only under the terms of one of those two | |
| 37 * licenses (the MPL or the GPL) and not to allow others to use your | |
| 38 * version of this file under the LGPL, indicate your decision by | |
| 39 * deletingthe provisions above and replace them with the notice and | |
| 40 * other provisions required by the MPL or the GPL, as the case may be. | |
| 41 * If you do not delete the provisions above, a recipient may use your | |
| 42 * version of this file under any of the LGPL, the MPL or the GPL. | |
| 43 | |
| 44 * Copyright 2006-2008 the V8 project authors. All rights reserved. | |
| 45 * Redistribution and use in source and binary forms, with or without | |
| 46 * modification, are permitted provided that the following conditions are | |
| 47 * met: | |
| 48 * | |
| 49 * * Redistributions of source code must retain the above copyright | |
| 50 * notice, this list of conditions and the following disclaimer. | |
| 51 * * Redistributions in binary form must reproduce the above | |
| 52 * copyright notice, this list of conditions and the following | |
| 53 * disclaimer in the documentation and/or other materials provided | |
| 54 * with the distribution. | |
| 55 * * Neither the name of Google Inc. nor the names of its | |
| 56 * contributors may be used to endorse or promote products derived | |
| 57 * from this software without specific prior written permission. | |
| 58 * | |
| 59 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 60 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 61 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 62 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 63 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 64 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 65 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 66 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 69 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 70 */ | |
| 71 | |
| 72 #include "wtf/DateMath.h" | |
| 73 | |
| 74 #include "wtf/ASCIICType.h" | |
| 75 #include "wtf/Assertions.h" | |
| 76 #include "wtf/CurrentTime.h" | |
| 77 #include "wtf/MathExtras.h" | |
| 78 #include "wtf/StdLibExtras.h" | |
| 79 #include "wtf/StringExtras.h" | |
| 80 #include "wtf/text/StringBuilder.h" | |
| 81 #include <algorithm> | |
| 82 #include <limits.h> | |
| 83 #include <limits> | |
| 84 #include <math.h> | |
| 85 #include <stdlib.h> | |
| 86 #include <time.h> | |
| 87 | |
| 88 #if OS(WIN) | |
| 89 #include <windows.h> | |
| 90 #else | |
| 91 #include <sys/time.h> | |
| 92 #endif | |
| 93 | |
| 94 namespace WTF { | |
| 95 | |
| 96 /* Constants */ | |
| 97 | |
| 98 static const double hoursPerDay = 24.0; | |
| 99 static const double secondsPerDay = 24.0 * 60.0 * 60.0; | |
| 100 | |
| 101 static const double maxUnixTime = 2145859200.0; // 12/31/2037 | |
| 102 static const double kMinimumECMADateInMs = -8640000000000000.0; | |
| 103 static const double kMaximumECMADateInMs = 8640000000000000.0; | |
| 104 | |
| 105 // Day of year for the first day of each month, where index 0 is January, and | |
| 106 // day 0 is January 1. First for non-leap years, then for leap years. | |
| 107 static const int firstDayOfMonth[2][12] = { | |
| 108 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, | |
| 109 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}}; | |
| 110 | |
| 111 static inline void getLocalTime(const time_t* localTime, struct tm* localTM) { | |
| 112 #if COMPILER(MSVC) | |
| 113 localtime_s(localTM, localTime); | |
| 114 #else | |
| 115 localtime_r(localTime, localTM); | |
| 116 #endif | |
| 117 } | |
| 118 | |
| 119 bool isLeapYear(int year) { | |
| 120 if (year % 4 != 0) | |
| 121 return false; | |
| 122 if (year % 400 == 0) | |
| 123 return true; | |
| 124 if (year % 100 == 0) | |
| 125 return false; | |
| 126 return true; | |
| 127 } | |
| 128 | |
| 129 static inline int daysInYear(int year) { | |
| 130 return 365 + isLeapYear(year); | |
| 131 } | |
| 132 | |
| 133 static inline double daysFrom1970ToYear(int year) { | |
| 134 // The Gregorian Calendar rules for leap years: | |
| 135 // Every fourth year is a leap year. 2004, 2008, and 2012 are leap years. | |
| 136 // However, every hundredth year is not a leap year. 1900 and 2100 are not | |
| 137 // leap years. | |
| 138 // Every four hundred years, there's a leap year after all. 2000 and 2400 are | |
| 139 // leap years. | |
| 140 | |
| 141 static const int leapDaysBefore1971By4Rule = 1970 / 4; | |
| 142 static const int excludedLeapDaysBefore1971By100Rule = 1970 / 100; | |
| 143 static const int leapDaysBefore1971By400Rule = 1970 / 400; | |
| 144 | |
| 145 const double yearMinusOne = year - 1; | |
| 146 const double yearsToAddBy4Rule = | |
| 147 floor(yearMinusOne / 4.0) - leapDaysBefore1971By4Rule; | |
| 148 const double yearsToExcludeBy100Rule = | |
| 149 floor(yearMinusOne / 100.0) - excludedLeapDaysBefore1971By100Rule; | |
| 150 const double yearsToAddBy400Rule = | |
| 151 floor(yearMinusOne / 400.0) - leapDaysBefore1971By400Rule; | |
| 152 | |
| 153 return 365.0 * (year - 1970) + yearsToAddBy4Rule - yearsToExcludeBy100Rule + | |
| 154 yearsToAddBy400Rule; | |
| 155 } | |
| 156 | |
| 157 static double msToDays(double ms) { | |
| 158 return floor(ms / msPerDay); | |
| 159 } | |
| 160 | |
| 161 static void appendTwoDigitNumber(StringBuilder& builder, int number) { | |
| 162 DCHECK_GE(number, 0); | |
| 163 DCHECK_LT(number, 100); | |
| 164 if (number <= 9) | |
| 165 builder.append('0'); | |
| 166 builder.appendNumber(number); | |
| 167 } | |
| 168 | |
| 169 int msToYear(double ms) { | |
| 170 DCHECK(std::isfinite(ms)); | |
| 171 DCHECK_GE(ms, kMinimumECMADateInMs); | |
| 172 DCHECK_LE(ms, kMaximumECMADateInMs); | |
| 173 int approxYear = static_cast<int>(floor(ms / (msPerDay * 365.2425)) + 1970); | |
| 174 double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear); | |
| 175 if (msFromApproxYearTo1970 > ms) | |
| 176 return approxYear - 1; | |
| 177 if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms) | |
| 178 return approxYear + 1; | |
| 179 return approxYear; | |
| 180 } | |
| 181 | |
| 182 int dayInYear(double ms, int year) { | |
| 183 return static_cast<int>(msToDays(ms) - daysFrom1970ToYear(year)); | |
| 184 } | |
| 185 | |
| 186 static inline double msToMilliseconds(double ms) { | |
| 187 double result = fmod(ms, msPerDay); | |
| 188 if (result < 0) | |
| 189 result += msPerDay; | |
| 190 return result; | |
| 191 } | |
| 192 | |
| 193 int monthFromDayInYear(int dayInYear, bool leapYear) { | |
| 194 const int d = dayInYear; | |
| 195 int step; | |
| 196 | |
| 197 if (d < (step = 31)) | |
| 198 return 0; | |
| 199 step += (leapYear ? 29 : 28); | |
| 200 if (d < step) | |
| 201 return 1; | |
| 202 if (d < (step += 31)) | |
| 203 return 2; | |
| 204 if (d < (step += 30)) | |
| 205 return 3; | |
| 206 if (d < (step += 31)) | |
| 207 return 4; | |
| 208 if (d < (step += 30)) | |
| 209 return 5; | |
| 210 if (d < (step += 31)) | |
| 211 return 6; | |
| 212 if (d < (step += 31)) | |
| 213 return 7; | |
| 214 if (d < (step += 30)) | |
| 215 return 8; | |
| 216 if (d < (step += 31)) | |
| 217 return 9; | |
| 218 if (d < (step += 30)) | |
| 219 return 10; | |
| 220 return 11; | |
| 221 } | |
| 222 | |
| 223 static inline bool checkMonth(int dayInYear, | |
| 224 int& startDayOfThisMonth, | |
| 225 int& startDayOfNextMonth, | |
| 226 int daysInThisMonth) { | |
| 227 startDayOfThisMonth = startDayOfNextMonth; | |
| 228 startDayOfNextMonth += daysInThisMonth; | |
| 229 return (dayInYear <= startDayOfNextMonth); | |
| 230 } | |
| 231 | |
| 232 int dayInMonthFromDayInYear(int dayInYear, bool leapYear) { | |
| 233 const int d = dayInYear; | |
| 234 int step; | |
| 235 int next = 30; | |
| 236 | |
| 237 if (d <= next) | |
| 238 return d + 1; | |
| 239 const int daysInFeb = (leapYear ? 29 : 28); | |
| 240 if (checkMonth(d, step, next, daysInFeb)) | |
| 241 return d - step; | |
| 242 if (checkMonth(d, step, next, 31)) | |
| 243 return d - step; | |
| 244 if (checkMonth(d, step, next, 30)) | |
| 245 return d - step; | |
| 246 if (checkMonth(d, step, next, 31)) | |
| 247 return d - step; | |
| 248 if (checkMonth(d, step, next, 30)) | |
| 249 return d - step; | |
| 250 if (checkMonth(d, step, next, 31)) | |
| 251 return d - step; | |
| 252 if (checkMonth(d, step, next, 31)) | |
| 253 return d - step; | |
| 254 if (checkMonth(d, step, next, 30)) | |
| 255 return d - step; | |
| 256 if (checkMonth(d, step, next, 31)) | |
| 257 return d - step; | |
| 258 if (checkMonth(d, step, next, 30)) | |
| 259 return d - step; | |
| 260 step = next; | |
| 261 return d - step; | |
| 262 } | |
| 263 | |
| 264 int dayInYear(int year, int month, int day) { | |
| 265 return firstDayOfMonth[isLeapYear(year)][month] + day - 1; | |
| 266 } | |
| 267 | |
| 268 double dateToDaysFrom1970(int year, int month, int day) { | |
| 269 year += month / 12; | |
| 270 | |
| 271 month %= 12; | |
| 272 if (month < 0) { | |
| 273 month += 12; | |
| 274 --year; | |
| 275 } | |
| 276 | |
| 277 double yearday = floor(daysFrom1970ToYear(year)); | |
| 278 DCHECK((year >= 1970 && yearday >= 0) || (year < 1970 && yearday < 0)); | |
| 279 return yearday + dayInYear(year, month, day); | |
| 280 } | |
| 281 | |
| 282 // There is a hard limit at 2038 that we currently do not have a workaround | |
| 283 // for (rdar://problem/5052975). | |
| 284 static inline int maximumYearForDST() { | |
| 285 return 2037; | |
| 286 } | |
| 287 | |
| 288 static inline double jsCurrentTime() { | |
| 289 // JavaScript doesn't recognize fractions of a millisecond. | |
| 290 return floor(WTF::currentTimeMS()); | |
| 291 } | |
| 292 | |
| 293 static inline int minimumYearForDST() { | |
| 294 // Because of the 2038 issue (see maximumYearForDST) if the current year is | |
| 295 // greater than the max year minus 27 (2010), we want to use the max year | |
| 296 // minus 27 instead, to ensure there is a range of 28 years that all years | |
| 297 // can map to. | |
| 298 return std::min(msToYear(jsCurrentTime()), maximumYearForDST() - 27); | |
| 299 } | |
| 300 | |
| 301 // Find an equivalent year for the one given, where equivalence is deterined by | |
| 302 // the two years having the same leapness and the first day of the year, falling | |
| 303 // on the same day of the week. | |
| 304 // | |
| 305 // This function returns a year between this current year and 2037, however this | |
| 306 // function will potentially return incorrect results if the current year is | |
| 307 // after 2010, (rdar://problem/5052975), if the year passed in is before 1900 | |
| 308 // or after 2100, (rdar://problem/5055038). | |
| 309 static int equivalentYearForDST(int year) { | |
| 310 // It is ok if the cached year is not the current year as long as the rules | |
| 311 // for DST did not change between the two years; if they did the app would | |
| 312 // need to be restarted. | |
| 313 static int minYear = minimumYearForDST(); | |
| 314 int maxYear = maximumYearForDST(); | |
| 315 | |
| 316 int difference; | |
| 317 if (year > maxYear) | |
| 318 difference = minYear - year; | |
| 319 else if (year < minYear) | |
| 320 difference = maxYear - year; | |
| 321 else | |
| 322 return year; | |
| 323 | |
| 324 int quotient = difference / 28; | |
| 325 int product = (quotient)*28; | |
| 326 | |
| 327 year += product; | |
| 328 DCHECK((year >= minYear && year <= maxYear) || | |
| 329 (product - year == | |
| 330 static_cast<int>(std::numeric_limits<double>::quiet_NaN()))); | |
| 331 return year; | |
| 332 } | |
| 333 | |
| 334 static double calculateUTCOffset() { | |
| 335 #if OS(WIN) | |
| 336 TIME_ZONE_INFORMATION timeZoneInformation; | |
| 337 GetTimeZoneInformation(&timeZoneInformation); | |
| 338 int32_t bias = timeZoneInformation.Bias + timeZoneInformation.StandardBias; | |
| 339 return -bias * 60 * 1000; | |
| 340 #else | |
| 341 time_t localTime = time(0); | |
| 342 tm localt; | |
| 343 getLocalTime(&localTime, &localt); | |
| 344 | |
| 345 // tm_gmtoff includes any daylight savings offset, so subtract it. | |
| 346 return static_cast<double>(localt.tm_gmtoff * msPerSecond - | |
| 347 (localt.tm_isdst > 0 ? msPerHour : 0)); | |
| 348 #endif | |
| 349 } | |
| 350 | |
| 351 /* | |
| 352 * Get the DST offset for the time passed in. | |
| 353 */ | |
| 354 static double calculateDSTOffsetSimple(double localTimeSeconds, | |
| 355 double utcOffset) { | |
| 356 if (localTimeSeconds > maxUnixTime) | |
| 357 localTimeSeconds = maxUnixTime; | |
| 358 else if (localTimeSeconds < | |
| 359 0) // Go ahead a day to make localtime work (does not work with 0) | |
| 360 localTimeSeconds += secondsPerDay; | |
| 361 | |
| 362 // FIXME: time_t has a potential problem in 2038 | |
| 363 time_t localTime = static_cast<time_t>(localTimeSeconds); | |
| 364 | |
| 365 tm localTM; | |
| 366 getLocalTime(&localTime, &localTM); | |
| 367 | |
| 368 return localTM.tm_isdst > 0 ? msPerHour : 0; | |
| 369 } | |
| 370 | |
| 371 // Get the DST offset, given a time in UTC | |
| 372 static double calculateDSTOffset(double ms, double utcOffset) { | |
| 373 // On macOS, the call to localtime (see calculateDSTOffsetSimple) will return | |
| 374 // historically accurate DST information (e.g. New Zealand did not have DST | |
| 375 // from 1946 to 1974) however the JavaScript standard explicitly dictates | |
| 376 // that historical information should not be considered when determining DST. | |
| 377 // For this reason we shift away from years that localtime can handle but | |
| 378 // would return historically accurate information. | |
| 379 int year = msToYear(ms); | |
| 380 int equivalentYear = equivalentYearForDST(year); | |
| 381 if (year != equivalentYear) { | |
| 382 bool leapYear = isLeapYear(year); | |
| 383 int dayInYearLocal = dayInYear(ms, year); | |
| 384 int dayInMonth = dayInMonthFromDayInYear(dayInYearLocal, leapYear); | |
| 385 int month = monthFromDayInYear(dayInYearLocal, leapYear); | |
| 386 double day = dateToDaysFrom1970(equivalentYear, month, dayInMonth); | |
| 387 ms = (day * msPerDay) + msToMilliseconds(ms); | |
| 388 } | |
| 389 | |
| 390 return calculateDSTOffsetSimple(ms / msPerSecond, utcOffset); | |
| 391 } | |
| 392 | |
| 393 void initializeDates() { | |
| 394 #if DCHECK_IS_ON() | |
| 395 static bool alreadyInitialized; | |
| 396 DCHECK(!alreadyInitialized); | |
| 397 alreadyInitialized = true; | |
| 398 #endif | |
| 399 | |
| 400 equivalentYearForDST( | |
| 401 2000); // Need to call once to initialize a static used in this function. | |
| 402 } | |
| 403 | |
| 404 static inline double ymdhmsToSeconds(int year, | |
| 405 long mon, | |
| 406 long day, | |
| 407 long hour, | |
| 408 long minute, | |
| 409 double second) { | |
| 410 double days = | |
| 411 (day - 32075) + floor(1461 * (year + 4800.0 + (mon - 14) / 12) / 4) + | |
| 412 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12 - | |
| 413 floor(3 * ((year + 4900.0 + (mon - 14) / 12) / 100) / 4) - 2440588; | |
| 414 return ((days * hoursPerDay + hour) * minutesPerHour + minute) * | |
| 415 secondsPerMinute + | |
| 416 second; | |
| 417 } | |
| 418 | |
| 419 // We follow the recommendation of RFC 2822 to consider all | |
| 420 // obsolete time zones not listed here equivalent to "-0000". | |
| 421 static const struct KnownZone { | |
| 422 #if !OS(WIN) | |
| 423 const | |
| 424 #endif | |
| 425 char tzName[4]; | |
| 426 int tzOffset; | |
| 427 } known_zones[] = {{"UT", 0}, {"GMT", 0}, {"EST", -300}, {"EDT", -240}, | |
| 428 {"CST", -360}, {"CDT", -300}, {"MST", -420}, {"MDT", -360}, | |
| 429 {"PST", -480}, {"PDT", -420}}; | |
| 430 | |
| 431 inline static void skipSpacesAndComments(const char*& s) { | |
| 432 int nesting = 0; | |
| 433 char ch; | |
| 434 while ((ch = *s)) { | |
| 435 if (!isASCIISpace(ch)) { | |
| 436 if (ch == '(') | |
| 437 nesting++; | |
| 438 else if (ch == ')' && nesting > 0) | |
| 439 nesting--; | |
| 440 else if (nesting == 0) | |
| 441 break; | |
| 442 } | |
| 443 s++; | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 // returns 0-11 (Jan-Dec); -1 on failure | |
| 448 static int findMonth(const char* monthStr) { | |
| 449 DCHECK(monthStr); | |
| 450 char needle[4]; | |
| 451 for (int i = 0; i < 3; ++i) { | |
| 452 if (!*monthStr) | |
| 453 return -1; | |
| 454 needle[i] = static_cast<char>(toASCIILower(*monthStr++)); | |
| 455 } | |
| 456 needle[3] = '\0'; | |
| 457 const char* haystack = "janfebmaraprmayjunjulaugsepoctnovdec"; | |
| 458 const char* str = strstr(haystack, needle); | |
| 459 if (str) { | |
| 460 int position = static_cast<int>(str - haystack); | |
| 461 if (position % 3 == 0) | |
| 462 return position / 3; | |
| 463 } | |
| 464 return -1; | |
| 465 } | |
| 466 | |
| 467 static bool parseInt(const char* string, | |
| 468 char** stopPosition, | |
| 469 int base, | |
| 470 int* result) { | |
| 471 long longResult = strtol(string, stopPosition, base); | |
| 472 // Avoid the use of errno as it is not available on Windows CE | |
| 473 if (string == *stopPosition || | |
| 474 longResult <= std::numeric_limits<int>::min() || | |
| 475 longResult >= std::numeric_limits<int>::max()) | |
| 476 return false; | |
| 477 *result = static_cast<int>(longResult); | |
| 478 return true; | |
| 479 } | |
| 480 | |
| 481 static bool parseLong(const char* string, | |
| 482 char** stopPosition, | |
| 483 int base, | |
| 484 long* result) { | |
| 485 *result = strtol(string, stopPosition, base); | |
| 486 // Avoid the use of errno as it is not available on Windows CE | |
| 487 if (string == *stopPosition || *result == std::numeric_limits<long>::min() || | |
| 488 *result == std::numeric_limits<long>::max()) | |
| 489 return false; | |
| 490 return true; | |
| 491 } | |
| 492 | |
| 493 // Odd case where 'exec' is allowed to be 0, to accomodate a caller in WebCore. | |
| 494 static double parseDateFromNullTerminatedCharacters(const char* dateString, | |
| 495 bool& haveTZ, | |
| 496 int& offset) { | |
| 497 haveTZ = false; | |
| 498 offset = 0; | |
| 499 | |
| 500 // This parses a date in the form: | |
| 501 // Tuesday, 09-Nov-99 23:12:40 GMT | |
| 502 // or | |
| 503 // Sat, 01-Jan-2000 08:00:00 GMT | |
| 504 // or | |
| 505 // Sat, 01 Jan 2000 08:00:00 GMT | |
| 506 // or | |
| 507 // 01 Jan 99 22:00 +0100 (exceptions in rfc822/rfc2822) | |
| 508 // ### non RFC formats, added for Javascript: | |
| 509 // [Wednesday] January 09 1999 23:12:40 GMT | |
| 510 // [Wednesday] January 09 23:12:40 GMT 1999 | |
| 511 // | |
| 512 // We ignore the weekday. | |
| 513 | |
| 514 // Skip leading space | |
| 515 skipSpacesAndComments(dateString); | |
| 516 | |
| 517 long month = -1; | |
| 518 const char* wordStart = dateString; | |
| 519 // Check contents of first words if not number | |
| 520 while (*dateString && !isASCIIDigit(*dateString)) { | |
| 521 if (isASCIISpace(*dateString) || *dateString == '(') { | |
| 522 if (dateString - wordStart >= 3) | |
| 523 month = findMonth(wordStart); | |
| 524 skipSpacesAndComments(dateString); | |
| 525 wordStart = dateString; | |
| 526 } else { | |
| 527 dateString++; | |
| 528 } | |
| 529 } | |
| 530 | |
| 531 // Missing delimiter between month and day (like "January29")? | |
| 532 if (month == -1 && wordStart != dateString) | |
| 533 month = findMonth(wordStart); | |
| 534 | |
| 535 skipSpacesAndComments(dateString); | |
| 536 | |
| 537 if (!*dateString) | |
| 538 return std::numeric_limits<double>::quiet_NaN(); | |
| 539 | |
| 540 // ' 09-Nov-99 23:12:40 GMT' | |
| 541 char* newPosStr; | |
| 542 long day; | |
| 543 if (!parseLong(dateString, &newPosStr, 10, &day)) | |
| 544 return std::numeric_limits<double>::quiet_NaN(); | |
| 545 dateString = newPosStr; | |
| 546 | |
| 547 if (!*dateString) | |
| 548 return std::numeric_limits<double>::quiet_NaN(); | |
| 549 | |
| 550 if (day < 0) | |
| 551 return std::numeric_limits<double>::quiet_NaN(); | |
| 552 | |
| 553 int year = 0; | |
| 554 if (day > 31) { | |
| 555 // ### where is the boundary and what happens below? | |
| 556 if (*dateString != '/') | |
| 557 return std::numeric_limits<double>::quiet_NaN(); | |
| 558 // looks like a YYYY/MM/DD date | |
| 559 if (!*++dateString) | |
| 560 return std::numeric_limits<double>::quiet_NaN(); | |
| 561 if (day <= std::numeric_limits<int>::min() || | |
| 562 day >= std::numeric_limits<int>::max()) | |
| 563 return std::numeric_limits<double>::quiet_NaN(); | |
| 564 year = static_cast<int>(day); | |
| 565 if (!parseLong(dateString, &newPosStr, 10, &month)) | |
| 566 return std::numeric_limits<double>::quiet_NaN(); | |
| 567 month -= 1; | |
| 568 dateString = newPosStr; | |
| 569 if (*dateString++ != '/' || !*dateString) | |
| 570 return std::numeric_limits<double>::quiet_NaN(); | |
| 571 if (!parseLong(dateString, &newPosStr, 10, &day)) | |
| 572 return std::numeric_limits<double>::quiet_NaN(); | |
| 573 dateString = newPosStr; | |
| 574 } else if (*dateString == '/' && month == -1) { | |
| 575 dateString++; | |
| 576 // This looks like a MM/DD/YYYY date, not an RFC date. | |
| 577 month = day - 1; // 0-based | |
| 578 if (!parseLong(dateString, &newPosStr, 10, &day)) | |
| 579 return std::numeric_limits<double>::quiet_NaN(); | |
| 580 if (day < 1 || day > 31) | |
| 581 return std::numeric_limits<double>::quiet_NaN(); | |
| 582 dateString = newPosStr; | |
| 583 if (*dateString == '/') | |
| 584 dateString++; | |
| 585 if (!*dateString) | |
| 586 return std::numeric_limits<double>::quiet_NaN(); | |
| 587 } else { | |
| 588 if (*dateString == '-') | |
| 589 dateString++; | |
| 590 | |
| 591 skipSpacesAndComments(dateString); | |
| 592 | |
| 593 if (*dateString == ',') | |
| 594 dateString++; | |
| 595 | |
| 596 if (month == -1) { // not found yet | |
| 597 month = findMonth(dateString); | |
| 598 if (month == -1) | |
| 599 return std::numeric_limits<double>::quiet_NaN(); | |
| 600 | |
| 601 while (*dateString && *dateString != '-' && *dateString != ',' && | |
| 602 !isASCIISpace(*dateString)) | |
| 603 dateString++; | |
| 604 | |
| 605 if (!*dateString) | |
| 606 return std::numeric_limits<double>::quiet_NaN(); | |
| 607 | |
| 608 // '-99 23:12:40 GMT' | |
| 609 if (*dateString != '-' && *dateString != '/' && *dateString != ',' && | |
| 610 !isASCIISpace(*dateString)) | |
| 611 return std::numeric_limits<double>::quiet_NaN(); | |
| 612 dateString++; | |
| 613 } | |
| 614 } | |
| 615 | |
| 616 if (month < 0 || month > 11) | |
| 617 return std::numeric_limits<double>::quiet_NaN(); | |
| 618 | |
| 619 // '99 23:12:40 GMT' | |
| 620 if (year <= 0 && *dateString) { | |
| 621 if (!parseInt(dateString, &newPosStr, 10, &year)) | |
| 622 return std::numeric_limits<double>::quiet_NaN(); | |
| 623 } | |
| 624 | |
| 625 // Don't fail if the time is missing. | |
| 626 long hour = 0; | |
| 627 long minute = 0; | |
| 628 long second = 0; | |
| 629 if (!*newPosStr) { | |
| 630 dateString = newPosStr; | |
| 631 } else { | |
| 632 // ' 23:12:40 GMT' | |
| 633 if (!(isASCIISpace(*newPosStr) || *newPosStr == ',')) { | |
| 634 if (*newPosStr != ':') | |
| 635 return std::numeric_limits<double>::quiet_NaN(); | |
| 636 // There was no year; the number was the hour. | |
| 637 year = -1; | |
| 638 } else { | |
| 639 // in the normal case (we parsed the year), advance to the next number | |
| 640 dateString = ++newPosStr; | |
| 641 skipSpacesAndComments(dateString); | |
| 642 } | |
| 643 | |
| 644 parseLong(dateString, &newPosStr, 10, &hour); | |
| 645 // Do not check for errno here since we want to continue | |
| 646 // even if errno was set becasue we are still looking | |
| 647 // for the timezone! | |
| 648 | |
| 649 // Read a number? If not, this might be a timezone name. | |
| 650 if (newPosStr != dateString) { | |
| 651 dateString = newPosStr; | |
| 652 | |
| 653 if (hour < 0 || hour > 23) | |
| 654 return std::numeric_limits<double>::quiet_NaN(); | |
| 655 | |
| 656 if (!*dateString) | |
| 657 return std::numeric_limits<double>::quiet_NaN(); | |
| 658 | |
| 659 // ':12:40 GMT' | |
| 660 if (*dateString++ != ':') | |
| 661 return std::numeric_limits<double>::quiet_NaN(); | |
| 662 | |
| 663 if (!parseLong(dateString, &newPosStr, 10, &minute)) | |
| 664 return std::numeric_limits<double>::quiet_NaN(); | |
| 665 dateString = newPosStr; | |
| 666 | |
| 667 if (minute < 0 || minute > 59) | |
| 668 return std::numeric_limits<double>::quiet_NaN(); | |
| 669 | |
| 670 // ':40 GMT' | |
| 671 if (*dateString && *dateString != ':' && !isASCIISpace(*dateString)) | |
| 672 return std::numeric_limits<double>::quiet_NaN(); | |
| 673 | |
| 674 // seconds are optional in rfc822 + rfc2822 | |
| 675 if (*dateString == ':') { | |
| 676 dateString++; | |
| 677 | |
| 678 if (!parseLong(dateString, &newPosStr, 10, &second)) | |
| 679 return std::numeric_limits<double>::quiet_NaN(); | |
| 680 dateString = newPosStr; | |
| 681 | |
| 682 if (second < 0 || second > 59) | |
| 683 return std::numeric_limits<double>::quiet_NaN(); | |
| 684 } | |
| 685 | |
| 686 skipSpacesAndComments(dateString); | |
| 687 | |
| 688 if (strncasecmp(dateString, "AM", 2) == 0) { | |
| 689 if (hour > 12) | |
| 690 return std::numeric_limits<double>::quiet_NaN(); | |
| 691 if (hour == 12) | |
| 692 hour = 0; | |
| 693 dateString += 2; | |
| 694 skipSpacesAndComments(dateString); | |
| 695 } else if (strncasecmp(dateString, "PM", 2) == 0) { | |
| 696 if (hour > 12) | |
| 697 return std::numeric_limits<double>::quiet_NaN(); | |
| 698 if (hour != 12) | |
| 699 hour += 12; | |
| 700 dateString += 2; | |
| 701 skipSpacesAndComments(dateString); | |
| 702 } | |
| 703 } | |
| 704 } | |
| 705 | |
| 706 // The year may be after the time but before the time zone. | |
| 707 if (isASCIIDigit(*dateString) && year == -1) { | |
| 708 if (!parseInt(dateString, &newPosStr, 10, &year)) | |
| 709 return std::numeric_limits<double>::quiet_NaN(); | |
| 710 dateString = newPosStr; | |
| 711 skipSpacesAndComments(dateString); | |
| 712 } | |
| 713 | |
| 714 // Don't fail if the time zone is missing. | |
| 715 // Some websites omit the time zone (4275206). | |
| 716 if (*dateString) { | |
| 717 if (strncasecmp(dateString, "GMT", 3) == 0 || | |
| 718 strncasecmp(dateString, "UTC", 3) == 0) { | |
| 719 dateString += 3; | |
| 720 haveTZ = true; | |
| 721 } | |
| 722 | |
| 723 if (*dateString == '+' || *dateString == '-') { | |
| 724 int o; | |
| 725 if (!parseInt(dateString, &newPosStr, 10, &o)) | |
| 726 return std::numeric_limits<double>::quiet_NaN(); | |
| 727 dateString = newPosStr; | |
| 728 | |
| 729 if (o < -9959 || o > 9959) | |
| 730 return std::numeric_limits<double>::quiet_NaN(); | |
| 731 | |
| 732 int sgn = (o < 0) ? -1 : 1; | |
| 733 o = abs(o); | |
| 734 if (*dateString != ':') { | |
| 735 if (o >= 24) | |
| 736 offset = ((o / 100) * 60 + (o % 100)) * sgn; | |
| 737 else | |
| 738 offset = o * 60 * sgn; | |
| 739 } else { // GMT+05:00 | |
| 740 ++dateString; // skip the ':' | |
| 741 int o2; | |
| 742 if (!parseInt(dateString, &newPosStr, 10, &o2)) | |
| 743 return std::numeric_limits<double>::quiet_NaN(); | |
| 744 dateString = newPosStr; | |
| 745 offset = (o * 60 + o2) * sgn; | |
| 746 } | |
| 747 haveTZ = true; | |
| 748 } else { | |
| 749 for (size_t i = 0; i < WTF_ARRAY_LENGTH(known_zones); ++i) { | |
| 750 if (0 == strncasecmp(dateString, known_zones[i].tzName, | |
| 751 strlen(known_zones[i].tzName))) { | |
| 752 offset = known_zones[i].tzOffset; | |
| 753 dateString += strlen(known_zones[i].tzName); | |
| 754 haveTZ = true; | |
| 755 break; | |
| 756 } | |
| 757 } | |
| 758 } | |
| 759 } | |
| 760 | |
| 761 skipSpacesAndComments(dateString); | |
| 762 | |
| 763 if (*dateString && year == -1) { | |
| 764 if (!parseInt(dateString, &newPosStr, 10, &year)) | |
| 765 return std::numeric_limits<double>::quiet_NaN(); | |
| 766 dateString = newPosStr; | |
| 767 skipSpacesAndComments(dateString); | |
| 768 } | |
| 769 | |
| 770 // Trailing garbage | |
| 771 if (*dateString) | |
| 772 return std::numeric_limits<double>::quiet_NaN(); | |
| 773 | |
| 774 // Y2K: Handle 2 digit years. | |
| 775 if (year >= 0 && year < 100) { | |
| 776 if (year < 50) | |
| 777 year += 2000; | |
| 778 else | |
| 779 year += 1900; | |
| 780 } | |
| 781 | |
| 782 return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * | |
| 783 msPerSecond; | |
| 784 } | |
| 785 | |
| 786 double parseDateFromNullTerminatedCharacters(const char* dateString) { | |
| 787 bool haveTZ; | |
| 788 int offset; | |
| 789 double ms = parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset); | |
| 790 if (std::isnan(ms)) | |
| 791 return std::numeric_limits<double>::quiet_NaN(); | |
| 792 | |
| 793 // fall back to local timezone | |
| 794 if (!haveTZ) { | |
| 795 double utcOffset = calculateUTCOffset(); | |
| 796 double dstOffset = calculateDSTOffset(ms, utcOffset); | |
| 797 offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute); | |
| 798 } | |
| 799 return ms - (offset * msPerMinute); | |
| 800 } | |
| 801 | |
| 802 // See http://tools.ietf.org/html/rfc2822#section-3.3 for more information. | |
| 803 String makeRFC2822DateString(unsigned dayOfWeek, | |
| 804 unsigned day, | |
| 805 unsigned month, | |
| 806 unsigned year, | |
| 807 unsigned hours, | |
| 808 unsigned minutes, | |
| 809 unsigned seconds, | |
| 810 int utcOffset) { | |
| 811 StringBuilder stringBuilder; | |
| 812 stringBuilder.append(weekdayName[dayOfWeek]); | |
| 813 stringBuilder.append(", "); | |
| 814 stringBuilder.appendNumber(day); | |
| 815 stringBuilder.append(' '); | |
| 816 stringBuilder.append(monthName[month]); | |
| 817 stringBuilder.append(' '); | |
| 818 stringBuilder.appendNumber(year); | |
| 819 stringBuilder.append(' '); | |
| 820 | |
| 821 appendTwoDigitNumber(stringBuilder, hours); | |
| 822 stringBuilder.append(':'); | |
| 823 appendTwoDigitNumber(stringBuilder, minutes); | |
| 824 stringBuilder.append(':'); | |
| 825 appendTwoDigitNumber(stringBuilder, seconds); | |
| 826 stringBuilder.append(' '); | |
| 827 | |
| 828 stringBuilder.append(utcOffset > 0 ? '+' : '-'); | |
| 829 int absoluteUTCOffset = abs(utcOffset); | |
| 830 appendTwoDigitNumber(stringBuilder, absoluteUTCOffset / 60); | |
| 831 appendTwoDigitNumber(stringBuilder, absoluteUTCOffset % 60); | |
| 832 | |
| 833 return stringBuilder.toString(); | |
| 834 } | |
| 835 | |
| 836 double convertToLocalTime(double ms) { | |
| 837 double utcOffset = calculateUTCOffset(); | |
| 838 double dstOffset = calculateDSTOffset(ms, utcOffset); | |
| 839 return (ms + utcOffset + dstOffset); | |
| 840 } | |
| 841 | |
| 842 } // namespace WTF | |
| OLD | NEW |