source: webkit/trunk/JavaScriptCore/kjs/DateMath.cpp@ 34854

Last change on this file since 34854 was 34581, checked in by Darin Adler, 17 years ago

JavaScriptCore:

2008-06-15 Darin Adler <Darin Adler>

  • API/JSBase.cpp:
  • API/JSCallbackConstructor.h:
  • API/JSCallbackFunction.cpp:
  • API/JSCallbackFunction.h:
  • API/JSCallbackObject.h:
  • API/JSCallbackObjectFunctions.h:
  • API/JSClassRef.h:
  • API/JSContextRef.cpp:
  • API/JSObjectRef.cpp:
  • API/JSStringRef.cpp:
  • API/JSStringRefCF.cpp:
  • API/JSValueRef.cpp:
  • GNUmakefile.am:
  • JavaScriptCore.pri:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • JavaScriptCoreSources.bkl:
  • VM/CodeBlock.cpp:
  • VM/CodeGenerator.cpp:
  • VM/ExceptionHelpers.cpp:
  • VM/ExceptionHelpers.h:
  • VM/JSPropertyNameIterator.cpp:
  • VM/JSPropertyNameIterator.h:
  • VM/Machine.cpp:
  • kjs/AllInOneFile.cpp:
  • kjs/DateMath.cpp:
  • kjs/DebuggerCallFrame.cpp:
  • kjs/ExecState.cpp:
  • kjs/JSActivation.cpp:
  • kjs/JSFunction.cpp: Copied from JavaScriptCore/kjs/function.cpp.
  • kjs/JSFunction.h: Copied from JavaScriptCore/kjs/function.h.
  • kjs/JSImmediate.cpp:
  • kjs/JSNotAnObject.h:
  • kjs/JSObject.cpp: Copied from JavaScriptCore/kjs/object.cpp.
  • kjs/JSObject.h: Copied from JavaScriptCore/kjs/object.h.
  • kjs/JSString.h: Copied from JavaScriptCore/kjs/internal.h.
  • kjs/JSValue.cpp: Copied from JavaScriptCore/kjs/value.cpp.
  • kjs/JSValue.h: Copied from JavaScriptCore/kjs/value.h.
  • kjs/JSVariableObject.h:
  • kjs/JSWrapperObject.h:
  • kjs/Shell.cpp:
  • kjs/SymbolTable.h:
  • kjs/array_instance.h:
  • kjs/collector.cpp:
  • kjs/date_object.cpp:
  • kjs/date_object.h:
  • kjs/error_object.cpp:
  • kjs/function.cpp: Removed.
  • kjs/function.h: Removed.
  • kjs/function_object.cpp:
  • kjs/function_object.h:
  • kjs/grammar.y:
  • kjs/internal.cpp:
  • kjs/internal.h: Removed.
  • kjs/lexer.cpp:
  • kjs/list.h:
  • kjs/lookup.h:
  • kjs/nodes.h:
  • kjs/object.cpp: Removed.
  • kjs/object.h: Removed.
  • kjs/object_object.h:
  • kjs/operations.cpp:
  • kjs/property_map.cpp:
  • kjs/property_slot.cpp:
  • kjs/property_slot.h:
  • kjs/protect.h:
  • kjs/regexp_object.cpp:
  • kjs/scope_chain.cpp:
  • kjs/string_object.h:
  • kjs/ustring.cpp:
  • kjs/value.cpp: Removed.
  • kjs/value.h: Removed.
  • profiler/Profile.cpp:
  • profiler/Profiler.cpp:

JavaScriptGlue:

2008-06-15 Darin Adler <Darin Adler>

  • ForwardingHeaders/kjs/JSValue.h: Copied from JavaScriptGlue/ForwardingHeaders/kjs/value.h.
  • ForwardingHeaders/kjs/value.h: Removed.
  • JSUtils.h:

WebCore:

