#include "config.h"
#include "DateMath.h"
#include "Assertions.h"
#include "ASCIICType.h"
#include "CurrentTime.h"
#if USE(JSC)
#include "JSObject.h"
#endif
#include "MathExtras.h"
#if USE(JSC)
#include "ScopeChain.h"
#endif
#include "StdLibExtras.h"
#include "StringExtras.h"
#include <algorithm>
#include <limits.h>
#include <limits>
#include <stdint.h>
#include <time.h>
#include <wtf/text/StringBuilder.h>
#if HAVE(ERRNO_H)
#include <errno.h>
#endif
#if OS(WINCE)
extern "C" size_t strftime(char * const s, const size_t maxsize, const char * const format, const struct tm * const t);
extern "C" struct tm * localtime(const time_t *timer);
#endif
#if HAVE(SYS_TIME_H)
#include <sys/time.h>
#endif
#if HAVE(SYS_TIMEB_H)
#include <sys/timeb.h>
#endif
#if USE(JSC)
#include "CallFrame.h"
#endif
using namespace WTF;
namespace WTF {
static const double minutesPerDay = 24.0 * 60.0;
static const double secondsPerDay = 24.0 * 60.0 * 60.0;
static const double secondsPerYear = 24.0 * 60.0 * 60.0 * 365.0;
static const double usecPerSec = 1000000.0;
static const double maxUnixTime = 2145859200.0; static const double maxECMAScriptTime = 8.64E15;
static const int firstDayOfMonth[2][12] = {
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
};
static 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;
}
static inline int daysInYear(int year)
{
return 365 + isLeapYear(year);
}
static inline double daysFrom1970ToYear(int year)
{
static const int leapDaysBefore1971By4Rule = 1970 / 4;
static const int excludedLeapDaysBefore1971By100Rule = 1970 / 100;
static const int leapDaysBefore1971By400Rule = 1970 / 400;
const double yearMinusOne = 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) + yearsToAddBy4Rule - yearsToExcludeBy100Rule + yearsToAddBy400Rule;
}
static inline double msToDays(double ms)
{
return floor(ms / msPerDay);
}
static String twoDigitStringFromNumber(int number)
{
ASSERT(number >= 0 && number < 100);
if (number > 9)
return String::number(number);
return makeString("0", String::number(number));
}
int msToYear(double ms)
{
int approxYear = static_cast<int>(floor(ms / (msPerDay * 365.2425)) + 1970);
double msFromApproxYearTo1970 = msPerDay * daysFrom1970ToYear(approxYear);
if (msFromApproxYearTo1970 > ms)
return approxYear - 1;
if (msFromApproxYearTo1970 + msPerDay * daysInYear(approxYear) <= ms)
return approxYear + 1;
return approxYear;
}
int dayInYear(double ms, int year)
{
return static_cast<int>(msToDays(ms) - daysFrom1970ToYear(year));
}
static inline double msToMilliseconds(double ms)
{
double result = fmod(ms, msPerDay);
if (result < 0)
result += msPerDay;
return result;
}
static inline int msToWeekDay(double ms)
{
int wd = (static_cast<int>(msToDays(ms)) + 4) % 7;
if (wd < 0)
wd += 7;
return wd;
}
static inline int msToSeconds(double ms)
{
double result = fmod(floor(ms / msPerSecond), secondsPerMinute);
if (result < 0)
result += secondsPerMinute;
return static_cast<int>(result);
}
static inline int msToMinutes(double ms)
{
double result = fmod(floor(ms / msPerMinute), minutesPerHour);
if (result < 0)
result += minutesPerHour;
return static_cast<int>(result);
}
static inline int msToHours(double ms)
{
double result = fmod(floor(ms/msPerHour), hoursPerDay);
if (result < 0)
result += hoursPerDay;
return static_cast<int>(result);
}
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;
}
static inline bool checkMonth(int dayInYear, int& startDayOfThisMonth, int& startDayOfNextMonth, int daysInThisMonth)
{
startDayOfThisMonth = startDayOfNextMonth;
startDayOfNextMonth += daysInThisMonth;
return (dayInYear <= startDayOfNextMonth);
}
int dayInMonthFromDayInYear(int dayInYear, bool leapYear)
{
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;
}
static inline int monthToDayInYear(int month, bool isLeapYear)
{
return firstDayOfMonth[isLeapYear][month];
}
static inline double timeToMS(double hour, double min, double sec, double ms)
{
return (((hour * minutesPerHour + min) * secondsPerMinute + sec) * msPerSecond + ms);
}
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));
int monthday = monthToDayInYear(month, isLeapYear(year));
return yearday + monthday + day - 1;
}
static inline int maximumYearForDST()
{
return 2037;
}
static inline int minimumYearForDST()
{
return std::min(msToYear(jsCurrentTime()), maximumYearForDST() - 27) ;
}
int equivalentYearForDST(int year)
{
static int minYear = minimumYearForDST();
int maxYear = maximumYearForDST();
int difference;
if (year > maxYear)
difference = minYear - year;
else if (year < minYear)
difference = maxYear - year;
else
return year;
int quotient = difference / 28;
int product = (quotient) * 28;
year += product;
ASSERT((year >= minYear && year <= maxYear) || (product - year == static_cast<int>(std::numeric_limits<double>::quiet_NaN())));
return year;
}
int32_t calculateUTCOffset()
{
#if PLATFORM(BREWMP)
time_t localTime = static_cast<time_t>(currentTime());
#else
time_t localTime = time(0);
#endif
tm localt;
getLocalTime(&localTime, &localt);
localt.tm_sec = 0;
localt.tm_min = 0;
localt.tm_hour = 0;
localt.tm_mday = 1;
localt.tm_mon = 0;
localt.tm_wday = 0;
localt.tm_yday = 0;
localt.tm_isdst = 0;
#if HAVE(TM_GMTOFF)
localt.tm_gmtoff = 0;
#endif
#if HAVE(TM_ZONE)
localt.tm_zone = 0;
#endif
#if HAVE(TIMEGM)
time_t utcOffset = timegm(&localt) - mktime(&localt);
#else
localt.tm_year = 109;
time_t utcOffset = 1230768000 - mktime(&localt);
#endif
return static_cast<int32_t>(utcOffset * 1000);
}
static double calculateDSTOffsetSimple(double localTimeSeconds, double utcOffset)
{
if (localTimeSeconds > maxUnixTime)
localTimeSeconds = maxUnixTime;
else if (localTimeSeconds < 0) localTimeSeconds += secondsPerDay;
double offsetTime = (localTimeSeconds * msPerSecond) + utcOffset;
int offsetHour = msToHours(offsetTime);
int offsetMinute = msToMinutes(offsetTime);
time_t localTime = static_cast<time_t>(localTimeSeconds);
tm localTM;
getLocalTime(&localTime, &localTM);
double diff = ((localTM.tm_hour - offsetHour) * secondsPerHour) + ((localTM.tm_min - offsetMinute) * 60);
if (diff < 0)
diff += secondsPerDay;
return (diff * msPerSecond);
}
double calculateDSTOffset(double ms, double utcOffset)
{
int year = msToYear(ms);
int equivalentYear = equivalentYearForDST(year);
if (year != equivalentYear) {
bool leapYear = isLeapYear(year);
int dayInYearLocal = dayInYear(ms, year);
int dayInMonth = dayInMonthFromDayInYear(dayInYearLocal, leapYear);
int month = monthFromDayInYear(dayInYearLocal, leapYear);
double day = dateToDaysFrom1970(equivalentYear, month, dayInMonth);
ms = (day * msPerDay) + msToMilliseconds(ms);
}
return calculateDSTOffsetSimple(ms / msPerSecond, utcOffset);
}
void initializeDates()
{
#ifndef NDEBUG
static bool alreadyInitialized;
ASSERT(!alreadyInitialized);
alreadyInitialized = true;
#endif
equivalentYearForDST(2000); }
static inline double ymdhmsToSeconds(long year, int mon, int day, int hour, int minute, double second)
{
double days = (day - 32075)
+ floor(1461 * (year + 4800.0 + (mon - 14) / 12) / 4)
+ 367 * (mon - 2 - (mon - 14) / 12 * 12) / 12
- floor(3 * ((year + 4900.0 + (mon - 14) / 12) / 100) / 4)
- 2440588;
return ((days * hoursPerDay + hour) * minutesPerHour + minute) * secondsPerMinute + second;
}
static const struct KnownZone {
#if !OS(WINDOWS)
const
#endif
char tzName[4];
int tzOffset;
} known_zones[] = {
{ "UT", 0 },
{ "GMT", 0 },
{ "EST", -300 },
{ "EDT", -240 },
{ "CST", -360 },
{ "CDT", -300 },
{ "MST", -420 },
{ "MDT", -360 },
{ "PST", -480 },
{ "PDT", -420 }
};
inline static void skipSpacesAndComments(const char*& s)
{
int nesting = 0;
char ch;
while ((ch = *s)) {
if (!isASCIISpace(ch)) {
if (ch == '(')
nesting++;
else if (ch == ')' && nesting > 0)
nesting--;
else if (nesting == 0)
break;
}
s++;
}
}
static int findMonth(const char* monthStr)
{
ASSERT(monthStr);
char needle[4];
for (int i = 0; i < 3; ++i) {
if (!*monthStr)
return -1;
needle[i] = static_cast<char>(toASCIILower(*monthStr++));
}
needle[3] = '\0';
const char *haystack = "janfebmaraprmayjunjulaugsepoctnovdec";
const char *str = strstr(haystack, needle);
if (str) {
int position = static_cast<int>(str - haystack);
if (position % 3 == 0)
return position / 3;
}
return -1;
}
static bool parseLong(const char* string, char** stopPosition, int base, long* result)
{
*result = strtol(string, stopPosition, base);
if (string == *stopPosition || *result == LONG_MIN || *result == LONG_MAX)
return false;
return true;
}
double parseES5DateFromNullTerminatedCharacters(const char* dateString)
{
static const long daysPerMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
const char* currentPosition = dateString;
char* postParsePosition;
long year;
if (!parseLong(currentPosition, &postParsePosition, 10, &year))
return std::numeric_limits<double>::quiet_NaN();
if (*postParsePosition != '-')
return std::numeric_limits<double>::quiet_NaN();
currentPosition = postParsePosition + 1;
long month;
if (!isASCIIDigit(*currentPosition))
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(currentPosition, &postParsePosition, 10, &month))
return std::numeric_limits<double>::quiet_NaN();
if (*postParsePosition != '-' || (postParsePosition - currentPosition) != 2)
return std::numeric_limits<double>::quiet_NaN();
currentPosition = postParsePosition + 1;
long day;
if (!isASCIIDigit(*currentPosition))
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(currentPosition, &postParsePosition, 10, &day))
return std::numeric_limits<double>::quiet_NaN();
if (*postParsePosition != 'T' || (postParsePosition - currentPosition) != 2)
return std::numeric_limits<double>::quiet_NaN();
currentPosition = postParsePosition + 1;
long hours;
if (!isASCIIDigit(*currentPosition))
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(currentPosition, &postParsePosition, 10, &hours))
return std::numeric_limits<double>::quiet_NaN();
if (*postParsePosition != ':' || (postParsePosition - currentPosition) != 2)
return std::numeric_limits<double>::quiet_NaN();
currentPosition = postParsePosition + 1;
long minutes;
if (!isASCIIDigit(*currentPosition))
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(currentPosition, &postParsePosition, 10, &minutes))
return std::numeric_limits<double>::quiet_NaN();
if (*postParsePosition != ':' || (postParsePosition - currentPosition) != 2)
return std::numeric_limits<double>::quiet_NaN();
currentPosition = postParsePosition + 1;
long intSeconds;
if (!isASCIIDigit(*currentPosition))
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(currentPosition, &postParsePosition, 10, &intSeconds))
return std::numeric_limits<double>::quiet_NaN();
if ((postParsePosition - currentPosition) != 2)
return std::numeric_limits<double>::quiet_NaN();
double seconds = intSeconds;
if (*postParsePosition == '.') {
currentPosition = postParsePosition + 1;
if (!isASCIIDigit(*currentPosition))
return std::numeric_limits<double>::quiet_NaN();
long fracSeconds;
if (!parseLong(currentPosition, &postParsePosition, 10, &fracSeconds))
return std::numeric_limits<double>::quiet_NaN();
long numFracDigits = postParsePosition - currentPosition;
seconds += fracSeconds * pow(10.0, static_cast<double>(-numFracDigits));
}
currentPosition = postParsePosition;
if (month < 1 || month > 12)
return std::numeric_limits<double>::quiet_NaN();
if (day < 1 || day > daysPerMonth[month - 1])
return std::numeric_limits<double>::quiet_NaN();
if (month == 2 && day > 28 && !isLeapYear(year))
return std::numeric_limits<double>::quiet_NaN();
if (hours < 0 || hours > 24)
return std::numeric_limits<double>::quiet_NaN();
if (hours == 24 && (minutes || seconds))
return std::numeric_limits<double>::quiet_NaN();
if (minutes < 0 || minutes > 59)
return std::numeric_limits<double>::quiet_NaN();
if (seconds < 0 || seconds >= 61)
return std::numeric_limits<double>::quiet_NaN();
if (seconds > 60) {
seconds = 60;
}
long timeZoneSeconds = 0;
if (*currentPosition != 'Z') {
bool tzNegative;
if (*currentPosition == '-')
tzNegative = true;
else if (*currentPosition == '+')
tzNegative = false;
else
return std::numeric_limits<double>::quiet_NaN();
currentPosition += 1;
long tzHours;
long tzHoursAbs;
long tzMinutes;
if (!isASCIIDigit(*currentPosition))
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(currentPosition, &postParsePosition, 10, &tzHours))
return std::numeric_limits<double>::quiet_NaN();
if (*postParsePosition != ':' || (postParsePosition - currentPosition) != 2)
return std::numeric_limits<double>::quiet_NaN();
tzHoursAbs = labs(tzHours);
currentPosition = postParsePosition + 1;
if (!isASCIIDigit(*currentPosition))
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(currentPosition, &postParsePosition, 10, &tzMinutes))
return std::numeric_limits<double>::quiet_NaN();
if ((postParsePosition - currentPosition) != 2)
return std::numeric_limits<double>::quiet_NaN();
currentPosition = postParsePosition;
if (tzHoursAbs > 24)
return std::numeric_limits<double>::quiet_NaN();
if (tzMinutes < 0 || tzMinutes > 59)
return std::numeric_limits<double>::quiet_NaN();
timeZoneSeconds = 60 * (tzMinutes + (60 * tzHoursAbs));
if (tzNegative)
timeZoneSeconds = -timeZoneSeconds;
} else {
currentPosition += 1;
}
if (*currentPosition)
return std::numeric_limits<double>::quiet_NaN();
double dateSeconds = ymdhmsToSeconds(year, month, day, hours, minutes, seconds) - timeZoneSeconds;
return dateSeconds * msPerSecond;
}
static double parseDateFromNullTerminatedCharacters(const char* dateString, bool& haveTZ, int& offset)
{
haveTZ = false;
offset = 0;
skipSpacesAndComments(dateString);
long month = -1;
const char *wordStart = dateString;
while (*dateString && !isASCIIDigit(*dateString)) {
if (isASCIISpace(*dateString) || *dateString == '(') {
if (dateString - wordStart >= 3)
month = findMonth(wordStart);
skipSpacesAndComments(dateString);
wordStart = dateString;
} else
dateString++;
}
if (month == -1 && wordStart != dateString)
month = findMonth(wordStart);
skipSpacesAndComments(dateString);
if (!*dateString)
return std::numeric_limits<double>::quiet_NaN();
char* newPosStr;
long day;
if (!parseLong(dateString, &newPosStr, 10, &day))
return std::numeric_limits<double>::quiet_NaN();
dateString = newPosStr;
if (!*dateString)
return std::numeric_limits<double>::quiet_NaN();
if (day < 0)
return std::numeric_limits<double>::quiet_NaN();
long year = 0;
if (day > 31) {
if (*dateString != '/')
return std::numeric_limits<double>::quiet_NaN();
if (!*++dateString)
return std::numeric_limits<double>::quiet_NaN();
year = day;
if (!parseLong(dateString, &newPosStr, 10, &month))
return std::numeric_limits<double>::quiet_NaN();
month -= 1;
dateString = newPosStr;
if (*dateString++ != '/' || !*dateString)
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(dateString, &newPosStr, 10, &day))
return std::numeric_limits<double>::quiet_NaN();
dateString = newPosStr;
} else if (*dateString == '/' && month == -1) {
dateString++;
month = day - 1; if (!parseLong(dateString, &newPosStr, 10, &day))
return std::numeric_limits<double>::quiet_NaN();
if (day < 1 || day > 31)
return std::numeric_limits<double>::quiet_NaN();
dateString = newPosStr;
if (*dateString == '/')
dateString++;
if (!*dateString)
return std::numeric_limits<double>::quiet_NaN();
} else {
if (*dateString == '-')
dateString++;
skipSpacesAndComments(dateString);
if (*dateString == ',')
dateString++;
if (month == -1) { month = findMonth(dateString);
if (month == -1)
return std::numeric_limits<double>::quiet_NaN();
while (*dateString && *dateString != '-' && *dateString != ',' && !isASCIISpace(*dateString))
dateString++;
if (!*dateString)
return std::numeric_limits<double>::quiet_NaN();
if (*dateString != '-' && *dateString != '/' && *dateString != ',' && !isASCIISpace(*dateString))
return std::numeric_limits<double>::quiet_NaN();
dateString++;
}
}
if (month < 0 || month > 11)
return std::numeric_limits<double>::quiet_NaN();
if (year <= 0 && *dateString) {
if (!parseLong(dateString, &newPosStr, 10, &year))
return std::numeric_limits<double>::quiet_NaN();
}
long hour = 0;
long minute = 0;
long second = 0;
if (!*newPosStr)
dateString = newPosStr;
else {
if (!(isASCIISpace(*newPosStr) || *newPosStr == ',')) {
if (*newPosStr != ':')
return std::numeric_limits<double>::quiet_NaN();
year = -1;
} else {
dateString = ++newPosStr;
skipSpacesAndComments(dateString);
}
parseLong(dateString, &newPosStr, 10, &hour);
if (newPosStr != dateString) {
dateString = newPosStr;
if (hour < 0 || hour > 23)
return std::numeric_limits<double>::quiet_NaN();
if (!*dateString)
return std::numeric_limits<double>::quiet_NaN();
if (*dateString++ != ':')
return std::numeric_limits<double>::quiet_NaN();
if (!parseLong(dateString, &newPosStr, 10, &minute))
return std::numeric_limits<double>::quiet_NaN();
dateString = newPosStr;
if (minute < 0 || minute > 59)
return std::numeric_limits<double>::quiet_NaN();
if (*dateString && *dateString != ':' && !isASCIISpace(*dateString))
return std::numeric_limits<double>::quiet_NaN();
if (*dateString ==':') {
dateString++;
if (!parseLong(dateString, &newPosStr, 10, &second))
return std::numeric_limits<double>::quiet_NaN();
dateString = newPosStr;
if (second < 0 || second > 59)
return std::numeric_limits<double>::quiet_NaN();
}
skipSpacesAndComments(dateString);
if (strncasecmp(dateString, "AM", 2) == 0) {
if (hour > 12)
return std::numeric_limits<double>::quiet_NaN();
if (hour == 12)
hour = 0;
dateString += 2;
skipSpacesAndComments(dateString);
} else if (strncasecmp(dateString, "PM", 2) == 0) {
if (hour > 12)
return std::numeric_limits<double>::quiet_NaN();
if (hour != 12)
hour += 12;
dateString += 2;
skipSpacesAndComments(dateString);
}
}
}
if (*dateString) {
if (strncasecmp(dateString, "GMT", 3) == 0 || strncasecmp(dateString, "UTC", 3) == 0) {
dateString += 3;
haveTZ = true;
}
if (*dateString == '+' || *dateString == '-') {
long o;
if (!parseLong(dateString, &newPosStr, 10, &o))
return std::numeric_limits<double>::quiet_NaN();
dateString = newPosStr;
if (o < -9959 || o > 9959)
return std::numeric_limits<double>::quiet_NaN();
int sgn = (o < 0) ? -1 : 1;
o = labs(o);
if (*dateString != ':') {
offset = ((o / 100) * 60 + (o % 100)) * sgn;
} else { long o2;
if (!parseLong(dateString, &newPosStr, 10, &o2))
return std::numeric_limits<double>::quiet_NaN();
dateString = newPosStr;
offset = (o * 60 + o2) * sgn;
}
haveTZ = true;
} else {
for (size_t i = 0; i < WTF_ARRAY_LENGTH(known_zones); ++i) {
if (0 == strncasecmp(dateString, known_zones[i].tzName, strlen(known_zones[i].tzName))) {
offset = known_zones[i].tzOffset;
dateString += strlen(known_zones[i].tzName);
haveTZ = true;
break;
}
}
}
}
skipSpacesAndComments(dateString);
if (*dateString && year == -1) {
if (!parseLong(dateString, &newPosStr, 10, &year))
return std::numeric_limits<double>::quiet_NaN();
dateString = newPosStr;
}
skipSpacesAndComments(dateString);
if (*dateString)
return std::numeric_limits<double>::quiet_NaN();
if (year >= 0 && year < 100) {
if (year < 50)
year += 2000;
else
year += 1900;
}
return ymdhmsToSeconds(year, month + 1, day, hour, minute, second) * msPerSecond;
}
double parseDateFromNullTerminatedCharacters(const char* dateString)
{
bool haveTZ;
int offset;
double ms = parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset);
if (isnan(ms))
return std::numeric_limits<double>::quiet_NaN();
if (!haveTZ) {
double utcOffset = calculateUTCOffset();
double dstOffset = calculateDSTOffset(ms, utcOffset);
offset = static_cast<int>((utcOffset + dstOffset) / msPerMinute);
}
return ms - (offset * msPerMinute);
}
double timeClip(double t)
{
if (!isfinite(t))
return std::numeric_limits<double>::quiet_NaN();
if (fabs(t) > maxECMAScriptTime)
return std::numeric_limits<double>::quiet_NaN();
return trunc(t);
}
String makeRFC2822DateString(unsigned dayOfWeek, unsigned day, unsigned month, unsigned year, unsigned hours, unsigned minutes, unsigned seconds, int utcOffset)
{
StringBuilder stringBuilder;
stringBuilder.append(weekdayName[dayOfWeek]);
stringBuilder.append(", ");
stringBuilder.append(String::number(day));
stringBuilder.append(" ");
stringBuilder.append(monthName[month]);
stringBuilder.append(" ");
stringBuilder.append(String::number(year));
stringBuilder.append(" ");
stringBuilder.append(twoDigitStringFromNumber(hours));
stringBuilder.append(':');
stringBuilder.append(twoDigitStringFromNumber(minutes));
stringBuilder.append(':');
stringBuilder.append(twoDigitStringFromNumber(seconds));
stringBuilder.append(' ');
stringBuilder.append(utcOffset > 0 ? "+" : "-");
int absoluteUTCOffset = abs(utcOffset);
stringBuilder.append(twoDigitStringFromNumber(absoluteUTCOffset / 60));
stringBuilder.append(twoDigitStringFromNumber(absoluteUTCOffset % 60));
return stringBuilder.toString();
}
}
#if USE(JSC)
namespace JSC {
static double getDSTOffset(ExecState* exec, double ms, double utcOffset)
{
DSTOffsetCache& cache = exec->globalData().dstOffsetCache;
double start = cache.start;
double end = cache.end;
if (start <= ms) {
if (ms <= end) return cache.offset;
double newEnd = end + cache.increment;
if (ms <= newEnd) {
double endOffset = calculateDSTOffset(newEnd, utcOffset);
if (cache.offset == endOffset) {
cache.end = newEnd;
cache.increment = msPerMonth;
return endOffset;
} else {
double offset = calculateDSTOffset(ms, utcOffset);
if (offset == endOffset) {
cache.start = ms;
cache.end = newEnd;
cache.increment = msPerMonth;
} else {
cache.increment /= 3;
cache.end = ms;
}
cache.offset = offset;
return offset;
}
}
}
double offset = calculateDSTOffset(ms, utcOffset);
cache.offset = offset;
cache.start = ms;
cache.end = ms;
cache.increment = msPerMonth;
return offset;
}
double getUTCOffset(ExecState* exec)
{
double utcOffset = exec->globalData().cachedUTCOffset;
if (!isnan(utcOffset))
return utcOffset;
exec->globalData().cachedUTCOffset = calculateUTCOffset();
return exec->globalData().cachedUTCOffset;
}
double gregorianDateTimeToMS(ExecState* exec, const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
{
double day = dateToDaysFrom1970(t.year + 1900, t.month, t.monthDay);
double ms = timeToMS(t.hour, t.minute, t.second, milliSeconds);
double result = (day * WTF::msPerDay) + ms;
if (!inputIsUTC) { double utcOffset = getUTCOffset(exec);
result -= utcOffset;
result -= getDSTOffset(exec, result, utcOffset);
}
return result;
}
void msToGregorianDateTime(ExecState* exec, double ms, bool outputIsUTC, GregorianDateTime& tm)
{
double dstOff = 0.0;
double utcOff = 0.0;
if (!outputIsUTC) {
utcOff = getUTCOffset(exec);
dstOff = getDSTOffset(exec, ms, utcOff);
ms += dstOff + utcOff;
}
const int year = msToYear(ms);
tm.second = msToSeconds(ms);
tm.minute = msToMinutes(ms);
tm.hour = msToHours(ms);
tm.weekDay = msToWeekDay(ms);
tm.yearDay = dayInYear(ms, year);
tm.monthDay = dayInMonthFromDayInYear(tm.yearDay, isLeapYear(year));
tm.month = monthFromDayInYear(tm.yearDay, isLeapYear(year));
tm.year = year - 1900;
tm.isDST = dstOff != 0.0;
tm.utcOffset = static_cast<long>((dstOff + utcOff) / WTF::msPerSecond);
tm.timeZone = nullptr;
}
double parseDateFromNullTerminatedCharacters(ExecState* exec, const char* dateString)
{
ASSERT(exec);
bool haveTZ;
int offset;
double ms = WTF::parseDateFromNullTerminatedCharacters(dateString, haveTZ, offset);
if (isnan(ms))
return std::numeric_limits<double>::quiet_NaN();
if (!haveTZ) {
double utcOffset = getUTCOffset(exec);
double dstOffset = getDSTOffset(exec, ms, utcOffset);
offset = static_cast<int>((utcOffset + dstOffset) / WTF::msPerMinute);
}
return ms - (offset * WTF::msPerMinute);
}
} #endif // USE(JSC)