#include "config.h"
#include "DateMath.h"
#include <math.h>
#include <stdint.h>
#include <value.h>
#include <wtf/Assertions.h>
#if PLATFORM(DARWIN)
#include <notify.h>
#endif
#if HAVE(SYS_TIME_H)
#include <sys/time.h>
#endif
#if HAVE(SYS_TIMEB_H)
#include <sys/timeb.h>
#endif
#if PLATFORM(WIN_OS)
#include <windows.h>
#endif
namespace KJS {
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 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 inline 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;
}
static inline 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);
}
static 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;
}
static inline bool checkMonth(int dayInYear, int& startDayOfThisMonth, int& startDayOfNextMonth, int daysInThisMonth)
{
startDayOfThisMonth = startDayOfNextMonth;
startDayOfNextMonth += daysInThisMonth;
return (dayInYear <= startDayOfNextMonth);
}
static inline 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);
}
static int dateToDayInYear(int year, int month, int day)
{
year += month / 12;
month %= 12;
if (month < 0) {
month += 12;
--year;
}
int yearday = static_cast<int>(floor(daysFrom1970ToYear(year)));
int monthday = monthToDayInYear(month, isLeapYear(year));
return yearday + monthday + day - 1;
}
#if PLATFORM(WIN_OS)
static LARGE_INTEGER qpcFrequency;
static bool syncedTime;
static double highResUpTime()
{
static LARGE_INTEGER qpcLast;
static DWORD tickCountLast;
static bool inited;
LARGE_INTEGER qpc;
QueryPerformanceCounter(&qpc);
DWORD tickCount = GetTickCount();
if (inited) {
__int64 qpcElapsed = ((qpc.QuadPart - qpcLast.QuadPart) * 1000) / qpcFrequency.QuadPart;
__int64 tickCountElapsed;
if (tickCount >= tickCountLast)
tickCountElapsed = (tickCount - tickCountLast);
else {
__int64 tickCountLarge = tickCount + 0x100000000I64;
tickCountElapsed = tickCountLarge - tickCountLast;
}
__int64 diff = tickCountElapsed - qpcElapsed;
if (diff > 500 || diff < -500)
syncedTime = false;
} else
inited = true;
qpcLast = qpc;
tickCountLast = tickCount;
return (1000.0 * qpc.QuadPart) / static_cast<double>(qpcFrequency.QuadPart);;
}
static double lowResUTCTime()
{
struct _timeb timebuffer;
_ftime(&timebuffer);
return timebuffer.time * msPerSecond + timebuffer.millitm;
}
static bool qpcAvailable()
{
static bool available;
static bool checked;
if (checked)
return available;
available = QueryPerformanceFrequency(&qpcFrequency);
checked = true;
return available;
}
#endif
double getCurrentUTCTime()
{
#if PLATFORM(WIN_OS)
static bool started;
static double syncLowResUTCTime;
static double syncHighResUpTime;
static double lastUTCTime;
double lowResTime = lowResUTCTime();
if (!qpcAvailable())
return lowResTime;
double highResTime = highResUpTime();
if (!syncedTime) {
timeBeginPeriod(1); syncLowResUTCTime = lowResTime = lowResUTCTime();
timeEndPeriod(1); syncHighResUpTime = highResTime;
syncedTime = true;
}
double highResElapsed = highResTime - syncHighResUpTime;
double utc = syncLowResUTCTime + highResElapsed;
double lowResElapsed = lowResTime - syncLowResUTCTime;
const double maximumAllowedDriftMsec = 15.625 * 2.0; if (fabs(highResElapsed - lowResElapsed) > maximumAllowedDriftMsec)
syncedTime = false;
const double backwardTimeLimit = 2000.0;
if (utc < lastUTCTime && (lastUTCTime - utc) < backwardTimeLimit)
return lastUTCTime;
lastUTCTime = utc;
#else
struct timeval tv;
gettimeofday(&tv, 0);
double utc = floor(tv.tv_sec * msPerSecond + tv.tv_usec / 1000);
#endif
return utc;
}
static inline int maximumYearForDST()
{
return 2037;
}
static int mimimumYearForDST()
{
static int minYear = std::min(msToYear(getCurrentUTCTime()), maximumYearForDST() - 27) ;
return minYear;
}
int equivalentYearForDST(int year)
{
static int minYear = mimimumYearForDST();
static 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>(NaN)));
return year;
}
double getUTCOffset()
{
#if PLATFORM(DARWIN)
static bool triedToRegister = false;
static bool haveNotificationToken = false;
static int notificationToken;
if (!triedToRegister) {
triedToRegister = true;
uint32_t status = notify_register_check("com.apple.system.timezone", ¬ificationToken);
if (status == NOTIFY_STATUS_OK)
haveNotificationToken = true;
}
static bool haveCachedOffset = false;
static double cachedOffset;
if (haveNotificationToken && haveCachedOffset) {
int notified;
uint32_t status = notify_check(notificationToken, ¬ified);
if (status == NOTIFY_STATUS_OK && !notified)
return cachedOffset;
}
#endif
tm localt;
memset(&localt, 0, sizeof(localt));
localt.tm_mday = 1;
localt.tm_year = 100;
double utcOffset = 946684800.0 - mktime(&localt);
utcOffset *= msPerSecond;
#if PLATFORM(DARWIN)
haveCachedOffset = true;
cachedOffset = utcOffset;
#endif
return utcOffset;
}
static double getDSTOffsetSimple(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;
#if PLATFORM(QT)
#if USE(MULTIPLE_THREADS)
#error Mulitple threads are currently not supported in the Qt/mingw build
#endif
localTM = *localtime(&localTime);
#elif PLATFORM(WIN_OS)
#if COMPILER(MSVC7)
localTM = *localtime(&localTime);
#else
localtime_s(&localTM, &localTime);
#endif
#else
localtime_r(&localTime, &localTM);
#endif
double diff = ((localTM.tm_hour - offsetHour) * secondsPerHour) + ((localTM.tm_min - offsetMinute) * 60);
if (diff < 0)
diff += secondsPerDay;
return (diff * msPerSecond);
}
static double getDSTOffset(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);
int day = dateToDayInYear(equivalentYear, month, dayInMonth);
ms = (day * msPerDay) + msToMilliseconds(ms);
}
return getDSTOffsetSimple(ms / msPerSecond, utcOffset);
}
double gregorianDateTimeToMS(const GregorianDateTime& t, double milliSeconds, bool inputIsUTC)
{
int day = dateToDayInYear(t.year + 1900, t.month, t.monthDay);
double ms = timeToMS(t.hour, t.minute, t.second, milliSeconds);
double result = (day * msPerDay) + ms;
if (!inputIsUTC) { double utcOffset = getUTCOffset();
result -= utcOffset;
result -= getDSTOffset(result, utcOffset);
}
return result;
}
void msToGregorianDateTime(double ms, bool outputIsUTC, GregorianDateTime& tm)
{
double dstOff = 0.0;
const double utcOff = getUTCOffset();
if (!outputIsUTC) { dstOff = getDSTOffset(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) / msPerSecond);
tm.timeZone = NULL;
}
}