JSDateMath.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2006, 2007, 2012 Apple Inc. All rights reserved.
  4. * Copyright (C) 2009 Google Inc. All rights reserved.
  5. * Copyright (C) 2007-2009 Torch Mobile, Inc.
  6. * Copyright (C) 2010 &yet, LLC. (nate@andyet.net)
  7. *
  8. * The Original Code is Mozilla Communicator client code, released
  9. * March 31, 1998.
  10. *
  11. * The Initial Developer of the Original Code is
  12. * Netscape Communications Corporation.
  13. * Portions created by the Initial Developer are Copyright (C) 1998
  14. * the Initial Developer. All Rights Reserved.
  15. *
  16. * This library is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU Lesser General Public
  18. * License as published by the Free Software Foundation; either
  19. * version 2.1 of the License, or (at your option) any later version.
  20. *
  21. * This library is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  24. * Lesser General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Lesser General Public
  27. * License along with this library; if not, write to the Free Software
  28. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  29. *
  30. * Alternatively, the contents of this file may be used under the terms
  31. * of either the Mozilla Public License Version 1.1, found at
  32. * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
  33. * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
  34. * (the "GPL"), in which case the provisions of the MPL or the GPL are
  35. * applicable instead of those above. If you wish to allow use of your
  36. * version of this file only under the terms of one of those two
  37. * licenses (the MPL or the GPL) and not to allow others to use your
  38. * version of this file under the LGPL, indicate your decision by
  39. * deletingthe provisions above and replace them with the notice and
  40. * other provisions required by the MPL or the GPL, as the case may be.
  41. * If you do not delete the provisions above, a recipient may use your
  42. * version of this file under any of the LGPL, the MPL or the GPL.
  43. * Copyright 2006-2008 the V8 project authors. All rights reserved.
  44. * Redistribution and use in source and binary forms, with or without
  45. * modification, are permitted provided that the following conditions are
  46. * met:
  47. *
  48. * * Redistributions of source code must retain the above copyright
  49. * notice, this list of conditions and the following disclaimer.
  50. * * Redistributions in binary form must reproduce the above
  51. * copyright notice, this list of conditions and the following
  52. * disclaimer in the documentation and/or other materials provided
  53. * with the distribution.
  54. * * Neither the name of Google Inc. nor the names of its
  55. * contributors may be used to endorse or promote products derived
  56. * from this software without specific prior written permission.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  59. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  60. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  61. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  62. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  63. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  64. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  65. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  66. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  67. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  68. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  69. */
  70. #include "config.h"
  71. #include "JSDateMath.h"
  72. #include "JSObject.h"
  73. #include "JSScope.h"
  74. #include "Operations.h"
  75. #include <algorithm>
  76. #include <limits.h>
  77. #include <limits>
  78. #include <stdint.h>
  79. #include <time.h>
  80. #include <wtf/ASCIICType.h>
  81. #include <wtf/Assertions.h>
  82. #include <wtf/CurrentTime.h>
  83. #include <wtf/MathExtras.h>
  84. #include <wtf/StdLibExtras.h>
  85. #include <wtf/StringExtras.h>
  86. #include <wtf/text/StringBuilder.h>
  87. #if HAVE(ERRNO_H)
  88. #include <errno.h>
  89. #endif
  90. #if HAVE(SYS_TIME_H)
  91. #include <sys/time.h>
  92. #endif
  93. #if HAVE(SYS_TIMEB_H)
  94. #include <sys/timeb.h>
  95. #endif
  96. using namespace WTF;
  97. namespace JSC {
  98. static inline double timeToMS(double hour, double min, double sec, double ms)
  99. {
  100. return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms);
  101. }
  102. static inline int msToSeconds(double ms)
  103. {
  104. double result = fmod(floor(ms / msPerSecond), secondsPerMinute);
  105. if (result < 0)
  106. result += secondsPerMinute;
  107. return static_cast<int>(result);
  108. }
  109. // 0: Sunday, 1: Monday, etc.
  110. static inline int msToWeekDay(double ms)
  111. {
  112. int wd = (static_cast<int>(msToDays(ms)) + 4) % 7;
  113. if (wd < 0)
  114. wd += 7;
  115. return wd;
  116. }
  117. // Get the combined UTC + DST offset for the time passed in.
  118. //
  119. // NOTE: The implementation relies on the fact that no time zones have
  120. // more than one daylight savings offset change per month.
  121. // If this function is called with NaN it returns NaN.
  122. static LocalTimeOffset localTimeOffset(ExecState* exec, double ms)
  123. {
  124. LocalTimeOffsetCache& cache = exec->vm().localTimeOffsetCache;
  125. double start = cache.start;
  126. double end = cache.end;
  127. if (start <= ms) {
  128. // If the time fits in the cached interval, return the cached offset.
  129. if (ms <= end) return cache.offset;
  130. // Compute a possible new interval end.
  131. double newEnd = end + cache.increment;
  132. if (ms <= newEnd) {
  133. LocalTimeOffset endOffset = calculateLocalTimeOffset(newEnd);
  134. if (cache.offset == endOffset) {
  135. // If the offset at the end of the new interval still matches
  136. // the offset in the cache, we grow the cached time interval
  137. // and return the offset.
  138. cache.end = newEnd;
  139. cache.increment = msPerMonth;
  140. return endOffset;
  141. }
  142. LocalTimeOffset offset = calculateLocalTimeOffset(ms);
  143. if (offset == endOffset) {
  144. // The offset at the given time is equal to the offset at the
  145. // new end of the interval, so that means that we've just skipped
  146. // the point in time where the DST offset change occurred. Updated
  147. // the interval to reflect this and reset the increment.
  148. cache.start = ms;
  149. cache.end = newEnd;
  150. cache.increment = msPerMonth;
  151. } else {
  152. // The interval contains a DST offset change and the given time is
  153. // before it. Adjust the increment to avoid a linear search for
  154. // the offset change point and change the end of the interval.
  155. cache.increment /= 3;
  156. cache.end = ms;
  157. }
  158. // Update the offset in the cache and return it.
  159. cache.offset = offset;
  160. return offset;
  161. }
  162. }
  163. // Compute the DST offset for the time and shrink the cache interval
  164. // to only contain the time. This allows fast repeated DST offset
  165. // computations for the same time.
  166. LocalTimeOffset offset = calculateLocalTimeOffset(ms);
  167. cache.offset = offset;
  168. cache.start = ms;
  169. cache.end = ms;
  170. cache.increment = msPerMonth;
  171. return offset;
  172. }
  173. double gregorianDateTimeToMS(ExecState* exec, const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
  174. {
  175. double day = dateToDaysFrom1970(t.year(), t.month(), t.monthDay());
  176. double ms = timeToMS(t.hour(), t.minute(), t.second(), milliSeconds);
  177. double result = (day * WTF::msPerDay) + ms;
  178. if (!inputIsUTC)
  179. result -= localTimeOffset(exec, result).offset;
  180. return result;
  181. }
  182. // input is UTC
  183. void msToGregorianDateTime(ExecState* exec, double ms, bool outputIsUTC, GregorianDateTime& tm)
  184. {
  185. LocalTimeOffset localTime;
  186. if (!outputIsUTC) {
  187. localTime = localTimeOffset(exec, ms);
  188. ms += localTime.offset;
  189. }
  190. const int year = msToYear(ms);
  191. tm.setSecond(msToSeconds(ms));
  192. tm.setMinute(msToMinutes(ms));
  193. tm.setHour(msToHours(ms));
  194. tm.setWeekDay(msToWeekDay(ms));
  195. tm.setYearDay(dayInYear(ms, year));
  196. tm.setMonthDay(dayInMonthFromDayInYear(tm.yearDay(), isLeapYear(year)));
  197. tm.setMonth(monthFromDayInYear(tm.yearDay(), isLeapYear(year)));
  198. tm.setYear(year);
  199. tm.setIsDST(localTime.isDST);
  200. tm.setUtcOffset(localTime.offset / WTF::msPerSecond);
  201. }
  202. double parseDateFromNullTerminatedCharacters(ExecState* exec, const char* dateString)
  203. {
  204. ASSERT(exec);
  205. bool haveTZ;
  206. int offset;
  207. double ms = WTF::parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset);
  208. if (std::isnan(ms))
  209. return QNaN;
  210. // fall back to local timezone
  211. if (!haveTZ)
  212. offset = localTimeOffset(exec, ms).offset / WTF::msPerMinute;
  213. return ms - (offset * WTF::msPerMinute);
  214. }
  215. double parseDate(ExecState* exec, const String& date)
  216. {
  217. if (date == exec->vm().cachedDateString)
  218. return exec->vm().cachedDateStringValue;
  219. double value = parseES5DateFromNullTerminatedCharacters(date.utf8().data());
  220. if (std::isnan(value))
  221. value = parseDateFromNullTerminatedCharacters(exec, date.utf8().data());
  222. exec->vm().cachedDateString = date;
  223. exec->vm().cachedDateStringValue = value;
  224. return value;
  225. }
  226. } // namespace JSC