2008-06-15 Darin Adler <Darin Adler>

  • ForwardingHeaders/kjs/JSFunction.h: Copied from WebCore/ForwardingHeaders/kjs/function.h.
  • ForwardingHeaders/kjs/JSObject.h: Copied from WebCore/ForwardingHeaders/kjs/object.h.
  • ForwardingHeaders/kjs/JSString.h: Copied from WebCore/ForwardingHeaders/kjs/internal.h.
  • ForwardingHeaders/kjs/JSValue.h: Copied from WebCore/ForwardingHeaders/kjs/value.h.
  • ForwardingHeaders/kjs/function.h: Removed.
  • ForwardingHeaders/kjs/internal.h: Removed.
  • ForwardingHeaders/kjs/object.h: Removed.
  • ForwardingHeaders/kjs/value.h: Removed.
  • WebCore.vcproj/WebCore.vcproj:
  • bindings/js/JSCustomSQLStatementCallback.h:
  • bindings/js/JSCustomSQLStatementErrorCallback.h:
  • bindings/js/JSCustomSQLTransactionErrorCallback.h:
  • bindings/js/JSCustomVoidCallback.h:
  • bindings/js/JSDOMBinding.h:
  • bindings/js/JSDOMWindowCustom.cpp:
  • bindings/js/JSDOMWindowShell.cpp:
  • bindings/js/JSQuarantinedObjectWrapper.h:
  • bridge/objc/objc_runtime.h:
  • bridge/objc/objc_utility.h:
  • bridge/qt/qt_runtime.cpp:
  • bridge/runtime.h:
  • bridge/runtime_array.h:
  • bridge/runtime_method.h:
  • bridge/runtime_object.h:
  • bridge/testbindings.cpp:
  • bridge/testbindings.mm:
  • bridge/testqtbindings.cpp:
  • loader/FrameLoader.cpp:
  • page/JavaScriptCallFrame.cpp:
  • page/JavaScriptProfile.cpp:
  • page/JavaScriptProfileNode.cpp:
  • platform/graphics/gtk/VideoSinkGStreamer.h:
  • plugins/PluginView.cpp:
  • plugins/gtk/PluginViewGtk.cpp:
  • plugins/qt/PluginViewQt.cpp:
  • plugins/win/PluginViewWin.cpp:

WebKit/gtk:

2008-06-15 Darin Adler <Darin Adler>

  • WebCoreSupport/ContextMenuClientGtk.cpp:
  • webkit/webkitnetworkrequest.h:
  • webkit/webkitwebbackforwardlist.h:
  • webkit/webkitwebframe.h:
  • webkit/webkitwebhistoryitem.h:
  • webkit/webkitwebsettings.h:

WebKit/mac:

2008-06-15 Darin Adler <Darin Adler>

  • ForwardingHeaders/kjs/JSFunction.h: Copied from WebKit/mac/ForwardingHeaders/kjs/function.h.
  • ForwardingHeaders/kjs/JSObject.h: Copied from WebKit/mac/ForwardingHeaders/kjs/object.h.
  • ForwardingHeaders/kjs/JSString.h: Copied from WebKit/mac/ForwardingHeaders/kjs/internal.h.
  • ForwardingHeaders/kjs/JSValue.h: Copied from WebKit/mac/ForwardingHeaders/kjs/value.h.
  • ForwardingHeaders/kjs/function.h: Removed.
  • ForwardingHeaders/kjs/internal.h: Removed.
  • ForwardingHeaders/kjs/object.h: Removed.
  • ForwardingHeaders/kjs/value.h: Removed.
  • WebView/WebScriptDebugDelegate.mm:

WebKit/qt:

2008-06-15 Darin Adler <Darin Adler>

  • Api/qwebframe.cpp:

WebKit/win:

2008-06-15 Darin Adler <Darin Adler>

  • WebView.cpp:

WebKit/wx:

2008-06-15 Darin Adler <Darin Adler>

  • WebView.cpp:

WebKitTools:

2008-06-15 Darin Adler <Darin Adler>

  • Scripts/do-file-rename: Updated for the latest round of renaming.
  • Scripts/do-webcore-rename: Tweaked and reorganized a bit.
  • Property svn:eol-style set to native
