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