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