1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
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 "JSDateMath.h"
26#include "JSGlobalObject.h"
27#include "JSCInlines.h"
28#include <math.h>
29#include <wtf/MathExtras.h>
30
31namespace JSC {
32
33const ClassInfo DateInstance::s_info = {"Date", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(DateInstance)};
34
35DateInstance::DateInstance(VM& vm, Structure* structure)
36 : Base(vm, structure)
37{
38}
39
40void DateInstance::finishCreation(VM& vm)
41{
42 Base::finishCreation(vm);
43 ASSERT(inherits(vm, info()));
44}
45
46void DateInstance::finishCreation(VM& vm, double time)
47{
48 Base::finishCreation(vm);
49 ASSERT(inherits(vm, info()));
50 m_internalNumber = timeClip(time);
51}
52
53void DateInstance::destroy(JSCell* cell)
54{
55 static_cast<DateInstance*>(cell)->DateInstance::~DateInstance();
56}
57
58const GregorianDateTime* DateInstance::calculateGregorianDateTime(VM& vm) const
59{
60 double milli = internalNumber();
61 if (std::isnan(milli))
62 return nullptr;
63
64 if (!m_data)
65 m_data = vm.dateInstanceCache.add(milli);
66
67 if (m_data->m_gregorianDateTimeCachedForMS != milli) {
68 msToGregorianDateTime(vm, milli, WTF::LocalTime, m_data->m_cachedGregorianDateTime);
69 m_data->m_gregorianDateTimeCachedForMS = milli;
70 }
71 return &m_data->m_cachedGregorianDateTime;
72}
73
74const GregorianDateTime* DateInstance::calculateGregorianDateTimeUTC(VM& vm) const
75{
76 double milli = internalNumber();
77 if (std::isnan(milli))
78 return nullptr;
79
80 if (!m_data)
81 m_data = vm.dateInstanceCache.add(milli);
82
83 if (m_data->m_gregorianDateTimeUTCCachedForMS != milli) {
84 msToGregorianDateTime(vm, milli, WTF::UTCTime, m_data->m_cachedGregorianDateTimeUTC);
85 m_data->m_gregorianDateTimeUTCCachedForMS = milli;
86 }
87 return &m_data->m_cachedGregorianDateTimeUTC;
88}
89
90} // namespace JSC
91