DateInstance.cpp 2.77 KB
Newer Older
1 2
/*
 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
darin@apple.com's avatar
darin@apple.com committed
3
 *  Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
17 18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 *  USA
19
 *
20 21
 */

22
#include "config.h"
23
#include "DateInstance.h"
darin@apple.com's avatar
darin@apple.com committed
24

25
#include "JSDateMath.h"
oliver@apple.com's avatar
oliver@apple.com committed
26
#include "JSGlobalObject.h"
27
#include "Operations.h"
darin@apple.com's avatar
darin@apple.com committed
28 29
#include <math.h>
#include <wtf/MathExtras.h>
darin's avatar
darin committed
30

31 32
using namespace WTF;

33
namespace JSC {
darin's avatar
darin committed
34

35
const ClassInfo DateInstance::s_info = {"Date", &JSWrapperObject::s_info, 0, 0, CREATE_METHOD_TABLE(DateInstance)};
36

37 38
DateInstance::DateInstance(VM& vm, Structure* structure)
    : JSWrapperObject(vm, structure)
39
{
40 41
}

ggaren@apple.com's avatar
ggaren@apple.com committed
42
void DateInstance::finishCreation(VM& vm)
43
{
ggaren@apple.com's avatar
ggaren@apple.com committed
44
    Base::finishCreation(vm);
45
    ASSERT(inherits(info()));
ggaren@apple.com's avatar
ggaren@apple.com committed
46
    setInternalValue(vm, jsNaN());
47 48
}

ggaren@apple.com's avatar
ggaren@apple.com committed
49
void DateInstance::finishCreation(VM& vm, double time)
50
{
ggaren@apple.com's avatar
ggaren@apple.com committed
51
    Base::finishCreation(vm);
52
    ASSERT(inherits(info()));
ggaren@apple.com's avatar
ggaren@apple.com committed
53
    setInternalValue(vm, jsNumber(timeClip(time)));
54 55
}

56 57
void DateInstance::destroy(JSCell* cell)
{
58
    static_cast<DateInstance*>(cell)->DateInstance::~DateInstance();
59 60
}

61
const GregorianDateTime* DateInstance::calculateGregorianDateTime(ExecState* exec) const
ap@webkit.org's avatar
ap@webkit.org committed
62
{
63
    double milli = internalNumber();
64
    if (std::isnan(milli))
65
        return 0;
66

67
    VM& vm = exec->vm();
68
    if (!m_data)
69
        m_data = vm.dateInstanceCache.add(milli);
70

71
    if (m_data->m_gregorianDateTimeCachedForMS != milli) {
72
        msToGregorianDateTime(vm, milli, false, m_data->m_cachedGregorianDateTime);
73 74 75
        m_data->m_gregorianDateTimeCachedForMS = milli;
    }
    return &m_data->m_cachedGregorianDateTime;
ap@webkit.org's avatar
ap@webkit.org committed
76 77
}

78 79 80
const GregorianDateTime* DateInstance::calculateGregorianDateTimeUTC(ExecState* exec) const
{
    double milli = internalNumber();
81
    if (std::isnan(milli))
82 83
        return 0;

84
    VM& vm = exec->vm();
85
    if (!m_data)
86
        m_data = vm.dateInstanceCache.add(milli);
87 88

    if (m_data->m_gregorianDateTimeUTCCachedForMS != milli) {
89
        msToGregorianDateTime(vm, milli, true, m_data->m_cachedGregorianDateTimeUTC);
90 91 92 93 94
        m_data->m_gregorianDateTimeUTCCachedForMS = milli;
    }
    return &m_data->m_cachedGregorianDateTimeUTC;
}

95
} // namespace JSC