1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
/*
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2006-2020 Apple Inc. All rights reserved.
* Copyright (C) 2009 Google Inc. All rights reserved.
* Copyright (C) 2010 Research In Motion Limited. All rights reserved.
*
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
*/
#pragma once
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <wtf/WallTime.h>
#include <wtf/text/WTFString.h>
namespace WTF {
enum TimeType {
UTCTime = 0,
LocalTime
};
struct LocalTimeOffset {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
LocalTimeOffset() = default;
constexpr LocalTimeOffset(bool isDST, int offset)
: isDST(isDST)
, offset(offset)
{
}
bool operator==(const LocalTimeOffset& other) const
{
return isDST == other.isDST && offset == other.offset;
}
bool operator!=(const LocalTimeOffset& other) const
{
return isDST != other.isDST || offset != other.offset;
}
bool isDST { false };
int offset { 0 };
};
void initializeDates();
int equivalentYearForDST(int year);
// Not really math related, but this is currently the only shared place to put these.
WTF_EXPORT_PRIVATE double parseES5DateFromNullTerminatedCharacters(const char* dateString, bool& isLocalTime);
WTF_EXPORT_PRIVATE double parseDateFromNullTerminatedCharacters(const char* dateString);
WTF_EXPORT_PRIVATE double parseDateFromNullTerminatedCharacters(const char* dateString, bool& isLocalTime);
// dayOfWeek: [0, 6] 0 being Monday, day: [1, 31], month: [0, 11], year: ex: 2011, hours: [0, 23], minutes: [0, 59], seconds: [0, 59], utcOffset: [-720,720].
WTF_EXPORT_PRIVATE String makeRFC2822DateString(unsigned dayOfWeek, unsigned day, unsigned month, unsigned year, unsigned hours, unsigned minutes, unsigned seconds, int utcOffset);
inline double jsCurrentTime()
{
// JavaScript doesn't recognize fractions of a millisecond.
return floor(WallTime::now().secondsSinceEpoch().milliseconds());
}
extern WTF_EXPORT_PRIVATE const ASCIILiteral weekdayName[7];
extern WTF_EXPORT_PRIVATE const ASCIILiteral monthName[12];
extern WTF_EXPORT_PRIVATE const ASCIILiteral monthFullName[12];
extern WTF_EXPORT_PRIVATE const int firstDayOfMonth[2][12];
static constexpr double hoursPerDay = 24.0;
static constexpr double minutesPerHour = 60.0;
static constexpr double secondsPerMinute = 60.0;
static constexpr double msPerSecond = 1000.0;
static constexpr double msPerMonth = 2592000000.0;
static constexpr double secondsPerHour = secondsPerMinute * minutesPerHour;
static constexpr double secondsPerDay = secondsPerHour * hoursPerDay;
static constexpr double msPerMinute = msPerSecond * secondsPerMinute;
static constexpr double msPerHour = msPerSecond * secondsPerHour;
static constexpr double msPerDay = msPerSecond * secondsPerDay;
static constexpr double maxUnixTime = 2145859200.0; // 12/31/2037
// ECMAScript asks not to support for a date of which total
// millisecond value is larger than the following value.
// See 15.9.1.14 of ECMA-262 5th edition.
static constexpr double maxECMAScriptTime = 8.64E15;
class TimeClippedPositiveMilliseconds {
public:
static constexpr int64_t hoursPerDay = 24;
static constexpr int64_t minutesPerHour = 60;
static constexpr int64_t secondsPerMinute = 60;
static constexpr int64_t msPerSecond = 1000;
static constexpr int64_t msPerMonth = 2592000000;
static constexpr int64_t secondsPerHour = secondsPerMinute * minutesPerHour;
static constexpr int64_t secondsPerDay = secondsPerHour * hoursPerDay;
static constexpr int64_t msPerMinute = msPerSecond * secondsPerMinute;
static constexpr int64_t msPerHour = msPerSecond * secondsPerHour;
static constexpr int64_t msPerDay = msPerSecond * secondsPerDay;
static constexpr int64_t maxECMAScriptTime = 8.64E15;
explicit TimeClippedPositiveMilliseconds(int64_t value)
: m_value(value)
{
ASSERT(value >= 0);
}
int64_t value() const { return m_value; }
double asDouble() const { return static_cast<double>(m_value); }
private:
int64_t m_value;
};
inline double timeClip(double t)
{
if (std::abs(t) > maxECMAScriptTime)
return std::numeric_limits<double>::quiet_NaN();
return std::trunc(t) + 0.0;
}
inline double daysFrom1970ToYear(int year)
{
// The Gregorian Calendar rules for leap years:
// Every fourth year is a leap year. 2004, 2008, and 2012 are leap years.
// However, every hundredth year is not a leap year. 1900 and 2100 are not leap years.
// Every four hundred years, there's a leap year after all. 2000 and 2400 are leap years.
static constexpr int leapDaysBefore1971By4Rule = 1970 / 4;
static constexpr int excludedLeapDaysBefore1971By100Rule = 1970 / 100;
static constexpr int leapDaysBefore1971By400Rule = 1970 / 400;
const double yearMinusOne = static_cast<double>(year) - 1;
const double yearsToAddBy4Rule = floor(yearMinusOne / 4.0) - leapDaysBefore1971By4Rule;
const double yearsToExcludeBy100Rule = floor(yearMinusOne / 100.0) - excludedLeapDaysBefore1971By100Rule;
const double yearsToAddBy400Rule = floor(yearMinusOne / 400.0) - leapDaysBefore1971By400Rule;
return 365.0 * (year - 1970.0) + yearsToAddBy4Rule - yearsToExcludeBy100Rule + yearsToAddBy400Rule;
}
inline int64_t daysFrom1970ToYearTimeClippedPositive(int year)
{
static constexpr int leapDaysBefore1971By4Rule = 1970 / 4;
static constexpr int excludedLeapDaysBefore1971By100Rule = 1970 / 100;
static constexpr int leapDaysBefore1971By400Rule = 1970 / 400;
ASSERT(year >= 1970);
const int64_t yearMinusOne = year - 1;
const int64_t yearsToAddBy4Rule = yearMinusOne / 4.0 - leapDaysBefore1971By4Rule;
const int64_t yearsToExcludeBy100Rule = yearMinusOne / 100.0 - excludedLeapDaysBefore1971By100Rule;
const int64_t yearsToAddBy400Rule = yearMinusOne / 400.0 - leapDaysBefore1971By400Rule;
return 365 * (year - 1970) + yearsToAddBy4Rule - yearsToExcludeBy100Rule + yearsToAddBy400Rule;
}
inline bool isLeapYear(int year)
{
if (year % 4 != 0)
return false;
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
return true;
}
inline int daysInYear(int year)
{
return 365 + isLeapYear(year);
}
inline double msToDays(double ms)
{
return floor(ms / msPerDay);
}
inline int64_t msToDays(TimeClippedPositiveMilliseconds ms)
{
return ms.value() / TimeClippedPositiveMilliseconds::msPerDay;
}
inline int dayInYear(int year, int month, int day)
{
return firstDayOfMonth[isLeapYear(year)][month] + day - 1;
}
inline int dayInYear(double ms, int year)
{
double result = msToDays(ms) - daysFrom1970ToYear(year);
return std::isnan(result) ? 0 : static_cast<int>(result);
}
inline int dayInYear(TimeClippedPositiveMilliseconds ms, int year)
{
return static_cast<int>(msToDays(ms) - daysFrom1970ToYearTimeClippedPositive(year));
}
// Returns the number of days from 1970-01-01 to the specified date.
inline double dateToDaysFrom1970(int year, int month, int day)
{
year += month / 12;
month %= 12;
if (month < 0) {
month += 12;
--year;
}
double yearday = floor(daysFrom1970ToYear(year));
ASSERT((year >= 1970 && yearday >= 0) || (year < 1970 && yearday < 0));
return yearday + dayInYear(year, month, day);
}
inline int msToYear(double ms)
{
double msAsYears = std::floor(ms / (msPerDay * 365.2425));
if (std::isnan(msAsYears))
msAsYears = 0;
int approxYear = static_cast<int>(msAsYears + 1970);
double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear);
if (msFromApproxYearTo1970 > ms)
return approxYear - 1;
if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms)
return approxYear + 1;
return approxYear;
}
inline int msToMinutes(double ms)
{
double result = fmod(floor(ms / msPerMinute), minutesPerHour);
if (result < 0)
result += minutesPerHour;
return static_cast<int>(result);
}
inline int msToMinutes(TimeClippedPositiveMilliseconds ms)
{
int64_t result = (ms.value() / TimeClippedPositiveMilliseconds::msPerMinute) % TimeClippedPositiveMilliseconds::minutesPerHour;
ASSERT(result >= 0);
return static_cast<int>(result);
}
inline int msToHours(double ms)
{
double result = fmod(floor(ms / msPerHour), hoursPerDay);
if (result < 0)
result += hoursPerDay;
return static_cast<int>(result);
}
inline int msToHours(TimeClippedPositiveMilliseconds ms)
{
int64_t result = (ms.value() / TimeClippedPositiveMilliseconds::msPerHour) % TimeClippedPositiveMilliseconds::hoursPerDay;
ASSERT(result >= 0);
return static_cast<int>(result);
}
inline int msToSeconds(double ms)
{
double result = fmod(floor(ms / msPerSecond), secondsPerMinute);
if (result < 0)
result += secondsPerMinute;
return static_cast<int>(result);
}
inline int msToSeconds(TimeClippedPositiveMilliseconds ms)
{
int64_t result = ms.value() / TimeClippedPositiveMilliseconds::msPerSecond % TimeClippedPositiveMilliseconds::secondsPerMinute;
ASSERT(result >= 0);
return static_cast<int>(result);
}
// 0: Sunday, 1: Monday, etc.
inline int msToWeekDay(double ms)
{
int wd = (static_cast<int>(msToDays(ms)) + 4) % 7;
if (wd < 0)
wd += 7;
return wd;
}
inline int msToWeekDay(TimeClippedPositiveMilliseconds ms)
{
int result = (static_cast<int>(msToDays(ms)) + 4) % 7;
ASSERT(result >= 0);
return result;
}
inline int monthFromDayInYear(int dayInYear, bool leapYear)
{
const int d = dayInYear;
int step;
if (d < (step = 31))
return 0;
step += (leapYear ? 29 : 28);
if (d < step)
return 1;
if (d < (step += 31))
return 2;
if (d < (step += 30))
return 3;
if (d < (step += 31))
return 4;
if (d < (step += 30))
return 5;
if (d < (step += 31))
return 6;
if (d < (step += 31))
return 7;
if (d < (step += 30))
return 8;
if (d < (step += 31))
return 9;
if (d < step + 30)
return 10;
return 11;
}
inline int dayInMonthFromDayInYear(int dayInYear, bool leapYear)
{
auto checkMonth = [] (int dayInYear, int& startDayOfThisMonth, int& startDayOfNextMonth, int daysInThisMonth) -> bool {
startDayOfThisMonth = startDayOfNextMonth;
startDayOfNextMonth += daysInThisMonth;
return (dayInYear <= startDayOfNextMonth);
};
const int d = dayInYear;
int step;
int next = 30;
if (d <= next)
return d + 1;
const int daysInFeb = (leapYear ? 29 : 28);
if (checkMonth(d, step, next, daysInFeb))
return d - step;
if (checkMonth(d, step, next, 31))
return d - step;
if (checkMonth(d, step, next, 30))
return d - step;
if (checkMonth(d, step, next, 31))
return d - step;
if (checkMonth(d, step, next, 30))
return d - step;
if (checkMonth(d, step, next, 31))
return d - step;
if (checkMonth(d, step, next, 31))
return d - step;
if (checkMonth(d, step, next, 30))
return d - step;
if (checkMonth(d, step, next, 31))
return d - step;
if (checkMonth(d, step, next, 30))
return d - step;
step = next;
return d - step;
}
inline double timeToMS(double hour, double min, double sec, double ms)
{
return (((hour * WTF::minutesPerHour + min) * WTF::secondsPerMinute + sec) * WTF::msPerSecond + ms);
}
WTF_EXPORT_PRIVATE bool isTimeZoneValid(StringView);
WTF_EXPORT_PRIVATE bool setTimeZoneOverride(StringView);
WTF_EXPORT_PRIVATE void getTimeZoneOverride(Vector<UChar, 32>& timeZoneID);
// Returns combined offset in millisecond (UTC + DST).
WTF_EXPORT_PRIVATE LocalTimeOffset calculateLocalTimeOffset(double utcInMilliseconds, TimeType = UTCTime);
} // namespace WTF
using WTF::calculateLocalTimeOffset;
using WTF::dateToDaysFrom1970;
using WTF::dayInMonthFromDayInYear;
using WTF::dayInYear;
using WTF::getTimeZoneOverride;
using WTF::isLeapYear;
using WTF::isTimeZoneValid;
using WTF::jsCurrentTime;
using WTF::LocalTimeOffset;
using WTF::makeRFC2822DateString;
using WTF::minutesPerHour;
using WTF::monthFromDayInYear;
using WTF::msPerDay;
using WTF::msPerHour;
using WTF::msPerMinute;
using WTF::msPerSecond;
using WTF::msToDays;
using WTF::msToHours;
using WTF::msToMinutes;
using WTF::msToYear;
using WTF::parseDateFromNullTerminatedCharacters;
using WTF::secondsPerDay;
using WTF::secondsPerMinute;
using WTF::setTimeZoneOverride;
using WTF::timeClip;
using WTF::timeToMS;
|