]>
Commit | Line | Data |
---|---|---|
6fe7ccc8 A |
1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) | |
3 | * Copyright (C) 2006, 2007 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 | ||
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" | |
76 | #include "ScopeChain.h" | |
77 | ||
78 | #include <algorithm> | |
79 | #include <limits.h> | |
80 | #include <limits> | |
81 | #include <stdint.h> | |
82 | #include <time.h> | |
83 | #include <wtf/ASCIICType.h> | |
84 | #include <wtf/Assertions.h> | |
85 | #include <wtf/CurrentTime.h> | |
86 | #include <wtf/MathExtras.h> | |
87 | #include <wtf/StdLibExtras.h> | |
88 | #include <wtf/StringExtras.h> | |
89 | #include <wtf/text/StringBuilder.h> | |
90 | ||
91 | #if HAVE(ERRNO_H) | |
92 | #include <errno.h> | |
93 | #endif | |
94 | ||
95 | #if HAVE(SYS_TIME_H) | |
96 | #include <sys/time.h> | |
97 | #endif | |
98 | ||
99 | #if HAVE(SYS_TIMEB_H) | |
100 | #include <sys/timeb.h> | |
101 | #endif | |
102 | ||
103 | using namespace WTF; | |
104 | ||
105 | namespace JSC { | |
106 | ||
107 | static inline double timeToMS(double hour, double min, double sec, double ms) | |
108 | { | |
109 | return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms); | |
110 | } | |
111 | ||
112 | static inline int msToSeconds(double ms) | |
113 | { | |
114 | double result = fmod(floor(ms / msPerSecond), secondsPerMinute); | |
115 | if (result < 0) | |
116 | result += secondsPerMinute; | |
117 | return static_cast<int>(result); | |
118 | } | |
119 | ||
120 | // 0: Sunday, 1: Monday, etc. | |
121 | static inline int msToWeekDay(double ms) | |
122 | { | |
123 | int wd = (static_cast<int>(msToDays(ms)) + 4) % 7; | |
124 | if (wd < 0) | |
125 | wd += 7; | |
126 | return wd; | |
127 | } | |
128 | ||
129 | // Get the DST offset for the time passed in. | |
130 | // | |
131 | // NOTE: The implementation relies on the fact that no time zones have | |
132 | // more than one daylight savings offset change per month. | |
133 | // If this function is called with NaN it returns NaN. | |
134 | static double getDSTOffset(ExecState* exec, double ms, double utcOffset) | |
135 | { | |
136 | DSTOffsetCache& cache = exec->globalData().dstOffsetCache; | |
137 | double start = cache.start; | |
138 | double end = cache.end; | |
139 | ||
140 | if (start <= ms) { | |
141 | // If the time fits in the cached interval, return the cached offset. | |
142 | if (ms <= end) return cache.offset; | |
143 | ||
144 | // Compute a possible new interval end. | |
145 | double newEnd = end + cache.increment; | |
146 | ||
147 | if (ms <= newEnd) { | |
148 | double endOffset = calculateDSTOffset(newEnd, utcOffset); | |
149 | if (cache.offset == endOffset) { | |
150 | // If the offset at the end of the new interval still matches | |
151 | // the offset in the cache, we grow the cached time interval | |
152 | // and return the offset. | |
153 | cache.end = newEnd; | |
154 | cache.increment = msPerMonth; | |
155 | return endOffset; | |
156 | } else { | |
157 | double offset = calculateDSTOffset(ms, utcOffset); | |
158 | if (offset == endOffset) { | |
159 | // The offset at the given time is equal to the offset at the | |
160 | // new end of the interval, so that means that we've just skipped | |
161 | // the point in time where the DST offset change occurred. Updated | |
162 | // the interval to reflect this and reset the increment. | |
163 | cache.start = ms; | |
164 | cache.end = newEnd; | |
165 | cache.increment = msPerMonth; | |
166 | } else { | |
167 | // The interval contains a DST offset change and the given time is | |
168 | // before it. Adjust the increment to avoid a linear search for | |
169 | // the offset change point and change the end of the interval. | |
170 | cache.increment /= 3; | |
171 | cache.end = ms; | |
172 | } | |
173 | // Update the offset in the cache and return it. | |
174 | cache.offset = offset; | |
175 | return offset; | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | // Compute the DST offset for the time and shrink the cache interval | |
181 | // to only contain the time. This allows fast repeated DST offset | |
182 | // computations for the same time. | |
183 | double offset = calculateDSTOffset(ms, utcOffset); | |
184 | cache.offset = offset; | |
185 | cache.start = ms; | |
186 | cache.end = ms; | |
187 | cache.increment = msPerMonth; | |
188 | return offset; | |
189 | } | |
190 | ||
191 | /* | |
192 | * Get the difference in milliseconds between this time zone and UTC (GMT) | |
193 | * NOT including DST. | |
194 | */ | |
195 | double getUTCOffset(ExecState* exec) | |
196 | { | |
197 | double utcOffset = exec->globalData().cachedUTCOffset; | |
198 | if (!isnan(utcOffset)) | |
199 | return utcOffset; | |
200 | exec->globalData().cachedUTCOffset = calculateUTCOffset(); | |
201 | return exec->globalData().cachedUTCOffset; | |
202 | } | |
203 | ||
204 | double gregorianDateTimeToMS(ExecState* exec, const GregorianDateTime& t, double milliSeconds, bool inputIsUTC) | |
205 | { | |
206 | double day = dateToDaysFrom1970(t.year + 1900, t.month, t.monthDay); | |
207 | double ms = timeToMS(t.hour, t.minute, t.second, milliSeconds); | |
208 | double result = (day * WTF::msPerDay) + ms; | |
209 | ||
210 | if (!inputIsUTC) { // convert to UTC | |
211 | double utcOffset = getUTCOffset(exec); | |
212 | result -= utcOffset; | |
213 | result -= getDSTOffset(exec, result, utcOffset); | |
214 | } | |
215 | ||
216 | return result; | |
217 | } | |
218 | ||
219 | // input is UTC | |
220 | void msToGregorianDateTime(ExecState* exec, double ms, bool outputIsUTC, GregorianDateTime& tm) | |
221 | { | |
222 | double dstOff = 0.0; | |
223 | double utcOff = 0.0; | |
224 | if (!outputIsUTC) { | |
225 | utcOff = getUTCOffset(exec); | |
226 | dstOff = getDSTOffset(exec, ms, utcOff); | |
227 | ms += dstOff + utcOff; | |
228 | } | |
229 | ||
230 | const int year = msToYear(ms); | |
231 | tm.second = msToSeconds(ms); | |
232 | tm.minute = msToMinutes(ms); | |
233 | tm.hour = msToHours(ms); | |
234 | tm.weekDay = msToWeekDay(ms); | |
235 | tm.yearDay = dayInYear(ms, year); | |
236 | tm.monthDay = dayInMonthFromDayInYear(tm.yearDay, isLeapYear(year)); | |
237 | tm.month = monthFromDayInYear(tm.yearDay, isLeapYear(year)); | |
238 | tm.year = year - 1900; | |
239 | tm.isDST = dstOff != 0.0; | |
240 | tm.utcOffset = static_cast<long>((dstOff + utcOff) / WTF::msPerSecond); | |
241 | tm.timeZone = nullptr; | |
242 | } | |
243 | ||
244 | double parseDateFromNullTerminatedCharacters(ExecState* exec, const char* dateString) | |
245 | { | |
246 | ASSERT(exec); | |
247 | bool haveTZ; | |
248 | int offset; | |
249 | double ms = WTF::parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset); | |
250 | if (isnan(ms)) | |
251 | return std::numeric_limits<double>::quiet_NaN(); | |
252 | ||
253 | // fall back to local timezone | |
254 | if (!haveTZ) { | |
255 | double utcOffset = getUTCOffset(exec); | |
256 | double dstOffset = getDSTOffset(exec, ms, utcOffset); | |
257 | offset = static_cast<int>((utcOffset + dstOffset) / WTF::msPerMinute); | |
258 | } | |
259 | return ms - (offset * WTF::msPerMinute); | |
260 | } | |
261 | ||
262 | } // namespace JSC |