bool DateComponents::setMillisecondsSinceEpochForWeek(double ms) { m_type = Invalid; if (!std::isfinite(ms)) return false; ms = round(ms); m_year = msToYear(ms); if (m_year < minimumYear() || m_year > maximumYear()) return false; int yearDay = dayInYear(ms, m_year); int offset = offsetTo1stWeekStart(m_year); if (yearDay < offset) { // The day belongs to the last week of the previous year. m_year--; if (m_year <= minimumYear()) return false; m_week = maxWeekNumberInYear(); } else { m_week = ((yearDay - offset) / 7) + 1; if (m_week > maxWeekNumberInYear()) { m_year++; m_week = 1; } if (m_year > maximumYear() || (m_year == maximumYear() && m_week > maximumWeekInMaximumYear)) return false; } m_type = Week; return true; }
static SoupDate* msToSoupDate(double ms) { int year = msToYear(ms); int dayOfYear = dayInYear(ms, year); bool leapYear = isLeapYear(year); return soup_date_new(year, monthFromDayInYear(dayOfYear, leapYear), dayInMonthFromDayInYear(dayOfYear, leapYear), msToHours(ms), msToMinutes(ms), static_cast<int>(ms / 1000) % 60); }
void msToGregorianDateTime(double ms, bool outputIsUTC, GregorianDateTime& tm) { // input is UTC double dstOff = 0.0; const double utcOff = getUTCOffset(); if (!outputIsUTC) { // convert to local time 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 = outputIsUTC ? 0 : static_cast<long>((dstOff + utcOff) / msPerSecond); tm.timeZone = NULL; }
bool DateComponents::setMillisecondsSinceEpochForDateInternal(double ms) { m_year = msToYear(ms); int yearDay = dayInYear(ms, m_year); m_month = monthFromDayInYear(yearDay, isLeapYear(m_year)); m_monthDay = dayInMonthFromDayInYear(yearDay, isLeapYear(m_year)); return true; }
static inline int msToMonth(double ms) { int step; int year = msToYear(ms); int d = dayInYear(ms, year); if (d < (step = 31)) return 0; step += (isInLeapYear(ms) ? 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 int minimumYearForDST() { // Because of the 2038 issue (see maximumYearForDST) if the current year is // greater than the max year minus 27 (2010), we want to use the max year // minus 27 instead, to ensure there is a range of 28 years that all years // can map to. return std::min(msToYear(jsCurrentTime()), maximumYearForDST() - 27) ; }
void msToGregorianDateTime(double ms, bool outputIsUTC, GregorianDateTime& tm) { // input is UTC double dstOff = 0.0; if (!outputIsUTC) { // convert to local time dstOff = getDSTOffset(ms); ms += dstOff + getUTCOffset(); } tm.second = msToSeconds(ms); tm.minute = msToMinutes(ms); tm.hour = msToHours(ms); tm.weekDay = msToWeekDay(ms); tm.monthDay = msToDayInMonth(ms); tm.yearDay = dayInYear(ms, msToYear(ms)); tm.month = msToMonth(ms); tm.year = msToYear(ms) - 1900; tm.isDST = dstOff != 0.0; tm.utcOffset = static_cast<long>((dstOff + getUTCOffset()) / msPerSecond); tm.timeZone = NULL; }
// Get the DST offset, given a time in UTC static double getDSTOffset(double ms) { // On Mac OS X, the call to localtime (see getDSTOffsetSimple) will return historically accurate // DST information (e.g. New Zealand did not have DST from 1946 to 1974) however the JavaScript // standard explicitly dictates that historical information should not be considered when // determining DST. For this reason we shift away from years that localtime can handle but would // return historically accurate information. int year = msToYear(ms); int equvalentYear = equivalentYearForDST(year); if (year != equvalentYear) { int day = dateToDayInYear(equvalentYear, msToMonth(ms), msToDayInMonth(ms)); ms = (day * msPerDay) + msToMilliseconds(ms); } return getDSTOffsetSimple(ms / msPerSecond); }
// Returns combined offset in millisecond (UTC + DST). LocalTimeOffset calculateLocalTimeOffset(double ms, TimeType inputTimeType) { #if HAVE(TM_GMTOFF) double localToUTCTimeOffset = inputTimeType == LocalTime ? calculateUTCOffset() : 0; #else double localToUTCTimeOffset = calculateUTCOffset(); #endif if (inputTimeType == LocalTime) ms -= localToUTCTimeOffset; // On Mac OS X, the call to localtime (see calculateDSTOffset) will return historically accurate // DST information (e.g. New Zealand did not have DST from 1946 to 1974) however the JavaScript // standard explicitly dictates that historical information should not be considered when // determining DST. For this reason we shift away from years that localtime can handle but would // return historically accurate information. 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); } double localTimeSeconds = ms / msPerSecond; if (localTimeSeconds > maxUnixTime) localTimeSeconds = maxUnixTime; else if (localTimeSeconds < 0) // Go ahead a day to make localtime work (does not work with 0). localTimeSeconds += secondsPerDay; // FIXME: time_t has a potential problem in 2038. time_t localTime = static_cast<time_t>(localTimeSeconds); #if HAVE(TM_GMTOFF) tm localTM; getLocalTime(&localTime, &localTM); return LocalTimeOffset(localTM.tm_isdst, localTM.tm_gmtoff * msPerSecond); #else double dstOffset = calculateDSTOffset(localTime, localToUTCTimeOffset); return LocalTimeOffset(dstOffset, localToUTCTimeOffset + dstOffset); #endif }
static inline int msToDayInMonth(double ms) { int step, next; int year = msToYear(ms); int d = dayInYear(ms, year); if (d <= (next = 30)) return d + 1; step = next; next += (isInLeapYear(ms) ? 29 : 28); if (d <= next) return d - step; step = next; if (d <= (next += 31)) return d - step; step = next; if (d <= (next += 30)) return d - step; step = next; if (d <= (next += 31)) return d - step; step = next; if (d <= (next += 30)) return d - step; step = next; if (d <= (next += 31)) return d - step; step = next; if (d <= (next += 31)) return d - step; step = next; if (d <= (next += 30)) return d - step; step = next; if (d <= (next += 31)) return d - step; step = next; if (d <= (next += 30)) return d - step; step = next; return d - step; }
// Get the DST offset, given a time in UTC static double calculateDSTOffset(double ms, double utcOffset) { // On Mac OS X, the call to localtime (see calculateDSTOffsetSimple) will return historically accurate // DST information (e.g. New Zealand did not have DST from 1946 to 1974) however the JavaScript // standard explicitly dictates that historical information should not be considered when // determining DST. For this reason we shift away from years that localtime can handle but would // return historically accurate information. 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); }
// input is UTC void msToGregorianDateTime(VM& vm, double ms, WTF::TimeType outputTimeType, GregorianDateTime& tm) { LocalTimeOffset localTime; if (outputTimeType == WTF::LocalTime) { localTime = localTimeOffset(vm, ms); ms += localTime.offset; } const int year = msToYear(ms); tm.setSecond(msToSeconds(ms)); tm.setMinute(msToMinutes(ms)); tm.setHour(msToHours(ms)); tm.setWeekDay(msToWeekDay(ms)); tm.setYearDay(dayInYear(ms, year)); tm.setMonthDay(dayInMonthFromDayInYear(tm.yearDay(), isLeapYear(year))); tm.setMonth(monthFromDayInYear(tm.yearDay(), isLeapYear(year))); tm.setYear(year); tm.setIsDST(localTime.isDST); tm.setUtcOffset(localTime.offset / WTF::msPerSecond); }
// input is UTC void msToGregorianDateTime(ExecState* exec, double ms, bool outputIsUTC, GregorianDateTime& tm) { LocalTimeOffset localTime(false, 0); if (!outputIsUTC) { localTime = localTimeOffset(exec, ms); ms += localTime.offset; } 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 = localTime.isDST; tm.utcOffset = localTime.offset / WTF::msPerSecond; tm.timeZone = NULL; }
// input is UTC 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.setSecond(msToSeconds(ms)); tm.setMinute(msToMinutes(ms)); tm.setHour(msToHours(ms)); tm.setWeekDay(msToWeekDay(ms)); tm.setYearDay(dayInYear(ms, year)); tm.setMonthDay(dayInMonthFromDayInYear(tm.yearDay(), isLeapYear(year))); tm.setMonth(monthFromDayInYear(tm.yearDay(), isLeapYear(year))); tm.setYear(year); tm.setIsDST(dstOff != 0.0); tm.setUtcOffset(static_cast<long>((dstOff + utcOff) / WTF::msPerSecond)); }
// input is UTC 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 = NULL; }
static inline bool isInLeapYear(double ms) { return isLeapYear(msToYear(ms)); }