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