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