예제 #1
0
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;
}
double DateComponents::millisecondsSinceEpoch() const
{
    switch (m_type) {
    case Date:
        return dateToDaysFrom1970(m_year, m_month, m_monthDay) * msPerDay;
    case DateTime:
    case DateTimeLocal:
        return dateToDaysFrom1970(m_year, m_month, m_monthDay) * msPerDay + millisecondsSinceEpochForTime();
    case Month:
        return dateToDaysFrom1970(m_year, m_month, 1) * msPerDay;
    case Time:
        return millisecondsSinceEpochForTime();
    case Week:
        return (dateToDaysFrom1970(m_year, 0, 1) + offsetTo1stWeekStart(m_year) + (m_week - 1) * 7) * msPerDay;
    case Invalid:
        break;
    }
    ASSERT_NOT_REACHED();
    return invalidMilliseconds();
}