DateInstance.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "config.h"
  22. #include "DateInstance.h"
  23. #include "JSDateMath.h"
  24. #include "JSGlobalObject.h"
  25. #include "Operations.h"
  26. #include <math.h>
  27. #include <wtf/MathExtras.h>
  28. using namespace WTF;
  29. namespace JSC {
  30. const ClassInfo DateInstance::s_info = {"Date", &JSWrapperObject::s_info, 0, 0, CREATE_METHOD_TABLE(DateInstance)};
  31. DateInstance::DateInstance(ExecState* exec, Structure* structure)
  32. : JSWrapperObject(exec->vm(), structure)
  33. {
  34. }
  35. void DateInstance::finishCreation(VM& vm)
  36. {
  37. Base::finishCreation(vm);
  38. ASSERT(inherits(&s_info));
  39. setInternalValue(vm, jsNaN());
  40. }
  41. void DateInstance::finishCreation(VM& vm, double time)
  42. {
  43. Base::finishCreation(vm);
  44. ASSERT(inherits(&s_info));
  45. setInternalValue(vm, jsNumber(timeClip(time)));
  46. }
  47. void DateInstance::destroy(JSCell* cell)
  48. {
  49. static_cast<DateInstance*>(cell)->DateInstance::~DateInstance();
  50. }
  51. const GregorianDateTime* DateInstance::calculateGregorianDateTime(ExecState* exec) const
  52. {
  53. double milli = internalNumber();
  54. if (std::isnan(milli))
  55. return 0;
  56. if (!m_data)
  57. m_data = exec->vm().dateInstanceCache.add(milli);
  58. if (m_data->m_gregorianDateTimeCachedForMS != milli) {
  59. msToGregorianDateTime(exec, milli, false, m_data->m_cachedGregorianDateTime);
  60. m_data->m_gregorianDateTimeCachedForMS = milli;
  61. }
  62. return &m_data->m_cachedGregorianDateTime;
  63. }
  64. const GregorianDateTime* DateInstance::calculateGregorianDateTimeUTC(ExecState* exec) const
  65. {
  66. double milli = internalNumber();
  67. if (std::isnan(milli))
  68. return 0;
  69. if (!m_data)
  70. m_data = exec->vm().dateInstanceCache.add(milli);
  71. if (m_data->m_gregorianDateTimeUTCCachedForMS != milli) {
  72. msToGregorianDateTime(exec, milli, true, m_data->m_cachedGregorianDateTimeUTC);
  73. m_data->m_gregorianDateTimeUTCCachedForMS = milli;
  74. }
  75. return &m_data->m_cachedGregorianDateTimeUTC;
  76. }
  77. } // namespace JSC