initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / DateInstance.cpp
1 /*
2  *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4  *
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.
9  *
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.
14  *
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
19  *
20  */
21
22 #include "config.h"
23 #include "DateInstance.h"
24
25 #include "JSGlobalObject.h"
26
27 #include <math.h>
28 #include <wtf/DateMath.h>
29 #include <wtf/MathExtras.h>
30
31 using namespace WTF;
32
33 namespace JSC {
34
35 const ClassInfo DateInstance::s_info = {"Date", &JSWrapperObject::s_info, 0, 0};
36
37 DateInstance::DateInstance(ExecState* exec, Structure* structure)
38     : JSWrapperObject(exec->globalData(), structure)
39 {
40 }
41
42 void DateInstance::finishCreation(JSGlobalData& globalData)
43 {
44     Base::finishCreation(globalData);
45     ASSERT(inherits(&s_info));
46     setInternalValue(globalData, jsNaN());
47 }
48
49 void DateInstance::finishCreation(JSGlobalData& globalData, double time)
50 {
51     Base::finishCreation(globalData);
52     ASSERT(inherits(&s_info));
53     setInternalValue(globalData, jsNumber(timeClip(time)));
54 }
55
56 const GregorianDateTime* DateInstance::calculateGregorianDateTime(ExecState* exec) const
57 {
58     double milli = internalNumber();
59     if (isnan(milli))
60         return 0;
61
62     if (!m_data)
63         m_data = exec->globalData().dateInstanceCache.add(milli);
64
65     if (m_data->m_gregorianDateTimeCachedForMS != milli) {
66         msToGregorianDateTime(exec, milli, false, m_data->m_cachedGregorianDateTime);
67         m_data->m_gregorianDateTimeCachedForMS = milli;
68     }
69     return &m_data->m_cachedGregorianDateTime;
70 }
71
72 const GregorianDateTime* DateInstance::calculateGregorianDateTimeUTC(ExecState* exec) const
73 {
74     double milli = internalNumber();
75     if (isnan(milli))
76         return 0;
77
78     if (!m_data)
79         m_data = exec->globalData().dateInstanceCache.add(milli);
80
81     if (m_data->m_gregorianDateTimeUTCCachedForMS != milli) {
82         msToGregorianDateTime(exec, milli, true, m_data->m_cachedGregorianDateTimeUTC);
83         m_data->m_gregorianDateTimeUTCCachedForMS = milli;
84     }
85     return &m_data->m_cachedGregorianDateTimeUTC;
86 }
87
88 } // namespace JSC