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