]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/DateInstance.cpp
62791aecb28627f6f7b0668b5f128e18da04f505
[apple/javascriptcore.git] / 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 <math.h>
26 #include <wtf/DateMath.h>
27 #include <wtf/MathExtras.h>
28
29 using namespace WTF;
30
31 namespace JSC {
32
33 struct DateInstance::Cache {
34 double m_gregorianDateTimeCachedForMS;
35 GregorianDateTime m_cachedGregorianDateTime;
36 double m_gregorianDateTimeUTCCachedForMS;
37 GregorianDateTime m_cachedGregorianDateTimeUTC;
38 };
39
40 const ClassInfo DateInstance::info = {"Date", 0, 0, 0};
41
42 DateInstance::DateInstance(PassRefPtr<Structure> structure)
43 : JSWrapperObject(structure)
44 , m_cache(0)
45 {
46 }
47
48 DateInstance::~DateInstance()
49 {
50 delete m_cache;
51 }
52
53 void DateInstance::msToGregorianDateTime(double milli, bool outputIsUTC, GregorianDateTime& t) const
54 {
55 if (!m_cache) {
56 m_cache = new Cache;
57 m_cache->m_gregorianDateTimeCachedForMS = NaN;
58 m_cache->m_gregorianDateTimeUTCCachedForMS = NaN;
59 }
60
61 if (outputIsUTC) {
62 if (m_cache->m_gregorianDateTimeUTCCachedForMS != milli) {
63 WTF::msToGregorianDateTime(milli, true, m_cache->m_cachedGregorianDateTimeUTC);
64 m_cache->m_gregorianDateTimeUTCCachedForMS = milli;
65 }
66 t.copyFrom(m_cache->m_cachedGregorianDateTimeUTC);
67 } else {
68 if (m_cache->m_gregorianDateTimeCachedForMS != milli) {
69 WTF::msToGregorianDateTime(milli, false, m_cache->m_cachedGregorianDateTime);
70 m_cache->m_gregorianDateTimeCachedForMS = milli;
71 }
72 t.copyFrom(m_cache->m_cachedGregorianDateTime);
73 }
74 }
75
76 bool DateInstance::getTime(GregorianDateTime& t, int& offset) const
77 {
78 double milli = internalNumber();
79 if (isnan(milli))
80 return false;
81
82 msToGregorianDateTime(milli, false, t);
83 offset = gmtoffset(t);
84 return true;
85 }
86
87 bool DateInstance::getUTCTime(GregorianDateTime& t) const
88 {
89 double milli = internalNumber();
90 if (isnan(milli))
91 return false;
92
93 msToGregorianDateTime(milli, true, t);
94 return true;
95 }
96
97 bool DateInstance::getTime(double& milli, int& offset) const
98 {
99 milli = internalNumber();
100 if (isnan(milli))
101 return false;
102
103 GregorianDateTime t;
104 msToGregorianDateTime(milli, false, t);
105 offset = gmtoffset(t);
106 return true;
107 }
108
109 bool DateInstance::getUTCTime(double& milli) const
110 {
111 milli = internalNumber();
112 if (isnan(milli))
113 return false;
114
115 return true;
116 }
117
118 } // namespace JSC