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; }
bool DateComponents::parseWeek(const String& src, unsigned start, unsigned& end) { unsigned index; if (!parseYear(src, start, index)) return false; // 4 characters ('-' 'W' digit digit) are needed. if (index + 3 >= src.length()) return false; if (src[index] != '-') return false; ++index; if (src[index] != 'W') return false; ++index; int week; if (!toInt(src, index, 2, week) || week < minimumWeekNumber || week > maxWeekNumberInYear()) return false; if (m_year == maximumYear() && week > maximumWeekInMaximumYear) return false; m_week = week; end = index + 2; m_type = Week; return true; }
bool DateComponents::setWeek(int year, int weekNumber) { m_type = Invalid; if (year < minimumYear() || year > maximumYear()) return false; m_year = year; if (weekNumber < 1 || weekNumber > maxWeekNumberInYear()) return false; m_week = weekNumber; m_type = Week; return true; }
bool DateComponents::parseYear(const String& src, unsigned start, unsigned& end) { unsigned digitsLength = countDigits(src, start); // Needs at least 4 digits according to the standard. if (digitsLength < 4) return false; int year; if (!toInt(src, start, digitsLength, year)) return false; if (year < minimumYear() || year > maximumYear()) return false; m_year = year; end = start + digitsLength; return true; }
bool DateComponents::setMonthsSinceEpoch(double months) { if (!std::isfinite(months)) return false; months = round(months); double doubleMonth = positiveFmod(months, 12); double doubleYear = 1970 + (months - doubleMonth) / 12; if (doubleYear < minimumYear() || maximumYear() < doubleYear) return false; int year = static_cast<int>(doubleYear); int month = static_cast<int>(doubleMonth); if (!withinHTMLDateLimits(year, month)) return false; m_year = year; m_month = month; m_type = Month; return true; }