source: webkit/trunk/JavaScriptCore/runtime/DateInstance.cpp@ 49323

Last change on this file since 49323 was 49217, checked in by [email protected], 16 years ago

Windows build fix

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
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 "JSGlobalObject.h"
26
27#include <math.h>
28#include <wtf/DateMath.h>
29#include <wtf/MathExtras.h>
30
31using namespace WTF;
32
33namespace JSC {
34
35struct DateInstance::Cache {
36 double m_gregorianDateTimeCachedForMS;
37 GregorianDateTime m_cachedGregorianDateTime;
38 double m_gregorianDateTimeUTCCachedForMS;
39 GregorianDateTime m_cachedGregorianDateTimeUTC;
40};
41
42const ClassInfo DateInstance::info = {"Date", 0, 0, 0};
43
44DateInstance::DateInstance(NonNullPassRefPtr<Structure> structure)
45 : JSWrapperObject(structure)
46 , m_cache(0)
47{
48}
49
50DateInstance::DateInstance(ExecState* exec, double time)
51 : JSWrapperObject(exec->lexicalGlobalObject()->dateStructure())
52 , m_cache(0)
53{
54 setInternalValue(jsNumber(exec, timeClip(time)));
55}
56
57DateInstance::~DateInstance()
58{
59 delete m_cache;
60}
61
62void DateInstance::msToGregorianDateTime(double milli, bool outputIsUTC, GregorianDateTime& t) const
63{
64 if (!m_cache) {
65 m_cache = new Cache;
66 m_cache->m_gregorianDateTimeCachedForMS = NaN;
67 m_cache->m_gregorianDateTimeUTCCachedForMS = NaN;
68 }
69
70 if (outputIsUTC) {
71 if (m_cache->m_gregorianDateTimeUTCCachedForMS != milli) {
72 WTF::msToGregorianDateTime(milli, true, m_cache->m_cachedGregorianDateTimeUTC);
73 m_cache->m_gregorianDateTimeUTCCachedForMS = milli;
74 }
75 t.copyFrom(m_cache->m_cachedGregorianDateTimeUTC);
76 } else {
77 if (m_cache->m_gregorianDateTimeCachedForMS != milli) {
78 WTF::msToGregorianDateTime(milli, false, m_cache->m_cachedGregorianDateTime);
79 m_cache->m_gregorianDateTimeCachedForMS = milli;
80 }
81 t.copyFrom(m_cache->m_cachedGregorianDateTime);
82 }
83}
84
85bool DateInstance::getTime(GregorianDateTime& t, int& offset) const
86{
87 double milli = internalNumber();
88 if (isnan(milli))
89 return false;
90
91 msToGregorianDateTime(milli, false, t);
92 offset = gmtoffset(t);
93 return true;
94}
95
96bool DateInstance::getUTCTime(GregorianDateTime& t) const
97{
98 double milli = internalNumber();
99 if (isnan(milli))
100 return false;
101
102 msToGregorianDateTime(milli, true, t);
103 return true;
104}
105
106bool DateInstance::getTime(double& milli, int& offset) const
107{
108 milli = internalNumber();
109 if (isnan(milli))
110 return false;
111
112 GregorianDateTime t;
113 msToGregorianDateTime(milli, false, t);
114 offset = gmtoffset(t);
115 return true;
116}
117
118bool DateInstance::getUTCTime(double& milli) const
119{
120 milli = internalNumber();
121 if (isnan(milli))
122 return false;
123
124 return true;
125}
126
127} // namespace JSC
Note: See TracBrowser for help on using the repository browser.