1 | /*
|
---|
2 | * Copyright (C) 1999-2000 Harri Porten ([email protected])
|
---|
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/MathExtras.h>
|
---|
52 | #include <wtf/StringExtras.h>
|
---|
53 |
|
---|
54 | #if HAVE(ERRNO_H)
|
---|
55 | #include <errno.h>
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #if PLATFORM(DARWIN)
|
---|
59 | #include <notify.h>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #if HAVE(SYS_TIME_H)
|
---|
63 | #include <sys/time.h>
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #if HAVE(SYS_TIMEB_H)
|
---|
67 | #include <sys/timeb.h>
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | using namespace WTF;
|
---|
71 |
|
---|
72 | namespace JSC {
|
---|
73 |
|
---|
74 | /* Constants */
|
---|
75 |
|
---|
76 | static const double minutesPerDay = 24.0 * 60.0;
|
---|
77 | static const double secondsPerDay = 24.0 * 60.0 * 60.0;
|
---|
78 | static const double secondsPerYear = 24.0 * 60.0 * 60.0 * 365.0;
|
---|
79 |
|
---|
80 | static const double usecPerSec = 1000000.0;
|
---|
81 |
|
---|
82 | static const double maxUnixTime = 2145859200.0; // 12/31/2037
|
---|
83 |
|
---|
84 | // Day of year for the first day of each month, where index 0 is January, and day 0 is January 1.
|
---|
85 | // First for non-leap years, then for leap years.
|
---|
86 | static const int firstDayOfMonth[2][12] = {
|
---|
87 | {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
|
---|
88 | {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
|
---|
89 | };
|
---|
90 |
|
---|
91 | static inline bool isLeapYear(int year)
|
---|
92 | {
|
---|
93 | if (year % 4 != 0)
|
---|
94 | return false;
|
---|
95 | if (year % 400 == 0)
|
---|
96 | return true;
|
---|
97 | if (year % 100 == 0)
|
---|
98 | return false;
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | static inline int daysInYear(int year)
|
---|
103 | {
|
---|
104 | return 365 + isLeapYear(year);
|
---|
105 | }
|
---|
106 |
|
---|
107 | static inline double daysFrom1970ToYear(int year)
|
---|
108 | {
|
---|
109 | // The Gregorian Calendar rules for leap years:
|
---|
110 | // Every fourth year is a leap year. 2004, 2008, and 2012 are leap years.
|
---|
111 | // However, every hundredth year is not a leap year. 1900 and 2100 are not leap years.
|
---|
112 | // Every four hundred years, there's a leap year after all. 2000 and 2400 are leap years.
|
---|
113 |
|
---|
114 | static const int leapDaysBefore1971By4Rule = 1970 / 4;
|
---|
115 | static const int excludedLeapDaysBefore1971By100Rule = 1970 / 100;
|
---|
116 | static const int leapDaysBefore1971By400Rule = 1970 / 400;
|
---|
117 |
|
---|
118 | const double yearMinusOne = year - 1;
|
---|
119 | const double yearsToAddBy4Rule = floor(yearMinusOne / 4.0) - leapDaysBefore1971By4Rule;
|
---|
120 | const double yearsToExcludeBy100Rule = floor(yearMinusOne / 100.0) - excludedLeapDaysBefore1971By100Rule;
|
---|
121 | const double yearsToAddBy400Rule = floor(yearMinusOne / 400.0) - leapDaysBefore1971By400Rule;
|
---|
122 |
|
---|
123 | return 365.0 * (year - 1970) + yearsToAddBy4Rule - yearsToExcludeBy100Rule + yearsToAddBy400Rule;
|
---|
124 | }
|
---|
125 |
|
---|
126 | static inline double msToDays(double ms)
|
---|
127 | {
|
---|
128 | return floor(ms / msPerDay);
|
---|
129 | }
|
---|
130 |
|
---|
131 | static inline int msToYear(double ms)
|
---|
132 | {
|
---|
133 | int approxYear = static_cast<int>(floor(ms / (msPerDay * 365.2425)) + 1970);
|
---|
134 | double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear);
|
---|
135 | if (msFromApproxYearTo1970 > ms)
|
---|
136 | return approxYear - 1;
|
---|
137 | if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms)
|
---|
138 | return approxYear + 1;
|
---|
139 | return approxYear;
|
---|
140 | }
|
---|
141 |
|
---|
142 | static inline int dayInYear(double ms, int year)
|
---|
143 | {
|
---|
144 | return static_cast<int>(msToDays(ms) - daysFrom1970ToYear(year));
|
---|
145 | }
|
---|
146 |
|
---|
147 | static inline double msToMilliseconds(double ms)
|
---|
148 | {
|
---|
149 | double result = fmod(ms, msPerDay);
|
---|
150 | if (result < 0)
|
---|
151 | result += msPerDay;
|
---|
152 | return result;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // 0: Sunday, 1: Monday, etc.
|
---|
156 | static inline int msToWeekDay(double ms)
|
---|
157 | {
|
---|
158 | int wd = (static_cast<int>(msToDays(ms)) + 4) % 7;
|
---|
159 | if (wd < 0)
|
---|
160 | wd += 7;
|
---|
161 | return wd;
|
---|
162 | }
|
---|
163 |
|
---|
164 | static inline int msToSeconds(double ms)
|
---|
165 | {
|
---|
166 | double result = fmod(floor(ms / msPerSecond), secondsPerMinute);
|
---|
167 | if (result < 0)
|
---|
168 | result += secondsPerMinute;
|
---|
169 | return static_cast<int>(result);
|
---|
170 | }
|
---|
171 |
|
---|
172 | static inline int msToMinutes(double ms)
|
---|
173 | {
|
---|
174 | double result = fmod(floor(ms / msPerMinute), minutesPerHour);
|
---|
175 | if (result < 0)
|
---|
176 | result += minutesPerHour;
|
---|
177 | return static_cast<int>(result);
|
---|
178 | }
|
---|
179 |
|
---|
180 | static inline int msToHours(double ms)
|
---|
181 | {
|
---|
182 | double result = fmod(floor(ms/msPerHour), hoursPerDay);
|
---|
183 | if (result < 0)
|
---|
184 | result += hoursPerDay;
|
---|
185 | return static_cast<int>(result);
|
---|
186 | }
|
---|
187 |
|
---|
188 | static inline int monthFromDayInYear(int dayInYear, bool leapYear)
|
---|
189 | {
|
---|
190 | const int d = dayInYear;
|
---|
191 | int step;
|
---|
192 |
|
---|
193 | if (d < (step = 31))
|
---|
194 | return 0;
|
---|
195 | step += (leapYear ? 29 : 28);
|
---|
196 | if (d < step)
|
---|
197 | return 1;
|
---|
198 | if (d < (step += 31))
|
---|
199 | return 2;
|
---|
200 | if (d < (step += 30))
|
---|
201 | return 3;
|
---|
202 | if (d < (step += 31))
|
---|
203 | return 4;
|
---|
204 | if (d < (step += 30))
|
---|
205 | return 5;
|
---|
206 | if (d < (step += 31))
|
---|
207 | return 6;
|
---|
208 | if (d < (step += 31))
|
---|
209 | return 7;
|
---|
210 | if (d < (step += 30))
|
---|
211 | return 8;
|
---|
212 | if (d < (step += 31))
|
---|
213 | return 9;
|
---|
214 | if (d < (step += 30))
|
---|
215 | return 10;
|
---|
216 | return 11;
|
---|
217 | }
|
---|
218 |
|
---|
219 | static inline bool checkMonth(int dayInYear, int& startDayOfThisMonth, int& startDayOfNextMonth, int daysInThisMonth)
|
---|
220 | {
|
---|
221 | startDayOfThisMonth = startDayOfNextMonth;
|
---|
222 | startDayOfNextMonth += daysInThisMonth;
|
---|
223 | return (dayInYear <= startDayOfNextMonth);
|
---|
224 | }
|
---|
225 |
|
---|
226 | static inline int dayInMonthFromDayInYear(int dayInYear, bool leapYear)
|
---|
227 | {
|
---|
228 | const int d = dayInYear;
|
---|
229 | int step;
|
---|
230 | int next = 30;
|
---|
231 |
|
---|
232 | if (d <= next)
|
---|
233 | return d + 1;
|
---|
234 | const int daysInFeb = (leapYear ? 29 : 28);
|
---|
235 | if (checkMonth(d, step, next, daysInFeb))
|
---|
236 | return d - step;
|
---|
237 | if (checkMonth(d, step, next, 31))
|
---|
238 | return d - step;
|
---|
239 | if (checkMonth(d, step, next, 30))
|
---|
240 | return d - step;
|
---|
241 | if (checkMonth(d, step, next, 31))
|
---|
242 | return d - step;
|
---|
243 | if (checkMonth(d, step, next, 30))
|
---|
244 | return d - step;
|
---|
245 | if (checkMonth(d, step, next, 31))
|
---|
246 | return d - step;
|
---|
247 | if (checkMonth(d, step, next, 31))
|
---|
248 | return d - step;
|
---|
249 | if (checkMonth(d, step, next, 30))
|
---|
250 | return d - step;
|
---|
251 | if (checkMonth(d, step, next, 31))
|
---|
252 | return d - step;
|
---|
253 | if (checkMonth(d, step, next, 30))
|
---|
254 | return d - step;
|
---|
255 | step = next;
|
---|
256 | return d - step;
|
---|
257 | }
|
---|
258 |
|
---|
259 | static inline int monthToDayInYear(int month, bool isLeapYear)
|
---|
260 | {
|
---|
261 | return firstDayOfMonth[isLeapYear][month];
|
---|
262 | }
|
---|
263 |
|
---|
264 | static inline double timeToMS(double hour, double min, double sec, double ms)
|
---|
265 | {
|
---|
266 | return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms);
|
---|
267 | }
|
---|
268 |
|
---|
269 | static int dateToDayInYear(int year, int month, int day)
|
---|
270 | {
|
---|
271 | year += month / 12;
|
---|
272 |
|
---|
273 | month %= 12;
|
---|
274 | if (month < 0) {
|
---|
275 | month += 12;
|
---|
276 | --year;
|
---|
277 | }
|
---|
278 |
|
---|
279 | int yearday = static_cast<int>(floor(daysFrom1970ToYear(year)));
|
---|
280 | int monthday = monthToDayInYear(month, isLeapYear(year));
|
---|
281 |
|
---|
282 | return yearday + monthday + day - 1;
|
---|
283 | }
|
---|
284 |
|
---|
285 | double getCurrentUTCTime()
|
---|
286 | {
|
---|
287 | return floor(getCurrentUTCTimeWithMicroseconds());
|
---|
288 | }
|
---|
289 |
|
---|
290 | double getCurrentUTCTimeWithMicroseconds()
|
---|
291 | {
|
---|
292 | #if PLATFORM(WIN_OS)
|
---|
293 | // FIXME: the implementation for Windows is only millisecond resolution.
|
---|
294 | #if COMPILER(BORLAND)
|
---|
295 | struct timeb timebuffer;
|
---|
296 | ftime(&timebuffer);
|
---|
297 | #else
|
---|
298 | struct _timeb timebuffer;
|
---|
299 | _ftime(&timebuffer);
|
---|
300 | #endif
|
---|
301 | double utc = timebuffer.time * msPerSecond + timebuffer.millitm;
|
---|
302 | #else
|
---|
303 | struct timeval tv;
|
---|
304 | gettimeofday(&tv, 0);
|
---|
305 | double utc = tv.tv_sec * msPerSecond + tv.tv_usec / 1000.0;
|
---|
306 | #endif
|
---|
307 | return utc;
|
---|
308 | }
|
---|
309 |
|
---|
310 | void getLocalTime(const time_t* localTime, struct tm* localTM)
|
---|
311 | {
|
---|
312 | #if COMPILER(MSVC7) || COMPILER(MINGW)
|
---|
313 | *localTM = *localtime(localTime);
|
---|
314 | #elif COMPILER(MSVC)
|
---|
315 | localtime_s(localTM, localTime);
|
---|
316 | #else
|
---|
317 | localtime_r(localTime, localTM);
|
---|
318 | #endif
|
---|
319 | }
|
---|
320 |
|
---|
321 | // There is a hard limit at 2038 that we currently do not have a workaround
|
---|
322 | // for (rdar://problem/5052975).
|
---|
323 | static inline int maximumYearForDST()
|
---|
324 | {
|
---|
325 | return 2037;
|
---|
326 | }
|
---|
327 |
|
---|
328 | static inline int mimimumYearForDST()
|
---|
329 | {
|
---|
330 | // Because of the 2038 issue (see maximumYearForDST) if the current year is
|
---|
331 | // greater than the max year minus 27 (2010), we want to use the max year
|
---|
332 | // minus 27 instead, to ensure there is a range of 28 years that all years
|
---|
333 | // can map to.
|
---|
334 | return std::min(msToYear(getCurrentUTCTime()), maximumYearForDST() - 27) ;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /*
|
---|
338 | * Find an equivalent year for the one given, where equivalence is deterined by
|
---|
339 | * the two years having the same leapness and the first day of the year, falling
|
---|
340 | * on the same day of the week.
|
---|
341 | *
|
---|
342 | * This function returns a year between this current year and 2037, however this
|
---|
343 | * function will potentially return incorrect results if the current year is after
|
---|
344 | * 2010, (rdar://problem/5052975), if the year passed in is before 1900 or after
|
---|
345 | * 2100, (rdar://problem/5055038).
|
---|
346 | */
|
---|
347 | int equivalentYearForDST(int year)
|
---|
348 | {
|
---|
349 | // It is ok if the cached year is not the current year as long as the rules
|
---|
350 | // for DST did not change between the two years; if they did the app would need
|
---|
351 | // to be restarted.
|
---|
352 | static int minYear = mimimumYearForDST();
|
---|
353 | int maxYear = maximumYearForDST();
|
---|
354 |
|
---|
355 | int difference;
|
---|
356 | if (year > maxYear)
|
---|
357 | difference = minYear - year;
|
---|
358 | else if (year < minYear)
|
---|
359 | difference = maxYear - year;
|
---|
360 | else
|
---|
361 | return year;
|
---|
362 |
|
---|
363 | int quotient = difference / 28;
|
---|
364 | int product = (quotient) * 28;
|
---|
365 |
|
---|
366 | year += product;
|
---|
367 | ASSERT((year >= minYear && year <= maxYear) || (product - year == static_cast<int>(NaN)));
|
---|
368 | return year;
|
---|
369 | }
|
---|
370 |
|
---|
371 | static int32_t calculateUTCOffset()
|
---|
372 | {
|
---|
373 | tm localt;
|
---|
374 | memset(&localt, 0, sizeof(localt));
|
---|
375 |
|
---|
376 | // get the difference between this time zone and UTC on Jan 01, 2000 12:00:00 AM
|
---|
377 | localt.tm_mday = 1;
|
---|
378 | localt.tm_year = 100;
|
---|
379 | time_t utcOffset = 946684800 - mktime(&localt);
|
---|
380 |
|
---|
381 | return static_cast<int32_t>(utcOffset * 1000);
|
---|
382 | }
|
---|
383 |
|
---|
384 | #if PLATFORM(DARWIN)
|
---|
385 | 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.
|
---|
386 | static bool s_haveCachedUTCOffset;
|
---|
387 | static int s_notificationToken;
|
---|
388 | #endif
|
---|
389 |
|
---|
390 | /*
|
---|
391 | * Get the difference in milliseconds between this time zone and UTC (GMT)
|
---|
392 | * NOT including DST.
|
---|
393 | */
|
---|
394 | double getUTCOffset()
|
---|
395 | {
|
---|
396 | #if PLATFORM(DARWIN)
|
---|
397 | if (s_haveCachedUTCOffset) {
|
---|
398 | int notified;
|
---|
399 | uint32_t status = notify_check(s_notificationToken, ¬ified);
|
---|
400 | if (status == NOTIFY_STATUS_OK && !notified)
|
---|
401 | return s_cachedUTCOffset;
|
---|
402 | }
|
---|
403 | #endif
|
---|
404 |
|
---|
405 | int32_t utcOffset = calculateUTCOffset();
|
---|
406 |
|
---|
407 | #if PLATFORM(DARWIN)
|
---|
408 | // Theoretically, it is possible that several threads will be executing this code at once, in which case we will have a race condition,
|
---|
409 | // and a newer value may be overwritten. In practice, time zones don't change that often.
|
---|
410 | s_cachedUTCOffset = utcOffset;
|
---|
411 | #endif
|
---|
412 |
|
---|
413 | return utcOffset;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /*
|
---|
417 | * Get the DST offset for the time passed in. Takes
|
---|
418 | * seconds (not milliseconds) and cannot handle dates before 1970
|
---|
419 | * on some OS'
|
---|
420 | */
|
---|
421 | static double getDSTOffsetSimple(double localTimeSeconds, double utcOffset)
|
---|
422 | {
|
---|
423 | if (localTimeSeconds > maxUnixTime)
|
---|
424 | localTimeSeconds = maxUnixTime;
|
---|
425 | else if (localTimeSeconds < 0) // Go ahead a day to make localtime work (does not work with 0)
|
---|
426 | localTimeSeconds += secondsPerDay;
|
---|
427 |
|
---|
428 | //input is UTC so we have to shift back to local time to determine DST thus the + getUTCOffset()
|
---|
429 | double offsetTime = (localTimeSeconds * msPerSecond) + utcOffset;
|
---|
430 |
|
---|
431 | // Offset from UTC but doesn't include DST obviously
|
---|
432 | int offsetHour = msToHours(offsetTime);
|
---|
433 | int offsetMinute = msToMinutes(offsetTime);
|
---|
434 |
|
---|
435 | // FIXME: time_t has a potential problem in 2038
|
---|
436 | time_t localTime = static_cast<time_t>(localTimeSeconds);
|
---|
437 |
|
---|
438 | tm localTM;
|
---|
439 | getLocalTime(&localTime, &localTM);
|
---|
440 |
|
---|
441 | double diff = ((localTM.tm_hour - offsetHour) * secondsPerHour) + ((localTM.tm_min - offsetMinute) * 60);
|
---|
442 |
|
---|
443 | if (diff < 0)
|
---|
444 | diff += secondsPerDay;
|
---|
445 |
|
---|
446 | return (diff * msPerSecond);
|
---|
447 | }
|
---|
448 |
|
---|
449 | // Get the DST offset, given a time in UTC
|
---|
450 | static double getDSTOffset(double ms, double utcOffset)
|
---|
451 | {
|
---|
452 | // On Mac OS X, the call to localtime (see getDSTOffsetSimple) will return historically accurate
|
---|
453 | // DST information (e.g. New Zealand did not have DST from 1946 to 1974) however the JavaScript
|
---|
454 | // standard explicitly dictates that historical information should not be considered when
|
---|
455 | // determining DST. For this reason we shift away from years that localtime can handle but would
|
---|
456 | // return historically accurate information.
|
---|
457 | int year = msToYear(ms);
|
---|
458 | int equivalentYear = equivalentYearForDST(year);
|
---|
459 | if (year != equivalentYear) {
|
---|
460 | bool leapYear = isLeapYear(year);
|
---|
461 | int dayInYearLocal = dayInYear(ms, year);
|
---|
462 | int dayInMonth = dayInMonthFromDayInYear(dayInYearLocal, leapYear);
|
---|
463 | int month = monthFromDayInYear(dayInYearLocal, leapYear);
|
---|
464 | int day = dateToDayInYear(equivalentYear, month, dayInMonth);
|
---|
465 | ms = (day * msPerDay) + msToMilliseconds(ms);
|
---|
466 | }
|
---|
467 |
|
---|
468 | return getDSTOffsetSimple(ms / msPerSecond, utcOffset);
|
---|
469 | }
|
---|
470 |
|
---|
471 | double gregorianDateTimeToMS(const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
|
---|
472 | {
|
---|
473 | int day = dateToDayInYear(t.year + 1900, t.month, t.monthDay);
|
---|
474 | double ms = timeToMS(t.hour, t.minute, t.second, milliSeconds);
|
---|
475 | double result = (day * msPerDay) + ms;
|
---|
476 |
|
---|
477 | if (!inputIsUTC) { // convert to UTC
|
---|
478 | double utcOffset = getUTCOffset();
|
---|
479 | result -= utcOffset;
|
---|
480 | result -= getDSTOffset(result, utcOffset);
|
---|
481 | }
|
---|
482 |
|
---|
483 | return result;
|
---|
484 | }
|
---|
485 |
|
---|
486 | void msToGregorianDateTime(double ms, bool outputIsUTC, GregorianDateTime& tm)
|
---|
487 | {
|
---|
488 | // input is UTC
|
---|
489 | double dstOff = 0.0;
|
---|
490 | const double utcOff = getUTCOffset();
|
---|
491 |
|
---|
492 | if (!outputIsUTC) { // convert to local time
|
---|
493 | dstOff = getDSTOffset(ms, utcOff);
|
---|
494 | ms += dstOff + utcOff;
|
---|
495 | }
|
---|
496 |
|
---|
497 | const int year = msToYear(ms);
|
---|
498 | tm.second = msToSeconds(ms);
|
---|
499 | tm.minute = msToMinutes(ms);
|
---|
500 | tm.hour = msToHours(ms);
|
---|
501 | tm.weekDay = msToWeekDay(ms);
|
---|
502 | tm.yearDay = dayInYear(ms, year);
|
---|
503 | tm.monthDay = dayInMonthFromDayInYear(tm.yearDay, isLeapYear(year));
|
---|
504 | tm.month = monthFromDayInYear(tm.yearDay, isLeapYear(year));
|
---|
505 | tm.year = year - 1900;
|
---|
506 | tm.isDST = dstOff != 0.0;
|
---|
507 |
|
---|
508 | tm.utcOffset = static_cast<long>((dstOff + utcOff) / msPerSecond);
|
---|
509 | tm.timeZone = NULL;
|
---|
510 | }
|
---|
511 |
|
---|
512 | void initDateMath()
|
---|
513 | {
|
---|
514 | #ifndef NDEBUG
|
---|
515 | static bool alreadyInitialized;
|
---|
516 | ASSERT(!alreadyInitialized++);
|
---|
517 | #endif
|
---|
518 |
|
---|
519 | equivalentYearForDST(2000); // Need to call once to initialize a static used in this function.
|
---|
520 | #if PLATFORM(DARWIN)
|
---|
521 | // Register for a notification whenever the time zone changes.
|
---|
522 | uint32_t status = notify_register_check("com.apple.system.timezone", &s_notificationToken);
|
---|
523 | if (status == NOTIFY_STATUS_OK) {
|
---|
524 | s_cachedUTCOffset = calculateUTCOffset();
|
---|
525 | s_haveCachedUTCOffset = true;
|
---|
526 | }
|
---|
527 | #endif
|
---|
528 | }
|
---|
529 |
|
---|
530 | static inline double ymdhmsToSeconds(long year, int mon, int day, int hour, int minute, int second)
|
---|
531 | {
|
---|
532 | double days = (day - 32075)
|
---|
533 | + floor(1461 * (year + 4800.0 + (mon - 14) / 12) / 4)
|
---|
534 | + 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12
|
---|
535 | - floor(3 * ((year + 4900.0 + (mon - 14) / 12) / 100) / 4)
|
---|
536 | - 2440588;
|
---|
537 | return ((days * hoursPerDay + hour) * minutesPerHour + minute) * secondsPerMinute + second;
|
---|
538 | }
|
---|
539 |
|
---|
540 | // We follow the recommendation of RFC 2822 to consider all
|
---|
541 | // obsolete time zones not listed here equivalent to "-0000".
|
---|
542 | static const struct KnownZone {
|
---|
543 | #if !PLATFORM(WIN_OS)
|
---|
544 | const
|
---|
545 | #endif
|
---|
546 | char tzName[4];
|
---|
547 | int tzOffset;
|
---|
548 | } known_zones[] = {
|
---|
549 | { "UT", 0 },
|
---|
550 | { "GMT", 0 },
|
---|
551 | { "EST", -300 },
|
---|
552 | { "EDT", -240 },
|
---|
553 | { "CST", -360 },
|
---|
554 | { "CDT", -300 },
|
---|
555 | { "MST", -420 },
|
---|
556 | { "MDT", -360 },
|
---|
557 | { "PST", -480 },
|
---|
558 | { "PDT", -420 }
|
---|
559 | };
|
---|
560 |
|
---|
561 | inline static void skipSpacesAndComments(const char*& s)
|
---|
562 | {
|
---|
563 | int nesting = 0;
|
---|
564 | char ch;
|
---|
565 | while ((ch = *s)) {
|
---|
566 | if (!isASCIISpace(ch)) {
|
---|
567 | if (ch == '(')
|
---|
568 | nesting++;
|
---|
569 | else if (ch == ')' && nesting > 0)
|
---|
570 | nesting--;
|
---|
571 | else if (nesting == 0)
|
---|
572 | break;
|
---|
573 | }
|
---|
574 | s++;
|
---|
575 | }
|
---|
576 | }
|
---|
577 |
|
---|
578 | // returns 0-11 (Jan-Dec); -1 on failure
|
---|
579 | static int findMonth(const char* monthStr)
|
---|
580 | {
|
---|
581 | ASSERT(monthStr);
|
---|
582 | char needle[4];
|
---|
583 | for (int i = 0; i < 3; ++i) {
|
---|
584 | if (!*monthStr)
|
---|
585 | return -1;
|
---|
586 | needle[i] = static_cast<char>(toASCIILower(*monthStr++));
|
---|
587 | }
|
---|
588 | needle[3] = '\0';
|
---|
589 | const char *haystack = "janfebmaraprmayjunjulaugsepoctnovdec";
|
---|
590 | const char *str = strstr(haystack, needle);
|
---|
591 | if (str) {
|
---|
592 | int position = static_cast<int>(str - haystack);
|
---|
593 | if (position % 3 == 0)
|
---|
594 | return position / 3;
|
---|
595 | }
|
---|
596 | return -1;
|
---|
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 | errno = 0;
|
---|
646 | long day = strtol(dateString, &newPosStr, 10);
|
---|
647 | if (errno)
|
---|
648 | return NaN;
|
---|
649 | dateString = newPosStr;
|
---|
650 |
|
---|
651 | if (!*dateString)
|
---|
652 | return NaN;
|
---|
653 |
|
---|
654 | if (day < 0)
|
---|
655 | return NaN;
|
---|
656 |
|
---|
657 | long year = 0;
|
---|
658 | if (day > 31) {
|
---|
659 | // ### where is the boundary and what happens below?
|
---|
660 | if (*dateString != '/')
|
---|
661 | return NaN;
|
---|
662 | // looks like a YYYY/MM/DD date
|
---|
663 | if (!*++dateString)
|
---|
664 | return NaN;
|
---|
665 | year = day;
|
---|
666 | month = strtol(dateString, &newPosStr, 10) - 1;
|
---|
667 | if (errno)
|
---|
668 | return NaN;
|
---|
669 | dateString = newPosStr;
|
---|
670 | if (*dateString++ != '/' || !*dateString)
|
---|
671 | return NaN;
|
---|
672 | day = strtol(dateString, &newPosStr, 10);
|
---|
673 | if (errno)
|
---|
674 | return NaN;
|
---|
675 | dateString = newPosStr;
|
---|
676 | } else if (*dateString == '/' && month == -1) {
|
---|
677 | dateString++;
|
---|
678 | // This looks like a MM/DD/YYYY date, not an RFC date.
|
---|
679 | month = day - 1; // 0-based
|
---|
680 | day = strtol(dateString, &newPosStr, 10);
|
---|
681 | if (errno)
|
---|
682 | return NaN;
|
---|
683 | if (day < 1 || day > 31)
|
---|
684 | return NaN;
|
---|
685 | dateString = newPosStr;
|
---|
686 | if (*dateString == '/')
|
---|
687 | dateString++;
|
---|
688 | if (!*dateString)
|
---|
689 | return NaN;
|
---|
690 | } else {
|
---|
691 | if (*dateString == '-')
|
---|
692 | dateString++;
|
---|
693 |
|
---|
694 | skipSpacesAndComments(dateString);
|
---|
695 |
|
---|
696 | if (*dateString == ',')
|
---|
697 | dateString++;
|
---|
698 |
|
---|
699 | if (month == -1) { // not found yet
|
---|
700 | month = findMonth(dateString);
|
---|
701 | if (month == -1)
|
---|
702 | return NaN;
|
---|
703 |
|
---|
704 | while (*dateString && *dateString != '-' && *dateString != ',' && !isASCIISpace(*dateString))
|
---|
705 | dateString++;
|
---|
706 |
|
---|
707 | if (!*dateString)
|
---|
708 | return NaN;
|
---|
709 |
|
---|
710 | // '-99 23:12:40 GMT'
|
---|
711 | if (*dateString != '-' && *dateString != '/' && *dateString != ',' && !isASCIISpace(*dateString))
|
---|
712 | return NaN;
|
---|
713 | dateString++;
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | if (month < 0 || month > 11)
|
---|
718 | return NaN;
|
---|
719 |
|
---|
720 | // '99 23:12:40 GMT'
|
---|
721 | if (year <= 0 && *dateString) {
|
---|
722 | year = strtol(dateString, &newPosStr, 10);
|
---|
723 | if (errno)
|
---|
724 | return NaN;
|
---|
725 | }
|
---|
726 |
|
---|
727 | // Don't fail if the time is missing.
|
---|
728 | long hour = 0;
|
---|
729 | long minute = 0;
|
---|
730 | long second = 0;
|
---|
731 | if (!*newPosStr)
|
---|
732 | dateString = newPosStr;
|
---|
733 | else {
|
---|
734 | // ' 23:12:40 GMT'
|
---|
735 | if (!(isASCIISpace(*newPosStr) || *newPosStr == ',')) {
|
---|
736 | if (*newPosStr != ':')
|
---|
737 | return NaN;
|
---|
738 | // There was no year; the number was the hour.
|
---|
739 | year = -1;
|
---|
740 | } else {
|
---|
741 | // in the normal case (we parsed the year), advance to the next number
|
---|
742 | dateString = ++newPosStr;
|
---|
743 | skipSpacesAndComments(dateString);
|
---|
744 | }
|
---|
745 |
|
---|
746 | hour = strtol(dateString, &newPosStr, 10);
|
---|
747 | // Do not check for errno here since we want to continue
|
---|
748 | // even if errno was set becasue we are still looking
|
---|
749 | // for the timezone!
|
---|
750 |
|
---|
751 | // Read a number? If not, this might be a timezone name.
|
---|
752 | if (newPosStr != dateString) {
|
---|
753 | dateString = newPosStr;
|
---|
754 |
|
---|
755 | if (hour < 0 || hour > 23)
|
---|
756 | return NaN;
|
---|
757 |
|
---|
758 | if (!*dateString)
|
---|
759 | return NaN;
|
---|
760 |
|
---|
761 | // ':12:40 GMT'
|
---|
762 | if (*dateString++ != ':')
|
---|
763 | return NaN;
|
---|
764 |
|
---|
765 | minute = strtol(dateString, &newPosStr, 10);
|
---|
766 | if (errno)
|
---|
767 | return NaN;
|
---|
768 | dateString = newPosStr;
|
---|
769 |
|
---|
770 | if (minute < 0 || minute > 59)
|
---|
771 | return NaN;
|
---|
772 |
|
---|
773 | // ':40 GMT'
|
---|
774 | if (*dateString && *dateString != ':' && !isASCIISpace(*dateString))
|
---|
775 | return NaN;
|
---|
776 |
|
---|
777 | // seconds are optional in rfc822 + rfc2822
|
---|
778 | if (*dateString ==':') {
|
---|
779 | dateString++;
|
---|
780 |
|
---|
781 | second = strtol(dateString, &newPosStr, 10);
|
---|
782 | if (errno)
|
---|
783 | return NaN;
|
---|
784 | dateString = newPosStr;
|
---|
785 |
|
---|
786 | if (second < 0 || second > 59)
|
---|
787 | return NaN;
|
---|
788 | }
|
---|
789 |
|
---|
790 | skipSpacesAndComments(dateString);
|
---|
791 |
|
---|
792 | if (strncasecmp(dateString, "AM", 2) == 0) {
|
---|
793 | if (hour > 12)
|
---|
794 | return NaN;
|
---|
795 | if (hour == 12)
|
---|
796 | hour = 0;
|
---|
797 | dateString += 2;
|
---|
798 | skipSpacesAndComments(dateString);
|
---|
799 | } else if (strncasecmp(dateString, "PM", 2) == 0) {
|
---|
800 | if (hour > 12)
|
---|
801 | return NaN;
|
---|
802 | if (hour != 12)
|
---|
803 | hour += 12;
|
---|
804 | dateString += 2;
|
---|
805 | skipSpacesAndComments(dateString);
|
---|
806 | }
|
---|
807 | }
|
---|
808 | }
|
---|
809 |
|
---|
810 | bool haveTZ = false;
|
---|
811 | int offset = 0;
|
---|
812 |
|
---|
813 | // Don't fail if the time zone is missing.
|
---|
814 | // Some websites omit the time zone (4275206).
|
---|
815 | if (*dateString) {
|
---|
816 | if (strncasecmp(dateString, "GMT", 3) == 0 || strncasecmp(dateString, "UTC", 3) == 0) {
|
---|
817 | dateString += 3;
|
---|
818 | haveTZ = true;
|
---|
819 | }
|
---|
820 |
|
---|
821 | if (*dateString == '+' || *dateString == '-') {
|
---|
822 | long o = strtol(dateString, &newPosStr, 10);
|
---|
823 | if (errno)
|
---|
824 | return NaN;
|
---|
825 | dateString = newPosStr;
|
---|
826 |
|
---|
827 | if (o < -9959 || o > 9959)
|
---|
828 | return NaN;
|
---|
829 |
|
---|
830 | int sgn = (o < 0) ? -1 : 1;
|
---|
831 | o = abs(o);
|
---|
832 | if (*dateString != ':') {
|
---|
833 | offset = ((o / 100) * 60 + (o % 100)) * sgn;
|
---|
834 | } else { // GMT+05:00
|
---|
835 | long o2 = strtol(dateString, &newPosStr, 10);
|
---|
836 | if (errno)
|
---|
837 | return NaN;
|
---|
838 | dateString = newPosStr;
|
---|
839 | offset = (o * 60 + o2) * sgn;
|
---|
840 | }
|
---|
841 | haveTZ = true;
|
---|
842 | } else {
|
---|
843 | for (int i = 0; i < int(sizeof(known_zones) / sizeof(KnownZone)); i++) {
|
---|
844 | if (0 == strncasecmp(dateString, known_zones[i].tzName, strlen(known_zones[i].tzName))) {
|
---|
845 | offset = known_zones[i].tzOffset;
|
---|
846 | dateString += strlen(known_zones[i].tzName);
|
---|
847 | haveTZ = true;
|
---|
848 | break;
|
---|
849 | }
|
---|
850 | }
|
---|
851 | }
|
---|
852 | }
|
---|
853 |
|
---|
854 | skipSpacesAndComments(dateString);
|
---|
855 |
|
---|
856 | if (*dateString && year == -1) {
|
---|
857 | year = strtol(dateString, &newPosStr, 10);
|
---|
858 | if (errno)
|
---|
859 | return NaN;
|
---|
860 | dateString = newPosStr;
|
---|
861 | }
|
---|
862 |
|
---|
863 | skipSpacesAndComments(dateString);
|
---|
864 |
|
---|
865 | // Trailing garbage
|
---|
866 | if (*dateString)
|
---|
867 | return NaN;
|
---|
868 |
|
---|
869 | // Y2K: Handle 2 digit years.
|
---|
870 | if (year >= 0 && year < 100) {
|
---|
871 | if (year < 50)
|
---|
872 | year += 2000;
|
---|
873 | else
|
---|
874 | year += 1900;
|
---|
875 | }
|
---|
876 |
|
---|
877 | // fall back to local timezone
|
---|
878 | if (!haveTZ) {
|
---|
879 | GregorianDateTime t;
|
---|
880 | t.monthDay = day;
|
---|
881 | t.month = month;
|
---|
882 | t.year = year - 1900;
|
---|
883 | t.isDST = -1;
|
---|
884 | t.second = second;
|
---|
885 | t.minute = minute;
|
---|
886 | t.hour = hour;
|
---|
887 |
|
---|
888 | // Use our gregorianDateTimeToMS() rather than mktime() as the latter can't handle the full year range.
|
---|
889 | return gregorianDateTimeToMS(t, 0, false);
|
---|
890 | }
|
---|
891 |
|
---|
892 | return (ymdhmsToSeconds(year, month + 1, day, hour, minute, second) - (offset * 60.0)) * msPerSecond;
|
---|
893 | }
|
---|
894 |
|
---|
895 | double timeClip(double t)
|
---|
896 | {
|
---|
897 | if (!isfinite(t))
|
---|
898 | return NaN;
|
---|
899 | if (fabs(t) > 8.64E15)
|
---|
900 | return NaN;
|
---|
901 | return trunc(t);
|
---|
902 | }
|
---|
903 |
|
---|
904 | UString formatDate(const GregorianDateTime &t)
|
---|
905 | {
|
---|
906 | char buffer[100];
|
---|
907 | snprintf(buffer, sizeof(buffer), "%s %s %02d %04d",
|
---|
908 | weekdayName[(t.weekDay + 6) % 7],
|
---|
909 | monthName[t.month], t.monthDay, t.year + 1900);
|
---|
910 | return buffer;
|
---|
911 | }
|
---|
912 |
|
---|
913 | UString formatDateUTCVariant(const GregorianDateTime &t)
|
---|
914 | {
|
---|
915 | char buffer[100];
|
---|
916 | snprintf(buffer, sizeof(buffer), "%s, %02d %s %04d",
|
---|
917 | weekdayName[(t.weekDay + 6) % 7],
|
---|
918 | t.monthDay, monthName[t.month], t.year + 1900);
|
---|
919 | return buffer;
|
---|
920 | }
|
---|
921 |
|
---|
922 | UString formatTime(const GregorianDateTime &t, bool utc)
|
---|
923 | {
|
---|
924 | char buffer[100];
|
---|
925 | if (utc) {
|
---|
926 | snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT", t.hour, t.minute, t.second);
|
---|
927 | } else {
|
---|
928 | int offset = abs(gmtoffset(t));
|
---|
929 | char tzname[70];
|
---|
930 | struct tm gtm = t;
|
---|
931 | strftime(tzname, sizeof(tzname), "%Z", >m);
|
---|
932 |
|
---|
933 | if (tzname[0]) {
|
---|
934 | snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT%c%02d%02d (%s)",
|
---|
935 | t.hour, t.minute, t.second,
|
---|
936 | gmtoffset(t) < 0 ? '-' : '+', offset / (60*60), (offset / 60) % 60, tzname);
|
---|
937 | } else {
|
---|
938 | snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d GMT%c%02d%02d",
|
---|
939 | t.hour, t.minute, t.second,
|
---|
940 | gmtoffset(t) < 0 ? '-' : '+', offset / (60*60), (offset / 60) % 60);
|
---|
941 | }
|
---|
942 | }
|
---|
943 | return UString(buffer);
|
---|
944 | }
|
---|
945 |
|
---|
946 | } // namespace JSC
|
---|