abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) |
ggaren@apple.com | 5a26a40 | 2014-06-25 21:51:45 +0000 | [diff] [blame] | 3 | * Copyright (C) 2006, 2007, 2012 Apple Inc. All rights reserved. |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 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 | |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 75 | #include "JSObject.h" |
ggaren@apple.com | b11e787 | 2012-08-30 22:50:00 +0000 | [diff] [blame] | 76 | #include "JSScope.h" |
fpizlo@apple.com | fb7eff2 | 2014-02-11 01:45:50 +0000 | [diff] [blame] | 77 | #include "JSCInlines.h" |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 78 | |
| 79 | #include <algorithm> |
| 80 | #include <limits.h> |
| 81 | #include <limits> |
| 82 | #include <stdint.h> |
| 83 | #include <time.h> |
eric@webkit.org | 3979f2d | 2012-03-07 08:50:54 +0000 | [diff] [blame] | 84 | #include <wtf/ASCIICType.h> |
| 85 | #include <wtf/Assertions.h> |
eric@webkit.org | fa5d72e | 2012-03-21 23:18:20 +0000 | [diff] [blame] | 86 | #include <wtf/CurrentTime.h> |
| 87 | #include <wtf/MathExtras.h> |
| 88 | #include <wtf/StdLibExtras.h> |
| 89 | #include <wtf/StringExtras.h> |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 90 | |
| 91 | #if HAVE(ERRNO_H) |
| 92 | #include <errno.h> |
| 93 | #endif |
| 94 | |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 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 | |
barraclough@apple.com | d490067 | 2013-05-28 21:33:51 +0000 | [diff] [blame] | 129 | // Get the combined UTC + DST offset for the time passed in. |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 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. |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 134 | static LocalTimeOffset localTimeOffset(VM& vm, double ms, WTF::TimeType inputTimeType = WTF::UTCTime) |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 135 | { |
akling@apple.com | 38c79a2 | 2013-09-27 13:40:59 +0000 | [diff] [blame] | 136 | LocalTimeOffsetCache& cache = vm.localTimeOffsetCache; |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 137 | double start = cache.start; |
| 138 | double end = cache.end; |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 139 | WTF::TimeType cachedTimeType = cache.timeType; |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 140 | |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 141 | if (cachedTimeType == inputTimeType && start <= ms) { |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 142 | // If the time fits in the cached interval, return the cached offset. |
| 143 | if (ms <= end) return cache.offset; |
| 144 | |
| 145 | // Compute a possible new interval end. |
| 146 | double newEnd = end + cache.increment; |
| 147 | |
ggaren@apple.com | 5a26a40 | 2014-06-25 21:51:45 +0000 | [diff] [blame] | 148 | if (ms <= newEnd) { |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 149 | LocalTimeOffset endOffset = calculateLocalTimeOffset(newEnd, inputTimeType); |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 150 | if (cache.offset == endOffset) { |
| 151 | // If the offset at the end of the new interval still matches |
| 152 | // the offset in the cache, we grow the cached time interval |
| 153 | // and return the offset. |
| 154 | cache.end = newEnd; |
| 155 | cache.increment = msPerMonth; |
| 156 | return endOffset; |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 157 | } |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 158 | LocalTimeOffset offset = calculateLocalTimeOffset(ms, inputTimeType); |
barraclough@apple.com | d490067 | 2013-05-28 21:33:51 +0000 | [diff] [blame] | 159 | if (offset == endOffset) { |
| 160 | // The offset at the given time is equal to the offset at the |
| 161 | // new end of the interval, so that means that we've just skipped |
| 162 | // the point in time where the DST offset change occurred. Updated |
| 163 | // the interval to reflect this and reset the increment. |
| 164 | cache.start = ms; |
| 165 | cache.end = newEnd; |
| 166 | cache.increment = msPerMonth; |
| 167 | } else { |
| 168 | // The interval contains a DST offset change and the given time is |
| 169 | // before it. Adjust the increment to avoid a linear search for |
| 170 | // the offset change point and change the end of the interval. |
| 171 | cache.increment /= 3; |
| 172 | cache.end = ms; |
| 173 | } |
| 174 | // Update the offset in the cache and return it. |
| 175 | cache.offset = offset; |
| 176 | return offset; |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 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. |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 183 | LocalTimeOffset offset = calculateLocalTimeOffset(ms, inputTimeType); |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 184 | cache.offset = offset; |
| 185 | cache.start = ms; |
| 186 | cache.end = ms; |
| 187 | cache.increment = msPerMonth; |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 188 | cache.timeType = inputTimeType; |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 189 | return offset; |
| 190 | } |
| 191 | |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 192 | double gregorianDateTimeToMS(VM& vm, const GregorianDateTime& t, double milliSeconds, WTF::TimeType inputTimeType) |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 193 | { |
paroga@webkit.org | 683889f | 2012-07-24 19:25:30 +0000 | [diff] [blame] | 194 | double day = dateToDaysFrom1970(t.year(), t.month(), t.monthDay()); |
paroga@webkit.org | 175d46f | 2012-07-24 05:19:55 +0000 | [diff] [blame] | 195 | double ms = timeToMS(t.hour(), t.minute(), t.second(), milliSeconds); |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 196 | double localTimeResult = (day * WTF::msPerDay) + ms; |
| 197 | double localToUTCTimeOffset = inputTimeType == LocalTime |
| 198 | ? localTimeOffset(vm, localTimeResult, inputTimeType).offset : 0; |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 199 | |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 200 | return localTimeResult - localToUTCTimeOffset; |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // input is UTC |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 204 | void msToGregorianDateTime(VM& vm, double ms, WTF::TimeType outputTimeType, GregorianDateTime& tm) |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 205 | { |
barraclough@apple.com | 2e1fc20 | 2013-05-28 21:45:52 +0000 | [diff] [blame] | 206 | LocalTimeOffset localTime; |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 207 | if (outputTimeType == WTF::LocalTime) { |
akling@apple.com | 38c79a2 | 2013-09-27 13:40:59 +0000 | [diff] [blame] | 208 | localTime = localTimeOffset(vm, ms); |
barraclough@apple.com | d490067 | 2013-05-28 21:33:51 +0000 | [diff] [blame] | 209 | ms += localTime.offset; |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | const int year = msToYear(ms); |
paroga@webkit.org | 175d46f | 2012-07-24 05:19:55 +0000 | [diff] [blame] | 213 | tm.setSecond(msToSeconds(ms)); |
| 214 | tm.setMinute(msToMinutes(ms)); |
| 215 | tm.setHour(msToHours(ms)); |
| 216 | tm.setWeekDay(msToWeekDay(ms)); |
| 217 | tm.setYearDay(dayInYear(ms, year)); |
| 218 | tm.setMonthDay(dayInMonthFromDayInYear(tm.yearDay(), isLeapYear(year))); |
| 219 | tm.setMonth(monthFromDayInYear(tm.yearDay(), isLeapYear(year))); |
paroga@webkit.org | 683889f | 2012-07-24 19:25:30 +0000 | [diff] [blame] | 220 | tm.setYear(year); |
barraclough@apple.com | d490067 | 2013-05-28 21:33:51 +0000 | [diff] [blame] | 221 | tm.setIsDST(localTime.isDST); |
| 222 | tm.setUtcOffset(localTime.offset / WTF::msPerSecond); |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 223 | } |
| 224 | |
ggaren@apple.com | 5a26a40 | 2014-06-25 21:51:45 +0000 | [diff] [blame] | 225 | double parseDateFromNullTerminatedCharacters(VM& vm, const char* dateString) |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 226 | { |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 227 | bool haveTZ; |
| 228 | int offset; |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 229 | double localTimeMS = WTF::parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset); |
| 230 | if (std::isnan(localTimeMS)) |
ggaren@apple.com | a02ae51 | 2014-06-25 21:59:54 +0000 | [diff] [blame] | 231 | return std::numeric_limits<double>::quiet_NaN(); |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 232 | |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 233 | // fall back to local timezone. |
barraclough@apple.com | d490067 | 2013-05-28 21:33:51 +0000 | [diff] [blame] | 234 | if (!haveTZ) |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 235 | offset = localTimeOffset(vm, localTimeMS, WTF::LocalTime).offset / WTF::msPerMinute; |
barraclough@apple.com | d490067 | 2013-05-28 21:33:51 +0000 | [diff] [blame] | 236 | |
commit-queue@webkit.org | 5d50831 | 2014-10-23 01:15:08 +0000 | [diff] [blame] | 237 | return localTimeMS - (offset * WTF::msPerMinute); |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 238 | } |
| 239 | |
akling@apple.com | 38c79a2 | 2013-09-27 13:40:59 +0000 | [diff] [blame] | 240 | double parseDate(VM& vm, const String& date) |
paroga@webkit.org | f463566 | 2012-07-23 20:53:35 +0000 | [diff] [blame] | 241 | { |
akling@apple.com | 38c79a2 | 2013-09-27 13:40:59 +0000 | [diff] [blame] | 242 | if (date == vm.cachedDateString) |
| 243 | return vm.cachedDateStringValue; |
paroga@webkit.org | f463566 | 2012-07-23 20:53:35 +0000 | [diff] [blame] | 244 | double value = parseES5DateFromNullTerminatedCharacters(date.utf8().data()); |
zandobersek@gmail.com | 9182d47 | 2013-02-13 23:01:21 +0000 | [diff] [blame] | 245 | if (std::isnan(value)) |
akling@apple.com | 38c79a2 | 2013-09-27 13:40:59 +0000 | [diff] [blame] | 246 | value = parseDateFromNullTerminatedCharacters(vm, date.utf8().data()); |
| 247 | vm.cachedDateString = date; |
| 248 | vm.cachedDateStringValue = value; |
paroga@webkit.org | f463566 | 2012-07-23 20:53:35 +0000 | [diff] [blame] | 249 | return value; |
| 250 | } |
| 251 | |
abarth@webkit.org | 2e4652a | 2011-11-15 00:04:53 +0000 | [diff] [blame] | 252 | } // namespace JSC |