Rubber-stamped by Brady Eidson.
[webbrowser.git] / JavaScriptCore / runtime / DateInstance.cpp
blob77a92be64b3b1319dedb1cf1be88c13724346d64
1 /*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18 * USA
22 #include "config.h"
23 #include "DateInstance.h"
25 #include "JSGlobalObject.h"
27 #include <math.h>
28 #include <wtf/DateMath.h>
29 #include <wtf/MathExtras.h>
31 using namespace WTF;
33 namespace JSC {
35 const ClassInfo DateInstance::info = {"Date", 0, 0, 0};
37 DateInstance::DateInstance(ExecState* exec, NonNullPassRefPtr<Structure> structure)
38 : JSWrapperObject(structure)
40 setInternalValue(jsNaN(exec));
43 DateInstance::DateInstance(ExecState* exec, double time)
44 : JSWrapperObject(exec->lexicalGlobalObject()->dateStructure())
46 setInternalValue(jsNumber(exec, timeClip(time)));
49 const GregorianDateTime* DateInstance::calculateGregorianDateTime(ExecState* exec) const
51 double milli = internalNumber();
52 if (isnan(milli))
53 return 0;
55 if (!m_data)
56 m_data = exec->globalData().dateInstanceCache.add(milli);
58 if (m_data->m_gregorianDateTimeCachedForMS != milli) {
59 msToGregorianDateTime(exec, milli, false, m_data->m_cachedGregorianDateTime);
60 m_data->m_gregorianDateTimeCachedForMS = milli;
62 return &m_data->m_cachedGregorianDateTime;
65 const GregorianDateTime* DateInstance::calculateGregorianDateTimeUTC(ExecState* exec) const
67 double milli = internalNumber();
68 if (isnan(milli))
69 return 0;
71 if (!m_data)
72 m_data = exec->globalData().dateInstanceCache.add(milli);
74 if (m_data->m_gregorianDateTimeUTCCachedForMS != milli) {
75 msToGregorianDateTime(exec, milli, true, m_data->m_cachedGregorianDateTimeUTC);
76 m_data->m_gregorianDateTimeUTCCachedForMS = milli;
78 return &m_data->m_cachedGregorianDateTimeUTC;
81 } // namespace JSC