| 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 US
A | |
| 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 "config.h" | |
| 73 #include "DateMath.h" | |
| 74 | |
| 75 #include "Assertions.h" | |
| 76 #include "ASCIICType.h" | |
| 77 #include "CurrentTime.h" | |
| 78 #include "MathExtras.h" | |
| 79 #include "StdLibExtras.h" | |
| 80 #include "StringExtras.h" | |
| 81 | |
| 82 #include <algorithm> | |
| 83 #include <limits.h> | |
| 84 #include <limits> | |
| 85 #include <stdint.h> | |
| 86 #include <time.h> | |
| 87 #include <wtf/text/StringBuilder.h> | |
| 88 | |
| 89 #if OS(WINDOWS) | |
| 90 #include <windows.h> | |
| 91 #endif | |
| 92 | |
| 93 #if HAVE(ERRNO_H) | |
| 94 #include <errno.h> | |
| 95 #endif | |
| 96 | |
| 97 #if HAVE(SYS_TIME_H) | |
| 98 #include <sys/time.h> | |
| 99 #endif | |
| 100 | |
| 101 #if HAVE(SYS_TIMEB_H) | |
| 102 #include <sys/timeb.h> | |
| 103 #endif | |
| 104 | |
| 105 #if OS(QNX) | |
| 106 // qnx6 defines timegm in nbutil.h | |
| 107 #include <nbutil.h> | |
| 108 #endif | |
| 109 | |
| 110 using namespace WTF; | |
| 111 | |
| 112 namespace WTF { | |
| 113 | |
| 114 /* Constants */ | |
| 115 | |
| 116 static const double minutesPerDay = 24.0 * 60.0; | |
| 117 static const double secondsPerDay = 24.0 * 60.0 * 60.0; | |
| 118 static const double secondsPerYear = 24.0 * 60.0 * 60.0 * 365.0; | |
| 119 | |
| 120 static const double usecPerSec = 1000000.0; | |
| 121 | |
| 122 static const double maxUnixTime = 2145859200.0; // 12/31/2037 | |
| 123 // ECMAScript asks not to support for a date of which total | |
| 124 // millisecond value is larger than the following value. | |
| 125 // See 15.9.1.14 of ECMA-262 5th edition. | |
| 126 static const double maxECMAScriptTime = 8.64E15; | |
| 127 | |
| 128 // Day of year for the first day of each month, where index 0 is January, and da
y 0 is January 1. | |
| 129 // First for non-leap years, then for leap years. | |
| 130 static const int firstDayOfMonth[2][12] = { | |
| 131 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}, | |
| 132 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335} | |
| 133 }; | |
| 134 | |
| 135 #if !OS(WINCE) | |
| 136 static inline void getLocalTime(const time_t* localTime, struct tm* localTM) | |
| 137 { | |
| 138 #if COMPILER(MSVC7_OR_LOWER) || COMPILER(MINGW) | |
| 139 *localTM = *localtime(localTime); | |
| 140 #elif COMPILER(MSVC) | |
| 141 localtime_s(localTM, localTime); | |
| 142 #else | |
| 143 localtime_r(localTime, localTM); | |
| 144 #endif | |
| 145 } | |
| 146 #endif | |
| 147 | |
| 148 bool isLeapYear(int year) | |
| 149 { | |
| 150 if (year % 4 != 0) | |
| 151 return false; | |
| 152 if (year % 400 == 0) | |
| 153 return true; | |
| 154 if (year % 100 == 0) | |
| 155 return false; | |
| 156 return true; | |
| 157 } | |
| 158 | |
| 159 static inline int daysInYear(int year) | |
| 160 { | |
| 161 return 365 + isLeapYear(year); | |
| 162 } | |
| 163 | |
| 164 static inline double daysFrom1970ToYear(int year) | |
| 165 { | |
| 166 // The Gregorian Calendar rules for leap years: | |
| 167 // Every fourth year is a leap year. 2004, 2008, and 2012 are leap years. | |
| 168 // However, every hundredth year is not a leap year. 1900 and 2100 are not
leap years. | |
| 169 // Every four hundred years, there's a leap year after all. 2000 and 2400 a
re leap years. | |
| 170 | |
| 171 static const int leapDaysBefore1971By4Rule = 1970 / 4; | |
| 172 static const int excludedLeapDaysBefore1971By100Rule = 1970 / 100; | |
| 173 static const int leapDaysBefore1971By400Rule = 1970 / 400; | |
| 174 | |
| 175 const double yearMinusOne = year - 1; | |
| 176 const double yearsToAddBy4Rule = floor(yearMinusOne / 4.0) - leapDaysBefore1
971By4Rule; | |
| 177 const double yearsToExcludeBy100Rule = floor(yearMinusOne / 100.0) - exclude
dLeapDaysBefore1971By100Rule; | |
| 178 const double yearsToAddBy400Rule = floor(yearMinusOne / 400.0) - leapDaysBef
ore1971By400Rule; | |
| 179 | |
| 180 return 365.0 * (year - 1970) + yearsToAddBy4Rule - yearsToExcludeBy100Rule +
yearsToAddBy400Rule; | |
| 181 } | |
| 182 | |
| 183 double msToDays(double ms) | |
| 184 { | |
| 185 return floor(ms / msPerDay); | |
| 186 } | |
| 187 | |
| 188 static String twoDigitStringFromNumber(int number) | |
| 189 { | |
| 190 ASSERT(number >= 0 && number < 100); | |
| 191 if (number > 9) | |
| 192 return String::number(number); | |
| 193 return makeString("0", String::number(number)); | |
| 194 } | |
| 195 | |
| 196 int msToYear(double ms) | |
| 197 { | |
| 198 int approxYear = static_cast<int>(floor(ms / (msPerDay * 365.2425)) + 1970); | |
| 199 double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear); | |
| 200 if (msFromApproxYearTo1970 > ms) | |
| 201 return approxYear - 1; | |
| 202 if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms) | |
| 203 return approxYear + 1; | |
| 204 return approxYear; | |
| 205 } | |
| 206 | |
| 207 int dayInYear(double ms, int year) | |
| 208 { | |
| 209 return static_cast<int>(msToDays(ms) - daysFrom1970ToYear(year)); | |
| 210 } | |
| 211 | |
| 212 static inline double msToMilliseconds(double ms) | |
| 213 { | |
| 214 double result = fmod(ms, msPerDay); | |
| 215 if (result < 0) | |
| 216 result += msPerDay; | |
| 217 return result; | |
| 218 } | |
| 219 | |
| 220 int msToMinutes(double ms) | |
| 221 { | |
| 222 double result = fmod(floor(ms / msPerMinute), minutesPerHour); | |
| 223 if (result < 0) | |
| 224 result += minutesPerHour; | |
| 225 return static_cast<int>(result); | |
| 226 } | |
| 227 | |
| 228 int msToHours(double ms) | |
| 229 { | |
| 230 double result = fmod(floor(ms/msPerHour), hoursPerDay); | |
| 231 if (result < 0) | |
| 232 result += hoursPerDay; | |
| 233 return static_cast<int>(result); | |
| 234 } | |
| 235 | |
| 236 int monthFromDayInYear(int dayInYear, bool leapYear) | |
| 237 { | |
| 238 const int d = dayInYear; | |
| 239 int step; | |
| 240 | |
| 241 if (d < (step = 31)) | |
| 242 return 0; | |
| 243 step += (leapYear ? 29 : 28); | |
| 244 if (d < step) | |
| 245 return 1; | |
| 246 if (d < (step += 31)) | |
| 247 return 2; | |
| 248 if (d < (step += 30)) | |
| 249 return 3; | |
| 250 if (d < (step += 31)) | |
| 251 return 4; | |
| 252 if (d < (step += 30)) | |
| 253 return 5; | |
| 254 if (d < (step += 31)) | |
| 255 return 6; | |
| 256 if (d < (step += 31)) | |
| 257 return 7; | |
| 258 if (d < (step += 30)) | |
| 259 return 8; | |
| 260 if (d < (step += 31)) | |
| 261 return 9; | |
| 262 if (d < (step += 30)) | |
| 263 return 10; | |
| 264 return 11; | |
| 265 } | |
| 266 | |
| 267 static inline bool checkMonth(int dayInYear, int& startDayOfThisMonth, int& star
tDayOfNextMonth, int daysInThisMonth) | |
| 268 { | |
| 269 startDayOfThisMonth = startDayOfNextMonth; | |
| 270 startDayOfNextMonth += daysInThisMonth; | |
| 271 return (dayInYear <= startDayOfNextMonth); | |
| 272 } | |
| 273 | |
| 274 int dayInMonthFromDayInYear(int dayInYear, bool leapYear) | |
| 275 { | |
| 276 const int d = dayInYear; | |
| 277 int step; | |
| 278 int next = 30; | |
| 279 | |
| 280 if (d <= next) | |
| 281 return d + 1; | |
| 282 const int daysInFeb = (leapYear ? 29 : 28); | |
| 283 if (checkMonth(d, step, next, daysInFeb)) | |
| 284 return d - step; | |
| 285 if (checkMonth(d, step, next, 31)) | |
| 286 return d - step; | |
| 287 if (checkMonth(d, step, next, 30)) | |
| 288 return d - step; | |
| 289 if (checkMonth(d, step, next, 31)) | |
| 290 return d - step; | |
| 291 if (checkMonth(d, step, next, 30)) | |
| 292 return d - step; | |
| 293 if (checkMonth(d, step, next, 31)) | |
| 294 return d - step; | |
| 295 if (checkMonth(d, step, next, 31)) | |
| 296 return d - step; | |
| 297 if (checkMonth(d, step, next, 30)) | |
| 298 return d - step; | |
| 299 if (checkMonth(d, step, next, 31)) | |
| 300 return d - step; | |
| 301 if (checkMonth(d, step, next, 30)) | |
| 302 return d - step; | |
| 303 step = next; | |
| 304 return d - step; | |
| 305 } | |
| 306 | |
| 307 int dayInYear(int year, int month, int day) | |
| 308 { | |
| 309 return firstDayOfMonth[isLeapYear(year)][month] + day - 1; | |
| 310 } | |
| 311 | |
| 312 double dateToDaysFrom1970(int year, int month, int day) | |
| 313 { | |
| 314 year += month / 12; | |
| 315 | |
| 316 month %= 12; | |
| 317 if (month < 0) { | |
| 318 month += 12; | |
| 319 --year; | |
| 320 } | |
| 321 | |
| 322 double yearday = floor(daysFrom1970ToYear(year)); | |
| 323 ASSERT((year >= 1970 && yearday >= 0) || (year < 1970 && yearday < 0)); | |
| 324 return yearday + dayInYear(year, month, day); | |
| 325 } | |
| 326 | |
| 327 // There is a hard limit at 2038 that we currently do not have a workaround | |
| 328 // for (rdar://problem/5052975). | |
| 329 static inline int maximumYearForDST() | |
| 330 { | |
| 331 return 2037; | |
| 332 } | |
| 333 | |
| 334 static inline int minimumYearForDST() | |
| 335 { | |
| 336 // Because of the 2038 issue (see maximumYearForDST) if the current year is | |
| 337 // greater than the max year minus 27 (2010), we want to use the max year | |
| 338 // minus 27 instead, to ensure there is a range of 28 years that all years | |
| 339 // can map to. | |
| 340 return std::min(msToYear(jsCurrentTime()), maximumYearForDST() - 27) ; | |
| 341 } | |
| 342 | |
| 343 /* | |
| 344 * Find an equivalent year for the one given, where equivalence is deterined by | |
| 345 * the two years having the same leapness and the first day of the year, falling | |
| 346 * on the same day of the week. | |
| 347 * | |
| 348 * This function returns a year between this current year and 2037, however this | |
| 349 * function will potentially return incorrect results if the current year is aft
er | |
| 350 * 2010, (rdar://problem/5052975), if the year passed in is before 1900 or after | |
| 351 * 2100, (rdar://problem/5055038). | |
| 352 */ | |
| 353 int equivalentYearForDST(int year) | |
| 354 { | |
| 355 // It is ok if the cached year is not the current year as long as the rules | |
| 356 // for DST did not change between the two years; if they did the app would n
eed | |
| 357 // to be restarted. | |
| 358 static int minYear = minimumYearForDST(); | |
| 359 int maxYear = maximumYearForDST(); | |
| 360 | |
| 361 int difference; | |
| 362 if (year > maxYear) | |
| 363 difference = minYear - year; | |
| 364 else if (year < minYear) | |
| 365 difference = maxYear - year; | |
| 366 else | |
| 367 return year; | |
| 368 | |
| 369 int quotient = difference / 28; | |
| 370 int product = (quotient) * 28; | |
| 371 | |
| 372 year += product; | |
| 373 ASSERT((year >= minYear && year <= maxYear) || (product - year == static_cas
t<int>(std::numeric_limits<double>::quiet_NaN()))); | |
| 374 return year; | |
| 375 } | |
| 376 | |
| 377 int32_t calculateUTCOffset() | |
| 378 { | |
| 379 #if OS(WINDOWS) | |
| 380 TIME_ZONE_INFORMATION timeZoneInformation; | |
| 381 GetTimeZoneInformation(&timeZoneInformation); | |
| 382 int32_t bias = timeZoneInformation.Bias + timeZoneInformation.StandardBias; | |
| 383 return -bias * 60 * 1000; | |
| 384 #else | |
| 385 time_t localTime = time(0); | |
| 386 tm localt; | |
| 387 getLocalTime(&localTime, &localt); | |
| 388 | |
| 389 // Get the difference between this time zone and UTC on the 1st of January o
f this year. | |
| 390 localt.tm_sec = 0; | |
| 391 localt.tm_min = 0; | |
| 392 localt.tm_hour = 0; | |
| 393 localt.tm_mday = 1; | |
| 394 localt.tm_mon = 0; | |
| 395 // Not setting localt.tm_year! | |
| 396 localt.tm_wday = 0; | |
| 397 localt.tm_yday = 0; | |
| 398 localt.tm_isdst = 0; | |
| 399 #if HAVE(TM_GMTOFF) | |
| 400 localt.tm_gmtoff = 0; | |
| 401 #endif | |
| 402 #if HAVE(TM_ZONE) | |
| 403 localt.tm_zone = 0; | |
| 404 #endif | |
| 405 | |
| 406 #if HAVE(TIMEGM) | |
| 407 time_t utcOffset = timegm(&localt) - mktime(&localt); | |
| 408 #else | |
| 409 // Using a canned date of 01/01/2009 on platforms with weaker date-handling
foo. | |
| 410 localt.tm_year = 109; | |
| 411 time_t utcOffset = 1230768000 - mktime(&localt); | |
| 412 #endif | |
| 413 | |
| 414 return static_cast<int32_t>(utcOffset * 1000); | |
| 415 #endif | |
| 416 } | |
| 417 | |
| 418 /* | |
| 419 * Get the DST offset for the time passed in. | |
| 420 */ | |
| 421 static double calculateDSTOffsetSimple(double localTimeSeconds, double utcOffset
) | |
| 422 { | |
| 423 #if OS(WINCE) | |
| 424 UNUSED_PARAM(localTimeSeconds); | |
| 425 UNUSED_PARAM(utcOffset); | |
| 426 return 0; | |
| 427 #else | |
| 428 if (localTimeSeconds > maxUnixTime) | |
| 429 localTimeSeconds = maxUnixTime; | |
| 430 else if (localTimeSeconds < 0) // Go ahead a day to make localtime work (doe
s not work with 0) | |
| 431 localTimeSeconds += secondsPerDay; | |
| 432 | |
| 433 //input is UTC so we have to shift back to local time to determine DST thus
the + getUTCOffset() | |
| 434 double offsetTime = (localTimeSeconds * msPerSecond) + utcOffset; | |
| 435 | |
| 436 // Offset from UTC but doesn't include DST obviously | |
| 437 int offsetHour = msToHours(offsetTime); | |
| 438 int offsetMinute = msToMinutes(offsetTime); | |
| 439 | |
| 440 // FIXME: time_t has a potential problem in 2038 | |
| 441 time_t localTime = static_cast<time_t>(localTimeSeconds); | |
| 442 | |
| 443 tm localTM; | |
| 444 getLocalTime(&localTime, &localTM); | |
| 445 | |
| 446 double diff = ((localTM.tm_hour - offsetHour) * secondsPerHour) + ((localTM.
tm_min - offsetMinute) * 60); | |
| 447 | |
| 448 if (diff < 0) | |
| 449 diff += secondsPerDay; | |
| 450 | |
| 451 return (diff * msPerSecond); | |
| 452 #endif | |
| 453 } | |
| 454 | |
| 455 // Get the DST offset, given a time in UTC | |
| 456 double calculateDSTOffset(double ms, double utcOffset) | |
| 457 { | |
| 458 // On Mac OS X, the call to localtime (see calculateDSTOffsetSimple) will re
turn historically accurate | |
| 459 // DST information (e.g. New Zealand did not have DST from 1946 to 1974) how
ever the JavaScript | |
| 460 // standard explicitly dictates that historical information should not be co
nsidered when | |
| 461 // determining DST. For this reason we shift away from years that localtime
can handle but would | |
| 462 // return historically accurate information. | |
| 463 int year = msToYear(ms); | |
| 464 int equivalentYear = equivalentYearForDST(year); | |
| 465 if (year != equivalentYear) { | |
| 466 bool leapYear = isLeapYear(year); | |
| 467 int dayInYearLocal = dayInYear(ms, year); | |
| 468 int dayInMonth = dayInMonthFromDayInYear(dayInYearLocal, leapYear); | |
| 469 int month = monthFromDayInYear(dayInYearLocal, leapYear); | |
| 470 double day = dateToDaysFrom1970(equivalentYear, month, dayInMonth); | |
| 471 ms = (day * msPerDay) + msToMilliseconds(ms); | |
| 472 } | |
| 473 | |
| 474 return calculateDSTOffsetSimple(ms / msPerSecond, utcOffset); | |
| 475 } | |
| 476 | |
| 477 void initializeDates() | |
| 478 { | |
| 479 #if !ASSERT_DISABLED | |
| 480 static bool alreadyInitialized; | |
| 481 ASSERT(!alreadyInitialized); | |
| 482 alreadyInitialized = true; | |
| 483 #endif | |
| 484 | |
| 485 equivalentYearForDST(2000); // Need to call once to initialize a static used
in this function. | |
| 486 } | |
| 487 | |
| 488 static inline double ymdhmsToSeconds(int year, long mon, long day, long hour, lo
ng minute, double second) | |
| 489 { | |
| 490 double days = (day - 32075) | |
| 491 + floor(1461 * (year + 4800.0 + (mon - 14) / 12) / 4) | |
| 492 + 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12 | |
| 493 - floor(3 * ((year + 4900.0 + (mon - 14) / 12) / 100) / 4) | |
| 494 - 2440588; | |
| 495 return ((days * hoursPerDay + hour) * minutesPerHour + minute) * secondsPerM
inute + second; | |
| 496 } | |
| 497 | |
| 498 // We follow the recommendation of RFC 2822 to consider all | |
| 499 // obsolete time zones not listed here equivalent to "-0000". | |
| 500 static const struct KnownZone { | |
| 501 #if !OS(WINDOWS) | |
| 502 const | |
| 503 #endif | |
| 504 char tzName[4]; | |
| 505 int tzOffset; | |
| 506 } known_zones[] = { | |
| 507 { "UT", 0 }, | |
| 508 { "GMT", 0 }, | |
| 509 { "EST", -300 }, | |
| 510 { "EDT", -240 }, | |
| 511 { "CST", -360 }, | |
| 512 { "CDT", -300 }, | |
| 513 { "MST", -420 }, | |
| 514 { "MDT", -360 }, | |
| 515 { "PST", -480 }, | |
| 516 { "PDT", -420 } | |
| 517 }; | |
| 518 | |
| 519 inline static void skipSpacesAndComments(const char*& s) | |
| 520 { | |
| 521 int nesting = 0; | |
| 522 char ch; | |
| 523 while ((ch = *s)) { | |
| 524 if (!isASCIISpace(ch)) { | |
| 525 if (ch == '(') | |
| 526 nesting++; | |
| 527 else if (ch == ')' && nesting > 0) | |
| 528 nesting--; | |
| 529 else if (nesting == 0) | |
| 530 break; | |
| 531 } | |
| 532 s++; | |
| 533 } | |
| 534 } | |
| 535 | |
| 536 // returns 0-11 (Jan-Dec); -1 on failure | |
| 537 static int findMonth(const char* monthStr) | |
| 538 { | |
| 539 ASSERT(monthStr); | |
| 540 char needle[4]; | |
| 541 for (int i = 0; i < 3; ++i) { | |
| 542 if (!*monthStr) | |
| 543 return -1; | |
| 544 needle[i] = static_cast<char>(toASCIILower(*monthStr++)); | |
| 545 } | |
| 546 needle[3] = '\0'; | |
| 547 const char *haystack = "janfebmaraprmayjunjulaugsepoctnovdec"; | |
| 548 const char *str = strstr(haystack, needle); | |
| 549 if (str) { | |
| 550 int position = static_cast<int>(str - haystack); | |
| 551 if (position % 3 == 0) | |
| 552 return position / 3; | |
| 553 } | |
| 554 return -1; | |
| 555 } | |
| 556 | |
| 557 static bool parseInt(const char* string, char** stopPosition, int base, int* res
ult) | |
| 558 { | |
| 559 long longResult = strtol(string, stopPosition, base); | |
| 560 // Avoid the use of errno as it is not available on Windows CE | |
| 561 if (string == *stopPosition || longResult <= std::numeric_limits<int>::min()
|| longResult >= std::numeric_limits<int>::max()) | |
| 562 return false; | |
| 563 *result = static_cast<int>(longResult); | |
| 564 return true; | |
| 565 } | |
| 566 | |
| 567 static bool parseLong(const char* string, char** stopPosition, int base, long* r
esult) | |
| 568 { | |
| 569 *result = strtol(string, stopPosition, base); | |
| 570 // Avoid the use of errno as it is not available on Windows CE | |
| 571 if (string == *stopPosition || *result == std::numeric_limits<long>::min() |
| *result == std::numeric_limits<long>::max()) | |
| 572 return false; | |
| 573 return true; | |
| 574 } | |
| 575 | |
| 576 // Parses a date with the format YYYY[-MM[-DD]]. | |
| 577 // Year parsing is lenient, allows any number of digits, and +/-. | |
| 578 // Returns 0 if a parse error occurs, else returns the end of the parsed portion
of the string. | |
| 579 static char* parseES5DatePortion(const char* currentPosition, int& year, long& m
onth, long& day) | |
| 580 { | |
| 581 char* postParsePosition; | |
| 582 | |
| 583 // This is a bit more lenient on the year string than ES5 specifies: | |
| 584 // instead of restricting to 4 digits (or 6 digits with mandatory +/-), | |
| 585 // it accepts any integer value. Consider this an implementation fallback. | |
| 586 if (!parseInt(currentPosition, &postParsePosition, 10, &year)) | |
| 587 return 0; | |
| 588 | |
| 589 // Check for presence of -MM portion. | |
| 590 if (*postParsePosition != '-') | |
| 591 return postParsePosition; | |
| 592 currentPosition = postParsePosition + 1; | |
| 593 | |
| 594 if (!isASCIIDigit(*currentPosition)) | |
| 595 return 0; | |
| 596 if (!parseLong(currentPosition, &postParsePosition, 10, &month)) | |
| 597 return 0; | |
| 598 if ((postParsePosition - currentPosition) != 2) | |
| 599 return 0; | |
| 600 | |
| 601 // Check for presence of -DD portion. | |
| 602 if (*postParsePosition != '-') | |
| 603 return postParsePosition; | |
| 604 currentPosition = postParsePosition + 1; | |
| 605 | |
| 606 if (!isASCIIDigit(*currentPosition)) | |
| 607 return 0; | |
| 608 if (!parseLong(currentPosition, &postParsePosition, 10, &day)) | |
| 609 return 0; | |
| 610 if ((postParsePosition - currentPosition) != 2) | |
| 611 return 0; | |
| 612 return postParsePosition; | |
| 613 } | |
| 614 | |
| 615 // Parses a time with the format HH:mm[:ss[.sss]][Z|(+|-)00:00]. | |
| 616 // Fractional seconds parsing is lenient, allows any number of digits. | |
| 617 // Returns 0 if a parse error occurs, else returns the end of the parsed portion
of the string. | |
| 618 static char* parseES5TimePortion(char* currentPosition, long& hours, long& minut
es, double& seconds, long& timeZoneSeconds) | |
| 619 { | |
| 620 char* postParsePosition; | |
| 621 if (!isASCIIDigit(*currentPosition)) | |
| 622 return 0; | |
| 623 if (!parseLong(currentPosition, &postParsePosition, 10, &hours)) | |
| 624 return 0; | |
| 625 if (*postParsePosition != ':' || (postParsePosition - currentPosition) != 2) | |
| 626 return 0; | |
| 627 currentPosition = postParsePosition + 1; | |
| 628 | |
| 629 if (!isASCIIDigit(*currentPosition)) | |
| 630 return 0; | |
| 631 if (!parseLong(currentPosition, &postParsePosition, 10, &minutes)) | |
| 632 return 0; | |
| 633 if ((postParsePosition - currentPosition) != 2) | |
| 634 return 0; | |
| 635 currentPosition = postParsePosition; | |
| 636 | |
| 637 // Seconds are optional. | |
| 638 if (*currentPosition == ':') { | |
| 639 ++currentPosition; | |
| 640 | |
| 641 long intSeconds; | |
| 642 if (!isASCIIDigit(*currentPosition)) | |
| 643 return 0; | |
| 644 if (!parseLong(currentPosition, &postParsePosition, 10, &intSeconds)) | |
| 645 return 0; | |
| 646 if ((postParsePosition - currentPosition) != 2) | |
| 647 return 0; | |
| 648 seconds = intSeconds; | |
| 649 if (*postParsePosition == '.') { | |
| 650 currentPosition = postParsePosition + 1; | |
| 651 | |
| 652 // In ECMA-262-5 it's a bit unclear if '.' can be present without mi
lliseconds, but | |
| 653 // a reasonable interpretation guided by the given examples and RFC
3339 says "no". | |
| 654 // We check the next character to avoid reading +/- timezone hours a
fter an invalid decimal. | |
| 655 if (!isASCIIDigit(*currentPosition)) | |
| 656 return 0; | |
| 657 | |
| 658 // We are more lenient than ES5 by accepting more or less than 3 fra
ction digits. | |
| 659 long fracSeconds; | |
| 660 if (!parseLong(currentPosition, &postParsePosition, 10, &fracSeconds
)) | |
| 661 return 0; | |
| 662 | |
| 663 long numFracDigits = postParsePosition - currentPosition; | |
| 664 seconds += fracSeconds * pow(10.0, static_cast<double>(-numFracDigit
s)); | |
| 665 } | |
| 666 currentPosition = postParsePosition; | |
| 667 } | |
| 668 | |
| 669 if (*currentPosition == 'Z') | |
| 670 return currentPosition + 1; | |
| 671 | |
| 672 bool tzNegative; | |
| 673 if (*currentPosition == '-') | |
| 674 tzNegative = true; | |
| 675 else if (*currentPosition == '+') | |
| 676 tzNegative = false; | |
| 677 else | |
| 678 return currentPosition; // no timezone | |
| 679 ++currentPosition; | |
| 680 | |
| 681 long tzHours; | |
| 682 long tzHoursAbs; | |
| 683 long tzMinutes; | |
| 684 | |
| 685 if (!isASCIIDigit(*currentPosition)) | |
| 686 return 0; | |
| 687 if (!parseLong(currentPosition, &postParsePosition, 10, &tzHours)) | |
| 688 return 0; | |
| 689 if (*postParsePosition != ':' || (postParsePosition - currentPosition) != 2) | |
| 690 return 0; | |
| 691 tzHoursAbs = labs(tzHours); | |
| 692 currentPosition = postParsePosition + 1; | |
| 693 | |
| 694 if (!isASCIIDigit(*currentPosition)) | |
| 695 return 0; | |
| 696 if (!parseLong(currentPosition, &postParsePosition, 10, &tzMinutes)) | |
| 697 return 0; | |
| 698 if ((postParsePosition - currentPosition) != 2) | |
| 699 return 0; | |
| 700 currentPosition = postParsePosition; | |
| 701 | |
| 702 if (tzHoursAbs > 24) | |
| 703 return 0; | |
| 704 if (tzMinutes < 0 || tzMinutes > 59) | |
| 705 return 0; | |
| 706 | |
| 707 timeZoneSeconds = 60 * (tzMinutes + (60 * tzHoursAbs)); | |
| 708 if (tzNegative) | |
| 709 timeZoneSeconds = -timeZoneSeconds; | |
| 710 | |
| 711 return currentPosition; | |
| 712 } | |
| 713 | |
| 714 double parseES5DateFromNullTerminatedCharacters(const char* dateString) | |
| 715 { | |
| 716 // This parses a date of the form defined in ECMA-262-5, section 15.9.1.15 | |
| 717 // (similar to RFC 3339 / ISO 8601: YYYY-MM-DDTHH:mm:ss[.sss]Z). | |
| 718 // In most cases it is intentionally strict (e.g. correct field widths, no s
tray whitespace). | |
| 719 | |
| 720 static const long daysPerMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 3
1, 30, 31 }; | |
| 721 | |
| 722 // The year must be present, but the other fields may be omitted - see ES5.1
15.9.1.15. | |
| 723 int year = 0; | |
| 724 long month = 1; | |
| 725 long day = 1; | |
| 726 long hours = 0; | |
| 727 long minutes = 0; | |
| 728 double seconds = 0; | |
| 729 long timeZoneSeconds = 0; | |
| 730 | |
| 731 // Parse the date YYYY[-MM[-DD]] | |
| 732 char* currentPosition = parseES5DatePortion(dateString, year, month, day); | |
| 733 if (!currentPosition) | |
| 734 return std::numeric_limits<double>::quiet_NaN(); | |
| 735 // Look for a time portion. | |
| 736 if (*currentPosition == 'T') { | |
| 737 // Parse the time HH:mm[:ss[.sss]][Z|(+|-)00:00] | |
| 738 currentPosition = parseES5TimePortion(currentPosition + 1, hours, minute
s, seconds, timeZoneSeconds); | |
| 739 if (!currentPosition) | |
| 740 return std::numeric_limits<double>::quiet_NaN(); | |
| 741 } | |
| 742 // Check that we have parsed all characters in the string. | |
| 743 if (*currentPosition) | |
| 744 return std::numeric_limits<double>::quiet_NaN(); | |
| 745 | |
| 746 // A few of these checks could be done inline above, but since many of them
are interrelated | |
| 747 // we would be sacrificing readability to "optimize" the (presumably less co
mmon) failure path. | |
| 748 if (month < 1 || month > 12) | |
| 749 return std::numeric_limits<double>::quiet_NaN(); | |
| 750 if (day < 1 || day > daysPerMonth[month - 1]) | |
| 751 return std::numeric_limits<double>::quiet_NaN(); | |
| 752 if (month == 2 && day > 28 && !isLeapYear(year)) | |
| 753 return std::numeric_limits<double>::quiet_NaN(); | |
| 754 if (hours < 0 || hours > 24) | |
| 755 return std::numeric_limits<double>::quiet_NaN(); | |
| 756 if (hours == 24 && (minutes || seconds)) | |
| 757 return std::numeric_limits<double>::quiet_NaN(); | |
| 758 if (minutes < 0 || minutes > 59) | |
| 759 return std::numeric_limits<double>::quiet_NaN(); | |
| 760 if (seconds < 0 || seconds >= 61) | |
| 761 return std::numeric_limits<double>::quiet_NaN(); | |
| 762 if (seconds > 60) { | |
| 763 // Discard leap seconds by clamping to the end of a minute. | |
| 764 seconds = 60; | |
| 765 } | |
| 766 | |
| 767 double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, secon
ds) - timeZoneSeconds; | |
| 768 return dateSeconds * msPerSecond; | |
| 769 } | |
| 770 | |
| 771 // Odd case where 'exec' is allowed to be 0, to accomodate a caller in WebCore. | |
| 772 double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveT
Z, int& offset) | |
| 773 { | |
| 774 haveTZ = false; | |
| 775 offset = 0; | |
| 776 | |
| 777 // This parses a date in the form: | |
| 778 // Tuesday, 09-Nov-99 23:12:40 GMT | |
| 779 // or | |
| 780 // Sat, 01-Jan-2000 08:00:00 GMT | |
| 781 // or | |
| 782 // Sat, 01 Jan 2000 08:00:00 GMT | |
| 783 // or | |
| 784 // 01 Jan 99 22:00 +0100 (exceptions in rfc822/rfc2822) | |
| 785 // ### non RFC formats, added for Javascript: | |
| 786 // [Wednesday] January 09 1999 23:12:40 GMT | |
| 787 // [Wednesday] January 09 23:12:40 GMT 1999 | |
| 788 // | |
| 789 // We ignore the weekday. | |
| 790 | |
| 791 // Skip leading space | |
| 792 skipSpacesAndComments(dateString); | |
| 793 | |
| 794 long month = -1; | |
| 795 const char *wordStart = dateString; | |
| 796 // Check contents of first words if not number | |
| 797 while (*dateString && !isASCIIDigit(*dateString)) { | |
| 798 if (isASCIISpace(*dateString) || *dateString == '(') { | |
| 799 if (dateString - wordStart >= 3) | |
| 800 month = findMonth(wordStart); | |
| 801 skipSpacesAndComments(dateString); | |
| 802 wordStart = dateString; | |
| 803 } else | |
| 804 dateString++; | |
| 805 } | |
| 806 | |
| 807 // Missing delimiter between month and day (like "January29")? | |
| 808 if (month == -1 && wordStart != dateString) | |
| 809 month = findMonth(wordStart); | |
| 810 | |
| 811 skipSpacesAndComments(dateString); | |
| 812 | |
| 813 if (!*dateString) | |
| 814 return std::numeric_limits<double>::quiet_NaN(); | |
| 815 | |
| 816 // ' 09-Nov-99 23:12:40 GMT' | |
| 817 char* newPosStr; | |
| 818 long day; | |
| 819 if (!parseLong(dateString, &newPosStr, 10, &day)) | |
| 820 return std::numeric_limits<double>::quiet_NaN(); | |
| 821 dateString = newPosStr; | |
| 822 | |
| 823 if (!*dateString) | |
| 824 return std::numeric_limits<double>::quiet_NaN(); | |
| 825 | |
| 826 if (day < 0) | |
| 827 return std::numeric_limits<double>::quiet_NaN(); | |
| 828 | |
| 829 int year = 0; | |
| 830 if (day > 31) { | |
| 831 // ### where is the boundary and what happens below? | |
| 832 if (*dateString != '/') | |
| 833 return std::numeric_limits<double>::quiet_NaN(); | |
| 834 // looks like a YYYY/MM/DD date | |
| 835 if (!*++dateString) | |
| 836 return std::numeric_limits<double>::quiet_NaN(); | |
| 837 if (day <= std::numeric_limits<int>::min() || day >= std::numeric_limits
<int>::max()) | |
| 838 return std::numeric_limits<double>::quiet_NaN(); | |
| 839 year = static_cast<int>(day); | |
| 840 if (!parseLong(dateString, &newPosStr, 10, &month)) | |
| 841 return std::numeric_limits<double>::quiet_NaN(); | |
| 842 month -= 1; | |
| 843 dateString = newPosStr; | |
| 844 if (*dateString++ != '/' || !*dateString) | |
| 845 return std::numeric_limits<double>::quiet_NaN(); | |
| 846 if (!parseLong(dateString, &newPosStr, 10, &day)) | |
| 847 return std::numeric_limits<double>::quiet_NaN(); | |
| 848 dateString = newPosStr; | |
| 849 } else if (*dateString == '/' && month == -1) { | |
| 850 dateString++; | |
| 851 // This looks like a MM/DD/YYYY date, not an RFC date. | |
| 852 month = day - 1; // 0-based | |
| 853 if (!parseLong(dateString, &newPosStr, 10, &day)) | |
| 854 return std::numeric_limits<double>::quiet_NaN(); | |
| 855 if (day < 1 || day > 31) | |
| 856 return std::numeric_limits<double>::quiet_NaN(); | |
| 857 dateString = newPosStr; | |
| 858 if (*dateString == '/') | |
| 859 dateString++; | |
| 860 if (!*dateString) | |
| 861 return std::numeric_limits<double>::quiet_NaN(); | |
| 862 } else { | |
| 863 if (*dateString == '-') | |
| 864 dateString++; | |
| 865 | |
| 866 skipSpacesAndComments(dateString); | |
| 867 | |
| 868 if (*dateString == ',') | |
| 869 dateString++; | |
| 870 | |
| 871 if (month == -1) { // not found yet | |
| 872 month = findMonth(dateString); | |
| 873 if (month == -1) | |
| 874 return std::numeric_limits<double>::quiet_NaN(); | |
| 875 | |
| 876 while (*dateString && *dateString != '-' && *dateString != ',' && !i
sASCIISpace(*dateString)) | |
| 877 dateString++; | |
| 878 | |
| 879 if (!*dateString) | |
| 880 return std::numeric_limits<double>::quiet_NaN(); | |
| 881 | |
| 882 // '-99 23:12:40 GMT' | |
| 883 if (*dateString != '-' && *dateString != '/' && *dateString != ',' &
& !isASCIISpace(*dateString)) | |
| 884 return std::numeric_limits<double>::quiet_NaN(); | |
| 885 dateString++; | |
| 886 } | |
| 887 } | |
| 888 | |
| 889 if (month < 0 || month > 11) | |
| 890 return std::numeric_limits<double>::quiet_NaN(); | |
| 891 | |
| 892 // '99 23:12:40 GMT' | |
| 893 if (year <= 0 && *dateString) { | |
| 894 if (!parseInt(dateString, &newPosStr, 10, &year)) | |
| 895 return std::numeric_limits<double>::quiet_NaN(); | |
| 896 } | |
| 897 | |
| 898 // Don't fail if the time is missing. | |
| 899 long hour = 0; | |
| 900 long minute = 0; | |
| 901 long second = 0; | |
| 902 if (!*newPosStr) | |
| 903 dateString = newPosStr; | |
| 904 else { | |
| 905 // ' 23:12:40 GMT' | |
| 906 if (!(isASCIISpace(*newPosStr) || *newPosStr == ',')) { | |
| 907 if (*newPosStr != ':') | |
| 908 return std::numeric_limits<double>::quiet_NaN(); | |
| 909 // There was no year; the number was the hour. | |
| 910 year = -1; | |
| 911 } else { | |
| 912 // in the normal case (we parsed the year), advance to the next numb
er | |
| 913 dateString = ++newPosStr; | |
| 914 skipSpacesAndComments(dateString); | |
| 915 } | |
| 916 | |
| 917 parseLong(dateString, &newPosStr, 10, &hour); | |
| 918 // Do not check for errno here since we want to continue | |
| 919 // even if errno was set becasue we are still looking | |
| 920 // for the timezone! | |
| 921 | |
| 922 // Read a number? If not, this might be a timezone name. | |
| 923 if (newPosStr != dateString) { | |
| 924 dateString = newPosStr; | |
| 925 | |
| 926 if (hour < 0 || hour > 23) | |
| 927 return std::numeric_limits<double>::quiet_NaN(); | |
| 928 | |
| 929 if (!*dateString) | |
| 930 return std::numeric_limits<double>::quiet_NaN(); | |
| 931 | |
| 932 // ':12:40 GMT' | |
| 933 if (*dateString++ != ':') | |
| 934 return std::numeric_limits<double>::quiet_NaN(); | |
| 935 | |
| 936 if (!parseLong(dateString, &newPosStr, 10, &minute)) | |
| 937 return std::numeric_limits<double>::quiet_NaN(); | |
| 938 dateString = newPosStr; | |
| 939 | |
| 940 if (minute < 0 || minute > 59) | |
| 941 return std::numeric_limits<double>::quiet_NaN(); | |
| 942 | |
| 943 // ':40 GMT' | |
| 944 if (*dateString && *dateString != ':' && !isASCIISpace(*dateString)) | |
| 945 return std::numeric_limits<double>::quiet_NaN(); | |
| 946 | |
| 947 // seconds are optional in rfc822 + rfc2822 | |
| 948 if (*dateString ==':') { | |
| 949 dateString++; | |
| 950 | |
| 951 if (!parseLong(dateString, &newPosStr, 10, &second)) | |
| 952 return std::numeric_limits<double>::quiet_NaN(); | |
| 953 dateString = newPosStr; | |
| 954 | |
| 955 if (second < 0 || second > 59) | |
| 956 return std::numeric_limits<double>::quiet_NaN(); | |
| 957 } | |
| 958 | |
| 959 skipSpacesAndComments(dateString); | |
| 960 | |
| 961 if (strncasecmp(dateString, "AM", 2) == 0) { | |
| 962 if (hour > 12) | |
| 963 return std::numeric_limits<double>::quiet_NaN(); | |
| 964 if (hour == 12) | |
| 965 hour = 0; | |
| 966 dateString += 2; | |
| 967 skipSpacesAndComments(dateString); | |
| 968 } else if (strncasecmp(dateString, "PM", 2) == 0) { | |
| 969 if (hour > 12) | |
| 970 return std::numeric_limits<double>::quiet_NaN(); | |
| 971 if (hour != 12) | |
| 972 hour += 12; | |
| 973 dateString += 2; | |
| 974 skipSpacesAndComments(dateString); | |
| 975 } | |
| 976 } | |
| 977 } | |
| 978 | |
| 979 // The year may be after the time but before the time zone. | |
| 980 if (isASCIIDigit(*dateString) && year == -1) { | |
| 981 if (!parseInt(dateString, &newPosStr, 10, &year)) | |
| 982 return std::numeric_limits<double>::quiet_NaN(); | |
| 983 dateString = newPosStr; | |
| 984 skipSpacesAndComments(dateString); | |
| 985 } | |
| 986 | |
| 987 // Don't fail if the time zone is missing. | |
| 988 // Some websites omit the time zone (4275206). | |
| 989 if (*dateString) { | |
| 990 if (strncasecmp(dateString, "GMT", 3) == 0 || strncasecmp(dateString, "U
TC", 3) == 0) { | |
| 991 dateString += 3; | |
| 992 haveTZ = true; | |
| 993 } | |
| 994 | |
| 995 if (*dateString == '+' || *dateString == '-') { | |
| 996 int o; | |
| 997 if (!parseInt(dateString, &newPosStr, 10, &o)) | |
| 998 return std::numeric_limits<double>::quiet_NaN(); | |
| 999 dateString = newPosStr; | |
| 1000 | |
| 1001 if (o < -9959 || o > 9959) | |
| 1002 return std::numeric_limits<double>::quiet_NaN(); | |
| 1003 | |
| 1004 int sgn = (o < 0) ? -1 : 1; | |
| 1005 o = abs(o); | |
| 1006 if (*dateString != ':') { | |
| 1007 if (o >= 24) | |
| 1008 offset = ((o / 100) * 60 + (o % 100)) * sgn; | |
| 1009 else | |
| 1010 offset = o * 60 * sgn; | |
| 1011 } else { // GMT+05:00 | |
| 1012 ++dateString; // skip the ':' | |
| 1013 int o2; | |
| 1014 if (!parseInt(dateString, &newPosStr, 10, &o2)) | |
| 1015 return std::numeric_limits<double>::quiet_NaN(); | |
| 1016 dateString = newPosStr; | |
| 1017 offset = (o * 60 + o2) * sgn; | |
| 1018 } | |
| 1019 haveTZ = true; | |
| 1020 } else { | |
| 1021 for (size_t i = 0; i < WTF_ARRAY_LENGTH(known_zones); ++i) { | |
| 1022 if (0 == strncasecmp(dateString, known_zones[i].tzName, strlen(k
nown_zones[i].tzName))) { | |
| 1023 offset = known_zones[i].tzOffset; | |
| 1024 dateString += strlen(known_zones[i].tzName); | |
| 1025 haveTZ = true; | |
| 1026 break; | |
| 1027 } | |
| 1028 } | |
| 1029 } | |
| 1030 } | |
| 1031 | |
| 1032 skipSpacesAndComments(dateString); | |
| 1033 | |
| 1034 if (*dateString && year == -1) { | |
| 1035 if (!parseInt(dateString, &newPosStr, 10, &year)) | |
| 1036 return std::numeric_limits<double>::quiet_NaN(); | |
| 1037 dateString = newPosStr; | |
| 1038 skipSpacesAndComments(dateString); | |
| 1039 } | |
| 1040 | |
| 1041 // Trailing garbage | |
| 1042 if (*dateString) | |
| 1043 return std::numeric_limits<double>::quiet_NaN(); | |
| 1044 | |
| 1045 // Y2K: Handle 2 digit years. | |
| 1046 if (year >= 0 && year < 100) { | |
| 1047 if (year < 50) | |
| 1048 year += 2000; | |
| 1049 else | |
| 1050 year += 1900; | |
| 1051 } | |
| 1052 | |
| 1053 return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSe
cond; | |
| 1054 } | |
| 1055 | |
| 1056 double parseDateFromNullTerminatedCharacters(const char* dateString) | |
| 1057 { | |
| 1058 bool haveTZ; | |
| 1059 int offset; | |
| 1060 double ms = parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset
); | |
| 1061 if (std::isnan(ms)) | |
| 1062 return std::numeric_limits<double>::quiet_NaN(); | |
| 1063 | |
| 1064 // fall back to local timezone | |
| 1065 if (!haveTZ) { | |
| 1066 double utcOffset = calculateUTCOffset(); | |
| 1067 double dstOffset = calculateDSTOffset(ms, utcOffset); | |
| 1068 offset = (utcOffset + dstOffset) / msPerMinute; | |
| 1069 } | |
| 1070 return ms - (offset * msPerMinute); | |
| 1071 } | |
| 1072 | |
| 1073 double timeClip(double t) | |
| 1074 { | |
| 1075 if (!std::isfinite(t)) | |
| 1076 return std::numeric_limits<double>::quiet_NaN(); | |
| 1077 if (fabs(t) > maxECMAScriptTime) | |
| 1078 return std::numeric_limits<double>::quiet_NaN(); | |
| 1079 return trunc(t); | |
| 1080 } | |
| 1081 | |
| 1082 // See http://tools.ietf.org/html/rfc2822#section-3.3 for more information. | |
| 1083 String makeRFC2822DateString(unsigned dayOfWeek, unsigned day, unsigned month, u
nsigned year, unsigned hours, unsigned minutes, unsigned seconds, int utcOffset) | |
| 1084 { | |
| 1085 StringBuilder stringBuilder; | |
| 1086 stringBuilder.append(weekdayName[dayOfWeek]); | |
| 1087 stringBuilder.appendLiteral(", "); | |
| 1088 stringBuilder.appendNumber(day); | |
| 1089 stringBuilder.append(' '); | |
| 1090 stringBuilder.append(monthName[month]); | |
| 1091 stringBuilder.append(' '); | |
| 1092 stringBuilder.appendNumber(year); | |
| 1093 stringBuilder.append(' '); | |
| 1094 | |
| 1095 stringBuilder.append(twoDigitStringFromNumber(hours)); | |
| 1096 stringBuilder.append(':'); | |
| 1097 stringBuilder.append(twoDigitStringFromNumber(minutes)); | |
| 1098 stringBuilder.append(':'); | |
| 1099 stringBuilder.append(twoDigitStringFromNumber(seconds)); | |
| 1100 stringBuilder.append(' '); | |
| 1101 | |
| 1102 stringBuilder.append(utcOffset > 0 ? '+' : '-'); | |
| 1103 int absoluteUTCOffset = abs(utcOffset); | |
| 1104 stringBuilder.append(twoDigitStringFromNumber(absoluteUTCOffset / 60)); | |
| 1105 stringBuilder.append(twoDigitStringFromNumber(absoluteUTCOffset % 60)); | |
| 1106 | |
| 1107 return stringBuilder.toString(); | |
| 1108 } | |
| 1109 | |
| 1110 } // namespace WTF | |
| OLD | NEW |