]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/DateMath.cpp
JavaScriptCore-525.tar.gz
[apple/javascriptcore.git] / runtime / DateMath.cpp
1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
4 *
5 * The Original Code is Mozilla Communicator client code, released
6 * March 31, 1998.
7 *
8 * The Initial Developer of the Original Code is
9 * Netscape Communications Corporation.
10 * Portions created by the Initial Developer are Copyright (C) 1998
11 * the Initial Developer. All Rights Reserved.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
22 *
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 *
27 * Alternatively, the contents of this file may be used under the terms
28 * of either the Mozilla Public License Version 1.1, found at
29 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
30 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
31 * (the "GPL"), in which case the provisions of the MPL or the GPL are
32 * applicable instead of those above. If you wish to allow use of your
33 * version of this file only under the terms of one of those two
34 * licenses (the MPL or the GPL) and not to allow others to use your
35 * version of this file under the LGPL, indicate your decision by
36 * deletingthe provisions above and replace them with the notice and
37 * other provisions required by the MPL or the GPL, as the case may be.
38 * If you do not delete the provisions above, a recipient may use your
39 * version of this file under any of the LGPL, the MPL or the GPL.
40 */
41
42 #include "config.h"
43 #include "DateMath.h"
44
45 #include "JSNumberCell.h"
46 #include <math.h>
47 #include <stdint.h>
48 #include <time.h>
49 #include <wtf/ASCIICType.h>
50 #include <wtf/Assertions.h>
51 #include <wtf/CurrentTime.h>
52 #include <wtf/MathExtras.h>
53 #include <wtf/StringExtras.h>
54
55 #if HAVE(ERRNO_H)
56 #include <errno.h>
57 #endif
58
59 #if PLATFORM(DARWIN)
60 #include <notify.h>
61 #endif
62
63 #if HAVE(SYS_TIME_H)
64 #include <sys/time.h>
65 #endif
66
67 #if HAVE(SYS_TIMEB_H)
68 #include <sys/timeb.h>
69 #endif
70
71 #if HAVE(STRINGS_H)
72 #include <strings.h>
73 #endif
74
75 using namespace WTF;
76
77 namespace JSC {
78
79 /* Constants */
80
81 static const double minutesPerDay = 24.0 * 60.0;
82 static const double secondsPerDay = 24.0 * 60.0 * 60.0;
83 static const double secondsPerYear = 24.0 * 60.0 * 60.0 * 365.0;
84
85 static const double usecPerSec = 1000000.0;
86
87 static const double maxUnixTime = 2145859200.0; // 12/31/2037
88
89 // Day of year for the first day of each month, where index 0 is January, and day 0 is January 1.
90 // First for non-leap years, then for leap years.
91 static const int firstDayOfMonth[2][12] = {
92 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
93 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
94 };
95
96 static inline bool isLeapYear(int year)
97 {
98 if (year % 4 != 0)
99 return false;
100 if (year % 400 == 0)
101 return true;
102 if (year % 100 == 0)
103 return false;
104 return true;
105 }
106
107 static inline int daysInYear(int year)
108 {
109 return 365 + isLeapYear(year);
110 }
111
112 static inline double daysFrom1970ToYear(int year)
113 {
114 // The Gregorian Calendar rules for leap years:
115 // Every fourth year is a leap year. 2004, 2008, and 2012 are leap years.
116 // However, every hundredth year is not a leap year. 1900 and 2100 are not leap years.
117 // Every four hundred years, there's a leap year after all. 2000 and 2400 are leap years.
118
119 static const int leapDaysBefore1971By4Rule = 1970 / 4;
120 static const int excludedLeapDaysBefore1971By100Rule = 1970 / 100;
121 static const int leapDaysBefore1971By400Rule = 1970 / 400;
122
123 const double yearMinusOne = year - 1;
124 const double yearsToAddBy4Rule = floor(yearMinusOne / 4.0) - leapDaysBefore1971By4Rule;
125 const double yearsToExcludeBy100Rule = floor(yearMinusOne / 100.0) - excludedLeapDaysBefore1971By100Rule;
126 const double yearsToAddBy400Rule = floor(yearMinusOne / 400.0) - leapDaysBefore1971By400Rule;
127
128 return 365.0 * (year - 1970) + yearsToAddBy4Rule - yearsToExcludeBy100Rule + yearsToAddBy400Rule;
129 }
130
131 static inline double msToDays(double ms)
132 {
133 return floor(ms / msPerDay);
134 }
135
136 static inline int msToYear(double ms)
137 {
138 int approxYear = static_cast<int>(floor(ms / (msPerDay * 365.2425)) + 1970);
139 double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear);
140 if (msFromApproxYearTo1970 > ms)
141 return approxYear - 1;
142 if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms)
143 return approxYear + 1;
144 return approxYear;
145 }
146
147 static inline int dayInYear(double ms, int year)
148 {
149 return static_cast<int>(msToDays(ms) - daysFrom1970ToYear(year));
150 }
151
152 static inline double msToMilliseconds(double ms)
153 {
154 double result = fmod(ms, msPerDay);
155 if (result < 0)
156 result += msPerDay;
157 return result;
158 }
159
160 // 0: Sunday, 1: Monday, etc.
161 static inline int msToWeekDay(double ms)
162 {
163 int wd = (static_cast<int>(msToDays(ms)) + 4) % 7;
164 if (wd < 0)
165 wd += 7;
166 return wd;
167 }
168
169 static inline int msToSeconds(double ms)
170 {
171 double result = fmod(floor(ms / msPerSecond), secondsPerMinute);
172 if (result < 0)
173 result += secondsPerMinute;
174 return static_cast<int>(result);
175 }
176
177 static inline int msToMinutes(double ms)
178 {
179 double result = fmod(floor(ms / msPerMinute), minutesPerHour);
180 if (result < 0)
181 result += minutesPerHour;
182 return static_cast<int>(result);
183 }
184
185 static inline int msToHours(double ms)
186 {
187 double result = fmod(floor(ms/msPerHour), hoursPerDay);
188 if (result < 0)
189 result += hoursPerDay;
190 return static_cast<int>(result);
191 }
192
193 static inline int monthFromDayInYear(int dayInYear, bool leapYear)
194 {
195 const int d = dayInYear;
196 int step;
197
198 if (d < (step = 31))
199 return 0;
200 step += (leapYear ? 29 : 28);
201 if (d < step)
202 return 1;
203 if (d < (step += 31))
204 return 2;
205 if (d < (step += 30))
206 return 3;
207 if (d < (step += 31))
208 return 4;
209 if (d < (step += 30))
210 return 5;
211 if (d < (step += 31))
212 return 6;
213 if (d < (step += 31))
214 return 7;
215 if (d < (step += 30))
216 return 8;
217 if (d < (step += 31))
218 return 9;
219 if (d < (step += 30))
220 return 10;
221 return 11;
222 }
223
224 static inline bool checkMonth(int dayInYear, int& startDayOfThisMonth, int& startDayOfNextMonth, int daysInThisMonth)
225 {
226 startDayOfThisMonth = startDayOfNextMonth;
227 startDayOfNextMonth += daysInThisMonth;
228 return (dayInYear <= startDayOfNextMonth);
229 }
230
231 static inline int dayInMonthFromDayInYear(int dayInYear, bool leapYear)
232 {
233 const int d = dayInYear;
234 int step;
235 int next = 30;
236
237 if (d <= next)
238 return d + 1;
239 const int daysInFeb = (leapYear ? 29 : 28);
240 if (checkMonth(d, step, next, daysInFeb))
241 return d - step;
242 if (checkMonth(d, step, next, 31))
243 return d - step;
244 if (checkMonth(d, step, next, 30))
245 return d - step;
246 if (checkMonth(d, step, next, 31))
247 return d - step;
248 if (checkMonth(d, step, next, 30))
249 return d - step;
250 if (checkMonth(d, step, next, 31))
251 return d - step;
252 if (checkMonth(d, step, next, 31))
253 return d - step;
254 if (checkMonth(d, step, next, 30))
255 return d - step;
256 if (checkMonth(d, step, next, 31))
257 return d - step;
258 if (checkMonth(d, step, next, 30))
259 return d - step;
260 step = next;
261 return d - step;
262 }
263
264 static inline int monthToDayInYear(int month, bool isLeapYear)
265 {
266 return firstDayOfMonth[isLeapYear][month];
267 }
268
269 static inline double timeToMS(double hour, double min, double sec, double ms)
270 {
271 return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms);
272 }
273
274 static int dateToDayInYear(int year, int month, int day)
275 {
276 year += month / 12;
277
278 month %= 12;
279 if (month < 0) {
280 month += 12;
281 --year;
282 }
283
284 int yearday = static_cast<int>(floor(daysFrom1970ToYear(year)));
285 int monthday = monthToDayInYear(month, isLeapYear(year));
286
287 return yearday + monthday + day - 1;
288 }
289
290 double getCurrentUTCTime()
291 {
292 return floor(getCurrentUTCTimeWithMicroseconds());
293 }
294
295 // Returns current time in milliseconds since 1 Jan 1970.
296 double getCurrentUTCTimeWithMicroseconds()
297 {
298 return currentTime() * 1000.0;
299 }
300
301 void getLocalTime(const time_t* localTime, struct tm* localTM)
302 {
303 #if COMPILER(MSVC7) || COMPILER(MINGW) || PLATFORM(WIN_CE)
304 *localTM = *localtime(localTime);
305 #elif COMPILER(MSVC)
306 localtime_s(localTM, localTime);
307 #else
308 localtime_r(localTime, localTM);
309 #endif
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(getCurrentUTCTime()), 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>(NaN)));
359 return year;
360 }
361
362 static int32_t calculateUTCOffset()
363 {
364 tm localt;
365 memset(&localt, 0, sizeof(localt));
366
367 // get the difference between this time zone and UTC on Jan 01, 2000 12:00:00 AM
368 localt.tm_mday = 1;
369 localt.tm_year = 100;
370 time_t utcOffset = 946684800 - mktime(&localt);
371
372 return static_cast<int32_t>(utcOffset * 1000);
373 }
374
375 #if PLATFORM(DARWIN)
376 static int32_t s_cachedUTCOffset; // In milliseconds. An assumption here is that access to an int32_t variable is atomic on platforms that take this code path.
377 static bool s_haveCachedUTCOffset;
378 static int s_notificationToken;
379 #endif
380
381 /*
382 * Get the difference in milliseconds between this time zone and UTC (GMT)
383 * NOT including DST.
384 */
385 double getUTCOffset()
386 {
387 #if PLATFORM(DARWIN)
388 if (s_haveCachedUTCOffset) {
389 int notified;
390 uint32_t status = notify_check(s_notificationToken, &notified);
391 if (status == NOTIFY_STATUS_OK && !notified)
392 return s_cachedUTCOffset;
393 }
394 #endif
395
396 int32_t utcOffset = calculateUTCOffset();
397
398 #if PLATFORM(DARWIN)
399 // Theoretically, it is possible that several threads will be executing this code at once, in which case we will have a race condition,
400 // and a newer value may be overwritten. In practice, time zones don't change that often.
401 s_cachedUTCOffset = utcOffset;
402 #endif
403
404 return utcOffset;
405 }
406
407 /*
408 * Get the DST offset for the time passed in. Takes
409 * seconds (not milliseconds) and cannot handle dates before 1970
410 * on some OS'
411 */
412 static double getDSTOffsetSimple(double localTimeSeconds, double utcOffset)
413 {
414 if (localTimeSeconds > maxUnixTime)
415 localTimeSeconds = maxUnixTime;
416 else if (localTimeSeconds < 0) // Go ahead a day to make localtime work (does not work with 0)
417 localTimeSeconds += secondsPerDay;
418
419 //input is UTC so we have to shift back to local time to determine DST thus the + getUTCOffset()
420 double offsetTime = (localTimeSeconds * msPerSecond) + utcOffset;
421
422 // Offset from UTC but doesn't include DST obviously
423 int offsetHour = msToHours(offsetTime);
424 int offsetMinute = msToMinutes(offsetTime);
425
426 // FIXME: time_t has a potential problem in 2038
427 time_t localTime = static_cast<time_t>(localTimeSeconds);
428
429 tm localTM;
430 getLocalTime(&localTime, &localTM);
431
432 double diff = ((localTM.tm_hour - offsetHour) * secondsPerHour) + ((localTM.tm_min - offsetMinute) * 60);
433
434 if (diff < 0)
435 diff += secondsPerDay;
436
437 return (diff * msPerSecond);
438 }
439
440 // Get the DST offset, given a time in UTC
441 static double getDSTOffset(double ms, double utcOffset)
442 {
443 // On Mac OS X, the call to localtime (see getDSTOffsetSimple) will return historically accurate
444 // DST information (e.g. New Zealand did not have DST from 1946 to 1974) however the JavaScript
445 // standard explicitly dictates that historical information should not be considered when
446 // determining DST. For this reason we shift away from years that localtime can handle but would
447 // return historically accurate information.
448 int year = msToYear(ms);
449 int equivalentYear = equivalentYearForDST(year);
450 if (year != equivalentYear) {
451 bool leapYear = isLeapYear(year);
452 int dayInYearLocal = dayInYear(ms, year);
453 int dayInMonth = dayInMonthFromDayInYear(dayInYearLocal, leapYear);
454 int month = monthFromDayInYear(dayInYearLocal, leapYear);
455 int day = dateToDayInYear(equivalentYear, month, dayInMonth);
456 ms = (day * msPerDay) + msToMilliseconds(ms);
457 }
458
459 return getDSTOffsetSimple(ms / msPerSecond, utcOffset);
460 }
461
462 double gregorianDateTimeToMS(const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
463 {
464 int day = dateToDayInYear(t.year + 1900, t.month, t.monthDay);
465 double ms = timeToMS(t.hour, t.minute, t.second, milliSeconds);
466 double result = (day * msPerDay) + ms;
467
468 if (!inputIsUTC) { // convert to UTC
469 double utcOffset = getUTCOffset();
470 result -= utcOffset;
471 result -= getDSTOffset(result, utcOffset);
472 }
473
474 return result;
475 }
476
477 void msToGregorianDateTime(double ms, bool outputIsUTC, GregorianDateTime& tm)
478 {
479 // input is UTC
480 double dstOff = 0.0;
481 const double utcOff = getUTCOffset();
482
483 if (!outputIsUTC) { // convert to local time
484 dstOff = getDSTOffset(ms, utcOff);
485 ms += dstOff + utcOff;
486 }
487
488 const int year = msToYear(ms);
489 tm.second = msToSeconds(ms);
490 tm.minute = msToMinutes(ms);
491 tm.hour = msToHours(ms);
492 tm.weekDay = msToWeekDay(ms);
493 tm.yearDay = dayInYear(ms, year);
494 tm.monthDay = dayInMonthFromDayInYear(tm.yearDay, isLeapYear(year));
495 tm.month = monthFromDayInYear(tm.yearDay, isLeapYear(year));
496 tm.year = year - 1900;
497 tm.isDST = dstOff != 0.0;
498
499 tm.utcOffset = static_cast<long>((dstOff + utcOff) / msPerSecond);
500 tm.timeZone = NULL;
501 }
502
503 void initDateMath()
504 {
505 #ifndef NDEBUG
506 static bool alreadyInitialized;
507 ASSERT(!alreadyInitialized++);
508 #endif
509
510 equivalentYearForDST(2000); // Need to call once to initialize a static used in this function.
511 #if PLATFORM(DARWIN)
512 // Register for a notification whenever the time zone changes.
513 uint32_t status = notify_register_check("com.apple.system.timezone", &s_notificationToken);
514 if (status == NOTIFY_STATUS_OK) {
515 s_cachedUTCOffset = calculateUTCOffset();
516 s_haveCachedUTCOffset = true;
517 }
518 #endif
519 }
520
521 static inline double ymdhmsToSeconds(long year, int mon, int day, int hour, int minute, int second)
522 {
523 double days = (day - 32075)
524 + floor(1461 * (year + 4800.0 + (mon - 14) / 12) / 4)
525 + 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12
526 - floor(3 * ((year + 4900.0 + (mon - 14) / 12) / 100) / 4)
527 - 2440588;
528 return ((days * hoursPerDay + hour) * minutesPerHour + minute) * secondsPerMinute + second;
529 }
530
531 // We follow the recommendation of RFC 2822 to consider all
532 // obsolete time zones not listed here equivalent to "-0000".
533 static const struct KnownZone {
534 #if !PLATFORM(WIN_OS)
535 const
536 #endif
537 char tzName[4];
538 int tzOffset;
539 } known_zones[] = {
540 { "UT", 0 },
541 { "GMT", 0 },
542 { "EST", -300 },
543 { "EDT", -240 },
544 { "CST", -360 },
545 { "CDT", -300 },
546 { "MST", -420 },
547 { "MDT", -360 },
548 { "PST", -480 },
549 { "PDT", -420 }
550 };
551
552 inline static void skipSpacesAndComments(const char*& s)
553 {
554 int nesting = 0;
555 char ch;
556 while ((ch = *s)) {
557 if (!isASCIISpace(ch)) {
558 if (ch == '(')
559 nesting++;
560 else if (ch == ')' && nesting > 0)
561 nesting--;
562 else if (nesting == 0)
563 break;
564 }
565 s++;
566 }
567 }
568
569 // returns 0-11 (Jan-Dec); -1 on failure
570 static int findMonth(const char* monthStr)
571 {
572 ASSERT(monthStr);
573 char needle[4];
574 for (int i = 0; i < 3; ++i) {
575 if (!*monthStr)
576 return -1;
577 needle[i] = static_cast<char>(toASCIILower(*monthStr++));
578 }
579 needle[3] = '\0';
580 const char *haystack = "janfebmaraprmayjunjulaugsepoctnovdec";
581 const char *str = strstr(haystack, needle);
582 if (str) {
583 int position = static_cast<int>(str - haystack);
584 if (position % 3 == 0)
585 return position / 3;
586 }
587 return -1;
588 }
589
590 static bool parseLong(const char* string, char** stopPosition, int base, long* result)
591 {
592 *result = strtol(string, stopPosition, base);
593 // Avoid the use of errno as it is not available on Windows CE
594 if (string == *stopPosition || *result == LONG_MIN || *result == LONG_MAX)
595 return false;
596 return true;
597 }
598
599 double parseDate(const UString &date)
600 {
601 // This parses a date in the form:
602 // Tuesday, 09-Nov-99 23:12:40 GMT
603 // or
604 // Sat, 01-Jan-2000 08:00:00 GMT
605 // or
606 // Sat, 01 Jan 2000 08:00:00 GMT
607 // or
608 // 01 Jan 99 22:00 +0100 (exceptions in rfc822/rfc2822)
609 // ### non RFC formats, added for Javascript:
610 // [Wednesday] January 09 1999 23:12:40 GMT
611 // [Wednesday] January 09 23:12:40 GMT 1999
612 //
613 // We ignore the weekday.
614
615 CString dateCString = date.UTF8String();
616 const char *dateString = dateCString.c_str();
617
618 // Skip leading space
619 skipSpacesAndComments(dateString);
620
621 long month = -1;
622 const char *wordStart = dateString;
623 // Check contents of first words if not number
624 while (*dateString && !isASCIIDigit(*dateString)) {
625 if (isASCIISpace(*dateString) || *dateString == '(') {
626 if (dateString - wordStart >= 3)
627 month = findMonth(wordStart);
628 skipSpacesAndComments(dateString);
629 wordStart = dateString;
630 } else
631 dateString++;
632 }
633
634 // Missing delimiter between month and day (like "January29")?
635 if (month == -1 && wordStart != dateString)
636 month = findMonth(wordStart);
637
638 skipSpacesAndComments(dateString);
639
640 if (!*dateString)
641 return NaN;
642
643 // ' 09-Nov-99 23:12:40 GMT'
644 char* newPosStr;
645 long day;
646 if (!parseLong(dateString, &newPosStr, 10, &day))
647 return NaN;
648 dateString = newPosStr;
649
650 if (!*dateString)
651 return NaN;
652
653 if (day < 0)
654 return NaN;
655
656 long year = 0;
657 if (day > 31) {
658 // ### where is the boundary and what happens below?
659 if (*dateString != '/')
660 return NaN;
661 // looks like a YYYY/MM/DD date
662 if (!*++dateString)
663 return NaN;
664 year = day;
665 if (!parseLong(dateString, &newPosStr, 10, &month))
666 return NaN;
667 month -= 1;
668 dateString = newPosStr;
669 if (*dateString++ != '/' || !*dateString)
670 return NaN;
671 if (!parseLong(dateString, &newPosStr, 10, &day))
672 return NaN;
673 dateString = newPosStr;
674 } else if (*dateString == '/' && month == -1) {
675 dateString++;
676 // This looks like a MM/DD/YYYY date, not an RFC date.
677 month = day - 1; // 0-based
678 if (!parseLong(dateString, &newPosStr, 10, &day))
679 return NaN;
680 if (day < 1 || day > 31)
681 return NaN;
682 dateString = newPosStr;
683 if (*dateString == '/')
684 dateString++;
685 if (!*dateString)
686 return NaN;
687 } else {
688 if (*dateString == '-')
689 dateString++;
690
691 skipSpacesAndComments(dateString);
692
693 if (*dateString == ',')
694 dateString++;
695
696 if (month == -1) { // not found yet
697 month = findMonth(dateString);
698 if (month == -1)
699 return NaN;
700
701 while (*dateString && *dateString != '-' && *dateString != ',' && !isASCIISpace(*dateString))
702 dateString++;
703
704 if (!*dateString)
705 return NaN;
706
707 // '-99 23:12:40 GMT'
708 if (*dateString != '-' && *dateString != '/' && *dateString != ',' && !isASCIISpace(*dateString))
709 return NaN;
710 dateString++;
711 }
712 }
713
714 if (month < 0 || month > 11)
715 return NaN;
716
717 // '99 23:12:40 GMT'
718 if (year <= 0 && *dateString) {
719 if (!parseLong(dateString, &newPosStr, 10, &year))
720 return NaN;
721 }
722
723 // Don't fail if the time is missing.
724 long hour = 0;
725 long minute = 0;
726 long second = 0;
727 if (!*newPosStr)
728 dateString = newPosStr;
729 else {
730 // ' 23:12:40 GMT'
731 if (!(isASCIISpace(*newPosStr) || *newPosStr == ',')) {
732 if (*newPosStr != ':')
733 return NaN;
734 // There was no year; the number was the hour.
735 year = -1;
736 } else {
737 // in the normal case (we parsed the year), advance to the next number
738 dateString = ++newPosStr;
739 skipSpacesAndComments(dateString);
740 }
741
742 parseLong(dateString, &newPosStr, 10, &hour);
743 // Do not check for errno here since we want to continue
744 // even if errno was set becasue we are still looking
745 // for the timezone!
746
747 // Read a number? If not, this might be a timezone name.
748 if (newPosStr != dateString) {
749 dateString = newPosStr;
750
751 if (hour < 0 || hour > 23)
752 return NaN;
753
754 if (!*dateString)
755 return NaN;
756
757 // ':12:40 GMT'
758 if (*dateString++ != ':')
759 return NaN;
760
761 if (!parseLong(dateString, &newPosStr, 10, &minute))
762 return NaN;
763 dateString = newPosStr;
764
765 if (minute < 0 || minute > 59)
766 return NaN;
767
768 // ':40 GMT'
769 if (*dateString && *dateString != ':' && !isASCIISpace(*dateString))
770 return NaN;
771
772 // seconds are optional in rfc822 + rfc2822
773 if (*dateString ==':') {
774 dateString++;
775
776 if (!parseLong(dateString, &newPosStr, 10, &second))
777 return NaN;
778 dateString = newPosStr;
779
780 if (second < 0 || second > 59)
781 return NaN;
782 }
783
784 skipSpacesAndComments(dateString);
785
786 if (strncasecmp(dateString, "AM", 2) == 0) {
787 if (hour > 12)
788 return NaN;
789 if (hour == 12)
790 hour = 0;
791 dateString += 2;
792 skipSpacesAndComments(dateString);
793 } else if (strncasecmp(dateString, "PM", 2) == 0) {
794 if (hour > 12)
795 return NaN;
796 if (hour != 12)
797 hour += 12;
798 dateString += 2;
799 skipSpacesAndComments(dateString);
800 }
801 }
802 }
803
804 bool haveTZ = false;
805 int offset = 0;
806
807 // Don't fail if the time zone is missing.
808 // Some websites omit the time zone (4275206).
809 if (*dateString) {
810 if (strncasecmp(dateString, "GMT", 3) == 0 || strncasecmp(dateString, "UTC", 3) == 0) {
811 dateString += 3;
812 haveTZ = true;
813 }
814
815 if (*dateString == '+' || *dateString == '-') {
816 long o;
817 if (!parseLong(dateString, &newPosStr, 10, &o))
818 return NaN;
819 dateString = newPosStr;
820
821 if (o < -9959 || o > 9959)
822 return NaN;
823
824 int sgn = (o < 0) ? -1 : 1;
825 o = abs(o);
826 if (*dateString != ':') {
827 offset = ((o / 100) * 60 + (o % 100)) * sgn;
828 } else { // GMT+05:00
829 long o2;
830 if (!parseLong(dateString, &newPosStr, 10, &o2))
831 return NaN;
832 dateString = newPosStr;
833 offset = (o * 60 + o2) * sgn;
834 }
835 haveTZ = true;
836 } else {
837 for (int i = 0; i < int(sizeof(known_zones) / sizeof(KnownZone)); i++) {
838 if (0 == strncasecmp(dateString, known_zones[i].tzName, strlen(known_zones[i].tzName))) {
839 offset = known_zones[i].tzOffset;
840 dateString += strlen(known_zones[i].tzName);
841 haveTZ = true;
842 break;
843 }
844 }
845 }
846 }
847
848 skipSpacesAndComments(dateString);
849
850 if (*dateString && year == -1) {
851 if (!parseLong(dateString, &newPosStr, 10, &year))
852 return NaN;
853 dateString = newPosStr;
854 }
855
856 skipSpacesAndComments(dateString);
857
858 // Trailing garbage
859 if (*dateString)
860 return NaN;
861
862 // Y2K: Handle 2 digit years.
863 if (year >= 0 && year < 100) {
864 if (year < 50)
865 year += 2000;
866 else
867 year += 1900;
868 }
869
870 // fall back to local timezone
871 if (!haveTZ) {
872 GregorianDateTime t;
873 t.monthDay = day;
874 t.month = month;
875 t.year = year - 1900;
876 t.isDST = -1;
877 t.second = second;
878 t.minute = minute;
879 t.hour = hour;
880
881 // Use our gregorianDateTimeToMS() rather than mktime() as the latter can't handle the full year range.
882 return gregorianDateTimeToMS(t, 0, false);
883 }
884
885 return (ymdhmsToSeconds(year, month + 1, day, hour, minute, second) - (offset * 60.0)) * msPerSecond;
886 }
887
888 double timeClip(double t)
889 {
890 if (!isfinite(t))
891 return NaN;
892 if (fabs(t) > 8.64E15)
893 return NaN;
894 return trunc(t);
895 }
896
897 UString formatDate(const GregorianDateTime &t)
898 {
899 char buffer[100];
900 snprintf(buffer, sizeof(buffer), "%s %s %02d %04d",
901 weekdayName[(t.weekDay + 6) % 7],
902 monthName[t.month], t.monthDay, t.year + 1900);
903 return buffer;
904 }
905
906 UString formatDateUTCVariant(const GregorianDateTime &t)
907 {
908 char buffer[100];
909 snprintf(buffer, sizeof(buffer), "%s, %02d %s %04d",
910 weekdayName[(t.weekDay + 6) % 7],
911 t.monthDay, monthName[t.month], t.year + 1900);
912 return buffer;
913 }
914
915 UString formatTime(const GregorianDateTime &t, bool utc)
916 {
917 char buffer[100];
918 if (utc) {
919 snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT", t.hour, t.minute, t.second);
920 } else {
921 int offset = abs(gmtoffset(t));
922 char tzname[70];
923 struct tm gtm = t;
924 strftime(tzname, sizeof(tzname), "%Z", &gtm);
925
926 if (tzname[0]) {
927 snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT%c%02d%02d (%s)",
928 t.hour, t.minute, t.second,
929 gmtoffset(t) < 0 ? '-' : '+', offset / (60*60), (offset / 60) % 60, tzname);
930 } else {
931 snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT%c%02d%02d",
932 t.hour, t.minute, t.second,
933 gmtoffset(t) < 0 ? '-' : '+', offset / (60*60), (offset / 60) % 60);
934 }
935 }
936 return UString(buffer);
937 }
938
939 } // namespace JSC