File size: 16.0 KB
Line 
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 <math.h>
46#include <stdint.h>
47#include <JSValue.h>
48
49#include <wtf/Assertions.h>
50
51#if PLATFORM(DARWIN)
52#include <notify.h>
53#endif
54
55#if HAVE(SYS_TIME_H)
56#include <sys/time.h>
57#endif
58
59#if HAVE(SYS_TIMEB_H)
60#include <sys/timeb.h>
61#endif
62
63namespace KJS {
64
65/* Constants */
66
67static const double minutesPerDay = 24.0 * 60.0;
68static const double secondsPerDay = 24.0 * 60.0 * 60.0;
69static const double secondsPerYear = 24.0 * 60.0 * 60.0 * 365.0;
70
71static const double usecPerSec = 1000000.0;
72
73static const double maxUnixTime = 2145859200.0; // 12/31/2037
74
75// Day of year for the first day of each month, where index 0 is January, and day 0 is January 1.
76// First for non-leap years, then for leap years.
77static const int firstDayOfMonth[2][12] = {
78 {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
79 {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
80};
81
82static inline bool isLeapYear(int year)
83{
84 if (year % 4 != 0)
85 return false;
86 if (year % 400 == 0)
87 return true;
88 if (year % 100 == 0)
89 return false;
90 return true;
91}
92
93static inline int daysInYear(int year)
94{
95 return 365 + isLeapYear(year);
96}
97
98static inline double daysFrom1970ToYear(int year)
99{
100 // The Gregorian Calendar rules for leap years:
101 // Every fourth year is a leap year. 2004, 2008, and 2012 are leap years.
102 // However, every hundredth year is not a leap year. 1900 and 2100 are not leap years.
103 // Every four hundred years, there's a leap year after all. 2000 and 2400 are leap years.
104
105 static const int leapDaysBefore1971By4Rule = 1970 / 4;
106 static const int excludedLeapDaysBefore1971By100Rule = 1970 / 100;
107 static const int leapDaysBefore1971By400Rule = 1970 / 400;
108
109 const double yearMinusOne = year - 1;
110 const double yearsToAddBy4Rule = floor(yearMinusOne / 4.0) - leapDaysBefore1971By4Rule;
111 const double yearsToExcludeBy100Rule = floor(yearMinusOne / 100.0) - excludedLeapDaysBefore1971By100Rule;
112 const double yearsToAddBy400Rule = floor(yearMinusOne / 400.0) - leapDaysBefore1971By400Rule;
113
114 return 365.0 * (year - 1970) + yearsToAddBy4Rule - yearsToExcludeBy100Rule + yearsToAddBy400Rule;
115}
116
117static inline double msToDays(double ms)
118{
119 return floor(ms / msPerDay);
120}
121
122static inline int msToYear(double ms)
123{
124 int approxYear = static_cast<int>(floor(ms / (msPerDay * 365.2425)) + 1970);
125 double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear);
126 if (msFromApproxYearTo1970 > ms)
127 return approxYear - 1;
128 if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms)
129 return approxYear + 1;
130 return approxYear;
131}
132
133static inline int dayInYear(double ms, int year)
134{
135 return static_cast<int>(msToDays(ms) - daysFrom1970ToYear(year));
136}
137
138static inline double msToMilliseconds(double ms)
139{
140 double result = fmod(ms, msPerDay);
141 if (result < 0)
142 result += msPerDay;
143 return result;
144}
145
146// 0: Sunday, 1: Monday, etc.
147static inline int msToWeekDay(double ms)
148{
149 int wd = (static_cast<int>(msToDays(ms)) + 4) % 7;
150 if (wd < 0)
151 wd += 7;
152 return wd;
153}
154
155static inline int msToSeconds(double ms)
156{
157 double result = fmod(floor(ms / msPerSecond), secondsPerMinute);
158 if (result < 0)
159 result += secondsPerMinute;
160 return static_cast<int>(result);
161}
162
163static inline int msToMinutes(double ms)
164{
165 double result = fmod(floor(ms / msPerMinute), minutesPerHour);
166 if (result < 0)
167 result += minutesPerHour;
168 return static_cast<int>(result);
169}
170
171static inline int msToHours(double ms)
172{
173 double result = fmod(floor(ms/msPerHour), hoursPerDay);
174 if (result < 0)
175 result += hoursPerDay;
176 return static_cast<int>(result);
177}
178
179static inline int monthFromDayInYear(int dayInYear, bool leapYear)
180{
181 const int d = dayInYear;
182 int step;
183
184 if (d < (step = 31))
185 return 0;
186 step += (leapYear ? 29 : 28);
187 if (d < step)
188 return 1;
189 if (d < (step += 31))
190 return 2;
191 if (d < (step += 30))
192 return 3;
193 if (d < (step += 31))
194 return 4;
195 if (d < (step += 30))
196 return 5;
197 if (d < (step += 31))
198 return 6;
199 if (d < (step += 31))
200 return 7;
201 if (d < (step += 30))
202 return 8;
203 if (d < (step += 31))
204 return 9;
205 if (d < (step += 30))
206 return 10;
207 return 11;
208}
209
210static inline bool checkMonth(int dayInYear, int& startDayOfThisMonth, int& startDayOfNextMonth, int daysInThisMonth)
211{
212 startDayOfThisMonth = startDayOfNextMonth;
213 startDayOfNextMonth += daysInThisMonth;
214 return (dayInYear <= startDayOfNextMonth);
215}
216
217static inline int dayInMonthFromDayInYear(int dayInYear, bool leapYear)
218{
219 const int d = dayInYear;
220 int step;
221 int next = 30;
222
223 if (d <= next)
224 return d + 1;
225 const int daysInFeb = (leapYear ? 29 : 28);
226 if (checkMonth(d, step, next, daysInFeb))
227 return d - step;
228 if (checkMonth(d, step, next, 31))
229 return d - step;
230 if (checkMonth(d, step, next, 30))
231 return d - step;
232 if (checkMonth(d, step, next, 31))
233 return d - step;
234 if (checkMonth(d, step, next, 30))
235 return d - step;
236 if (checkMonth(d, step, next, 31))
237 return d - step;
238 if (checkMonth(d, step, next, 31))
239 return d - step;
240 if (checkMonth(d, step, next, 30))
241 return d - step;
242 if (checkMonth(d, step, next, 31))
243 return d - step;
244 if (checkMonth(d, step, next, 30))
245 return d - step;
246 step = next;
247 return d - step;
248}
249
250static inline int monthToDayInYear(int month, bool isLeapYear)
251{
252 return firstDayOfMonth[isLeapYear][month];
253}
254
255static inline double timeToMS(double hour, double min, double sec, double ms)
256{
257 return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms);
258}
259
260static int dateToDayInYear(int year, int month, int day)
261{
262 year += month / 12;
263
264 month %= 12;
265 if (month < 0) {
266 month += 12;
267 --year;
268 }
269
270 int yearday = static_cast<int>(floor(daysFrom1970ToYear(year)));
271 int monthday = monthToDayInYear(month, isLeapYear(year));
272
273 return yearday + monthday + day - 1;
274}
275
276double getCurrentUTCTime()
277{
278 return floor(getCurrentUTCTimeWithMicroseconds());
279}
280
281double getCurrentUTCTimeWithMicroseconds()
282{
283#if PLATFORM(WIN_OS)
284 // FIXME: the implementation for Windows is only millisecond resolution.
285#if COMPILER(BORLAND)
286 struct timeb timebuffer;
287 ftime(&timebuffer);
288#else
289 struct _timeb timebuffer;
290 _ftime(&timebuffer);
291#endif
292 double utc = timebuffer.time * msPerSecond + timebuffer.millitm;
293#else
294 struct timeval tv;
295 gettimeofday(&tv, 0);
296 double utc = tv.tv_sec * msPerSecond + tv.tv_usec / 1000.0;
297#endif
298 return utc;
299}
300
301void getLocalTime(const time_t* localTime, struct tm* localTM)
302{
303#if PLATFORM(QT)
304#if USE(MULTIPLE_THREADS)
305#error Mulitple threads are currently not supported in the Qt/mingw build
306#endif
307 *localTM = *localtime(localTime);
308#elif PLATFORM(WIN_OS)
309 #if COMPILER(MSVC7)
310 *localTM = *localtime(localTime);
311 #else
312 localtime_s(localTM, localTime);
313 #endif
314#else
315 localtime_r(localTime, localTM);
316#endif
317}
318
319// There is a hard limit at 2038 that we currently do not have a workaround
320// for (rdar://problem/5052975).
321static inline int maximumYearForDST()
322{
323 return 2037;
324}
325
326static inline int mimimumYearForDST()
327{
328 // Because of the 2038 issue (see maximumYearForDST) if the current year is
329 // greater than the max year minus 27 (2010), we want to use the max year
330 // minus 27 instead, to ensure there is a range of 28 years that all years
331 // can map to.
332 return std::min(msToYear(getCurrentUTCTime()), maximumYearForDST() - 27) ;
333}
334
335/*
336 * Find an equivalent year for the one given, where equivalence is deterined by
337 * the two years having the same leapness and the first day of the year, falling
338 * on the same day of the week.
339 *
340 * This function returns a year between this current year and 2037, however this
341 * function will potentially return incorrect results if the current year is after
342 * 2010, (rdar://problem/5052975), if the year passed in is before 1900 or after
343 * 2100, (rdar://problem/5055038).
344 */
345int equivalentYearForDST(int year)
346{
347 // It is ok if the cached year is not the current year as long as the rules
348 // for DST did not change between the two years; if they did the app would need
349 // to be restarted.
350 static int minYear = mimimumYearForDST();
351 int maxYear = maximumYearForDST();
352
353 int difference;
354 if (year > maxYear)
355 difference = minYear - year;
356 else if (year < minYear)
357 difference = maxYear - year;
358 else
359 return year;
360
361 int quotient = difference / 28;
362 int product = (quotient) * 28;
363
364 year += product;
365 ASSERT((year >= minYear && year <= maxYear) || (product - year == static_cast<int>(NaN)));
366 return year;
367}
368
369static int32_t calculateUTCOffset()
370{
371 tm localt;
372 memset(&localt, 0, sizeof(localt));
373
374 // get the difference between this time zone and UTC on Jan 01, 2000 12:00:00 AM
375 localt.tm_mday = 1;
376 localt.tm_year = 100;
377 time_t utcOffset = 946684800 - mktime(&localt);
378
379 return static_cast<int32_t>(utcOffset * 1000);
380}
381
382#if PLATFORM(DARWIN)
383static 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.
384static bool s_haveCachedUTCOffset;
385static int s_notificationToken;
386#endif
387
388/*
389 * Get the difference in milliseconds between this time zone and UTC (GMT)
390 * NOT including DST.
391 */
392double getUTCOffset()
393{
394#if PLATFORM(DARWIN)
395 if (s_haveCachedUTCOffset) {
396 int notified;
397 uint32_t status = notify_check(s_notificationToken, &notified);
398 if (status == NOTIFY_STATUS_OK && !notified)
399 return s_cachedUTCOffset;
400 }
401#endif
402
403 int32_t utcOffset = calculateUTCOffset();
404
405#if PLATFORM(DARWIN)
406 // Theoretically, it is possible that several threads will be executing this code at once, in which case we will have a race condition,
407 // and a newer value may be overwritten. In practice, time zones don't change that often.
408 s_cachedUTCOffset = utcOffset;
409#endif
410
411 return utcOffset;
412}
413
414/*
415 * Get the DST offset for the time passed in. Takes
416 * seconds (not milliseconds) and cannot handle dates before 1970
417 * on some OS'
418 */
419static double getDSTOffsetSimple(double localTimeSeconds, double utcOffset)
420{
421 if (localTimeSeconds > maxUnixTime)
422 localTimeSeconds = maxUnixTime;
423 else if (localTimeSeconds < 0) // Go ahead a day to make localtime work (does not work with 0)
424 localTimeSeconds += secondsPerDay;
425
426 //input is UTC so we have to shift back to local time to determine DST thus the + getUTCOffset()
427 double offsetTime = (localTimeSeconds * msPerSecond) + utcOffset;
428
429 // Offset from UTC but doesn't include DST obviously
430 int offsetHour = msToHours(offsetTime);
431 int offsetMinute = msToMinutes(offsetTime);
432
433 // FIXME: time_t has a potential problem in 2038
434 time_t localTime = static_cast<time_t>(localTimeSeconds);
435
436 tm localTM;
437 getLocalTime(&localTime, &localTM);
438
439 double diff = ((localTM.tm_hour - offsetHour) * secondsPerHour) + ((localTM.tm_min - offsetMinute) * 60);
440
441 if (diff < 0)
442 diff += secondsPerDay;
443
444 return (diff * msPerSecond);
445}
446
447// Get the DST offset, given a time in UTC
448static double getDSTOffset(double ms, double utcOffset)
449{
450 // On Mac OS X, the call to localtime (see getDSTOffsetSimple) will return historically accurate
451 // DST information (e.g. New Zealand did not have DST from 1946 to 1974) however the JavaScript
452 // standard explicitly dictates that historical information should not be considered when
453 // determining DST. For this reason we shift away from years that localtime can handle but would
454 // return historically accurate information.
455 int year = msToYear(ms);
456 int equivalentYear = equivalentYearForDST(year);
457 if (year != equivalentYear) {
458 bool leapYear = isLeapYear(year);
459 int dayInYearLocal = dayInYear(ms, year);
460 int dayInMonth = dayInMonthFromDayInYear(dayInYearLocal, leapYear);
461 int month = monthFromDayInYear(dayInYearLocal, leapYear);
462 int day = dateToDayInYear(equivalentYear, month, dayInMonth);
463 ms = (day * msPerDay) + msToMilliseconds(ms);
464 }
465
466 return getDSTOffsetSimple(ms / msPerSecond, utcOffset);
467}
468
469double gregorianDateTimeToMS(const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
470{
471 int day = dateToDayInYear(t.year + 1900, t.month, t.monthDay);
472 double ms = timeToMS(t.hour, t.minute, t.second, milliSeconds);
473 double result = (day * msPerDay) + ms;
474
475 if (!inputIsUTC) { // convert to UTC
476 double utcOffset = getUTCOffset();
477 result -= utcOffset;
478 result -= getDSTOffset(result, utcOffset);
479 }
480
481 return result;
482}
483
484void msToGregorianDateTime(double ms, bool outputIsUTC, GregorianDateTime& tm)
485{
486 // input is UTC
487 double dstOff = 0.0;
488 const double utcOff = getUTCOffset();
489
490 if (!outputIsUTC) { // convert to local time
491 dstOff = getDSTOffset(ms, utcOff);
492 ms += dstOff + utcOff;
493 }
494
495 const int year = msToYear(ms);
496 tm.second = msToSeconds(ms);
497 tm.minute = msToMinutes(ms);
498 tm.hour = msToHours(ms);
499 tm.weekDay = msToWeekDay(ms);
500 tm.yearDay = dayInYear(ms, year);
501 tm.monthDay = dayInMonthFromDayInYear(tm.yearDay, isLeapYear(year));
502 tm.month = monthFromDayInYear(tm.yearDay, isLeapYear(year));
503 tm.year = year - 1900;
504 tm.isDST = dstOff != 0.0;
505
506 tm.utcOffset = static_cast<long>((dstOff + utcOff) / msPerSecond);
507 tm.timeZone = NULL;
508}
509
510void initDateMath()
511{
512#ifndef NDEBUG
513 static bool alreadyInitialized;
514 ASSERT(!alreadyInitialized++);
515#endif
516
517 equivalentYearForDST(2000); // Need to call once to initialize a static used in this function.
518#if PLATFORM(DARWIN)
519 // Register for a notification whenever the time zone changes.
520 uint32_t status = notify_register_check("com.apple.system.timezone", &s_notificationToken);
521 if (status == NOTIFY_STATUS_OK) {
522 s_cachedUTCOffset = calculateUTCOffset();
523 s_haveCachedUTCOffset = true;
524 }
525#endif
526}
527
528} // namespace KJS
Note: See TracBrowser for help on using the repository browser.