]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSDateMath.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / JSDateMath.cpp
CommitLineData
6fe7ccc8
A
1/*
2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
93a37866 3 * Copyright (C) 2006, 2007, 2012 Apple Inc. All rights reserved.
6fe7ccc8
A
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
44 * Copyright 2006-2008 the V8 project authors. All rights reserved.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions are
47 * met:
48 *
49 * * Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * * Redistributions in binary form must reproduce the above
52 * copyright notice, this list of conditions and the following
53 * disclaimer in the documentation and/or other materials provided
54 * with the distribution.
55 * * Neither the name of Google Inc. nor the names of its
56 * contributors may be used to endorse or promote products derived
57 * from this software without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
60 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
61 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
62 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
63 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
64 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
65 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 */
71
72#include "config.h"
73#include "JSDateMath.h"
74
75#include "JSObject.h"
93a37866 76#include "JSScope.h"
81345200 77#include "JSCInlines.h"
6fe7ccc8
A
78
79#include <algorithm>
80#include <limits.h>
81#include <limits>
82#include <stdint.h>
83#include <time.h>
84#include <wtf/ASCIICType.h>
85#include <wtf/Assertions.h>
86#include <wtf/CurrentTime.h>
87#include <wtf/MathExtras.h>
88#include <wtf/StdLibExtras.h>
89#include <wtf/StringExtras.h>
90#include <wtf/text/StringBuilder.h>
91
92#if HAVE(ERRNO_H)
93#include <errno.h>
94#endif
95
96#if HAVE(SYS_TIME_H)
97#include <sys/time.h>
98#endif
99
100#if HAVE(SYS_TIMEB_H)
101#include <sys/timeb.h>
102#endif
103
104using namespace WTF;
105
106namespace JSC {
107
108static inline double timeToMS(double hour, double min, double sec, double ms)
109{
110 return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms);
111}
112
113static inline int msToSeconds(double ms)
114{
115 double result = fmod(floor(ms / msPerSecond), secondsPerMinute);
116 if (result < 0)
117 result += secondsPerMinute;
118 return static_cast<int>(result);
119}
120
121// 0: Sunday, 1: Monday, etc.
122static inline int msToWeekDay(double ms)
123{
124 int wd = (static_cast<int>(msToDays(ms)) + 4) % 7;
125 if (wd < 0)
126 wd += 7;
127 return wd;
128}
129
93a37866 130// Get the combined UTC + DST offset for the time passed in.
6fe7ccc8
A
131//
132// NOTE: The implementation relies on the fact that no time zones have
133// more than one daylight savings offset change per month.
134// If this function is called with NaN it returns NaN.
ed1e77d3 135static LocalTimeOffset localTimeOffset(VM& vm, double ms, WTF::TimeType inputTimeType = WTF::UTCTime)
6fe7ccc8 136{
81345200 137 LocalTimeOffsetCache& cache = vm.localTimeOffsetCache;
6fe7ccc8
A
138 double start = cache.start;
139 double end = cache.end;
ed1e77d3 140 WTF::TimeType cachedTimeType = cache.timeType;
6fe7ccc8 141
ed1e77d3 142 if (cachedTimeType == inputTimeType && start <= ms) {
6fe7ccc8
A
143 // If the time fits in the cached interval, return the cached offset.
144 if (ms <= end) return cache.offset;
145
146 // Compute a possible new interval end.
147 double newEnd = end + cache.increment;
148
149 if (ms <= newEnd) {
ed1e77d3 150 LocalTimeOffset endOffset = calculateLocalTimeOffset(newEnd, inputTimeType);
6fe7ccc8
A
151 if (cache.offset == endOffset) {
152 // If the offset at the end of the new interval still matches
153 // the offset in the cache, we grow the cached time interval
154 // and return the offset.
155 cache.end = newEnd;
156 cache.increment = msPerMonth;
157 return endOffset;
93a37866 158 }
ed1e77d3 159 LocalTimeOffset offset = calculateLocalTimeOffset(ms, inputTimeType);
93a37866
A
160 if (offset == endOffset) {
161 // The offset at the given time is equal to the offset at the
162 // new end of the interval, so that means that we've just skipped
163 // the point in time where the DST offset change occurred. Updated
164 // the interval to reflect this and reset the increment.
165 cache.start = ms;
166 cache.end = newEnd;
167 cache.increment = msPerMonth;
6fe7ccc8 168 } else {
93a37866
A
169 // The interval contains a DST offset change and the given time is
170 // before it. Adjust the increment to avoid a linear search for
171 // the offset change point and change the end of the interval.
172 cache.increment /= 3;
173 cache.end = ms;
6fe7ccc8 174 }
93a37866
A
175 // Update the offset in the cache and return it.
176 cache.offset = offset;
177 return offset;
6fe7ccc8
A
178 }
179 }
180
181 // Compute the DST offset for the time and shrink the cache interval
182 // to only contain the time. This allows fast repeated DST offset
183 // computations for the same time.
ed1e77d3 184 LocalTimeOffset offset = calculateLocalTimeOffset(ms, inputTimeType);
6fe7ccc8
A
185 cache.offset = offset;
186 cache.start = ms;
187 cache.end = ms;
188 cache.increment = msPerMonth;
ed1e77d3 189 cache.timeType = inputTimeType;
6fe7ccc8
A
190 return offset;
191}
192
ed1e77d3 193double gregorianDateTimeToMS(VM& vm, const GregorianDateTime& t, double milliSeconds, WTF::TimeType inputTimeType)
6fe7ccc8 194{
93a37866
A
195 double day = dateToDaysFrom1970(t.year(), t.month(), t.monthDay());
196 double ms = timeToMS(t.hour(), t.minute(), t.second(), milliSeconds);
ed1e77d3
A
197 double localTimeResult = (day * WTF::msPerDay) + ms;
198 double localToUTCTimeOffset = inputTimeType == LocalTime
199 ? localTimeOffset(vm, localTimeResult, inputTimeType).offset : 0;
6fe7ccc8 200
ed1e77d3 201 return localTimeResult - localToUTCTimeOffset;
6fe7ccc8
A
202}
203
204// input is UTC
ed1e77d3 205void msToGregorianDateTime(VM& vm, double ms, WTF::TimeType outputTimeType, GregorianDateTime& tm)
6fe7ccc8 206{
93a37866 207 LocalTimeOffset localTime;
ed1e77d3 208 if (outputTimeType == WTF::LocalTime) {
81345200 209 localTime = localTimeOffset(vm, ms);
93a37866 210 ms += localTime.offset;
6fe7ccc8
A
211 }
212
213 const int year = msToYear(ms);
93a37866
A
214 tm.setSecond(msToSeconds(ms));
215 tm.setMinute(msToMinutes(ms));
216 tm.setHour(msToHours(ms));
217 tm.setWeekDay(msToWeekDay(ms));
218 tm.setYearDay(dayInYear(ms, year));
219 tm.setMonthDay(dayInMonthFromDayInYear(tm.yearDay(), isLeapYear(year)));
220 tm.setMonth(monthFromDayInYear(tm.yearDay(), isLeapYear(year)));
221 tm.setYear(year);
222 tm.setIsDST(localTime.isDST);
223 tm.setUtcOffset(localTime.offset / WTF::msPerSecond);
6fe7ccc8
A
224}
225
81345200 226double parseDateFromNullTerminatedCharacters(VM& vm, const char* dateString)
6fe7ccc8 227{
6fe7ccc8
A
228 bool haveTZ;
229 int offset;
ed1e77d3
A
230 double localTimeMS = WTF::parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset);
231 if (std::isnan(localTimeMS))
81345200 232 return std::numeric_limits<double>::quiet_NaN();
6fe7ccc8 233
ed1e77d3 234 // fall back to local timezone.
93a37866 235 if (!haveTZ)
ed1e77d3 236 offset = localTimeOffset(vm, localTimeMS, WTF::LocalTime).offset / WTF::msPerMinute;
93a37866 237
ed1e77d3 238 return localTimeMS - (offset * WTF::msPerMinute);
6fe7ccc8
A
239}
240
81345200 241double parseDate(VM& vm, const String& date)
93a37866 242{
81345200
A
243 if (date == vm.cachedDateString)
244 return vm.cachedDateStringValue;
93a37866
A
245 double value = parseES5DateFromNullTerminatedCharacters(date.utf8().data());
246 if (std::isnan(value))
81345200
A
247 value = parseDateFromNullTerminatedCharacters(vm, date.utf8().data());
248 vm.cachedDateString = date;
249 vm.cachedDateStringValue = value;
93a37866
A
250 return value;
251}
252
6fe7ccc8 253} // namespace JSC